Skip to content

Commit

Permalink
Merge pull request vianney#3 from guns/remount-ext4-with-nobarrier
Browse files Browse the repository at this point in the history
Remount / with barrier=0 when fstype is ext4
  • Loading branch information
vianney committed Jun 28, 2014
2 parents 11f2c66 + f5e2c8f commit 15420df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
31 changes: 28 additions & 3 deletions arch-luks-suspend
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

set -e -u
trap 'echo "Press ENTER to continue."; read dummy' ERR
Expand All @@ -9,22 +9,33 @@ trap 'echo "Press ENTER to continue."; read dummy' ERR
INITRAMFS_DIR=/run/initramfs
SYSTEM_SLEEP_PATH=/usr/lib/systemd/system-sleep
BIND_PATHS="/sys /proc /dev /run"
REMOUNT=0
# Retrieve cryptdevice name from boot cmdline
CRYPTNAME="$(sed -n 's/.*cryptdevice=[^: ]*:\([^: ]*\).*$/\1/p' /proc/cmdline)"

# run_dir DIR ARGS...
# Run all executable scripts in directory DIR with arguments ARGS
run_dir() {
dir=$1
local dir=$1
shift
find "${dir}" -type f -executable -exec "{}" "$@" ";"
}

# Restore chroot
umount_initramfs() {
local p
for p in ${BIND_PATHS}; do
! mountpoint -q "${INITRAMFS_DIR}${p}" || umount "${INITRAMFS_DIR}${p}"
done
}

ext4_cryptdevice_mount_options() {
local mt="$(grep "^/dev/mapper/${1} " /proc/mounts | cut -d ' ' -f 3,4)"
if [[ "${mt:0:5}" == "ext4 " ]]; then
echo "${mt:5}"
fi
}

################################################################################
## Main script

Expand All @@ -46,12 +57,26 @@ systemctl stop systemd-udevd-control.socket
systemctl stop systemd-udevd-kernel.socket
systemctl stop systemd-udevd.service

# Journalled ext4 filesystems in kernel versions 3.11+ will block suspend
# if mounted with `barrier=1`, which is the default. Temporarily remount with
# `barrier=0` if this is true of the crypt fs.
MOUNT_OPTS="$(ext4_cryptdevice_mount_options "$CRYPTNAME")"
if [[ "$MOUNT_OPTS" ]] && ! [[ "$MOUNT_OPTS" == *nobarrier* || "$MOUNT_OPTS" == *barrier=0* ]]; then
REMOUNT=1
mount -o remount,"$MOUNT_OPTS",barrier=0 /
fi

# Synchronize filesystems before luksSuspend
sync

# Hand over execution to script inside initramfs
cd "${INITRAMFS_DIR}"
chroot . /suspend
chroot . /suspend "$CRYPTNAME"

# Restore original mount options if necessary
if ((REMOUNT)); then
mount -o remount,"$MOUNT_OPTS",barrier=1 /
fi

# Restart udev
systemctl start systemd-udevd-control.socket
Expand Down
5 changes: 2 additions & 3 deletions initramfs-suspend
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/ash

cryptname="${1}"

# Start udev from initramfs
/usr/lib/systemd/systemd-udevd --daemon --resolve-names=never

# Retrieve cryptdevice name from boot cmdline
cryptname=$(sed -n 's/.*cryptdevice=[^: ]*:\([^: ]*\).*$/\1/p' /proc/cmdline)

# Suspend root device
[ -z "${cryptname}" ] || cryptsetup luksSuspend "${cryptname}"

Expand Down

0 comments on commit 15420df

Please sign in to comment.