Harald Hoyer 5f0dd7
From 0386e4627779cb51f4292b3c642d90586d5e71b4 Mon Sep 17 00:00:00 2001
Harald Hoyer 5f0dd7
From: Martin Wilck <mwilck@suse.de>
Harald Hoyer 5f0dd7
Date: Wed, 29 Jan 2020 23:53:29 +0100
Harald Hoyer 5f0dd7
Subject: [PATCH] dracut.sh: don't call fsfreeze on subvol of root file system
Harald Hoyer 5f0dd7
Harald Hoyer 5f0dd7
dracut.sh already doesn't call fsfreeze if the output file is on
Harald Hoyer 5f0dd7
the root file system. For btrfs, however, this is not sufficient.
Harald Hoyer 5f0dd7
Because fsfreeze is a superblock operation, and all btrfs subvolumes
Harald Hoyer 5f0dd7
share the same superblock, fsfreeze may freeze the entire system
Harald Hoyer 5f0dd7
if the subvolume on which the output file is written and / are
Harald Hoyer 5f0dd7
subvolumes of the same file system. Avoid this by comparing file
Harald Hoyer 5f0dd7
system UUIDs for btrfs.
Harald Hoyer 5f0dd7
Harald Hoyer 5f0dd7
Fixes: de576db3c225 ("call fsfreeze(8) on /boot to flush initramfs data & metadata to media")
Harald Hoyer 5f0dd7
---
Harald Hoyer 5f0dd7
 dracut.sh | 36 +++++++++++++++++++++++++++++++++++-
Harald Hoyer 5f0dd7
 1 file changed, 35 insertions(+), 1 deletion(-)
Harald Hoyer 5f0dd7
Harald Hoyer 5f0dd7
diff --git a/dracut.sh b/dracut.sh
Harald Hoyer 5f0dd7
index af346f3a..c14f6c0b 100755
Harald Hoyer 5f0dd7
--- a/dracut.sh
Harald Hoyer 5f0dd7
+++ b/dracut.sh
Harald Hoyer 5f0dd7
@@ -2075,6 +2075,40 @@ fi
Harald Hoyer 5f0dd7
 
Harald Hoyer 5f0dd7
 command -v restorecon &>/dev/null && restorecon -- "$outfile"
Harald Hoyer 5f0dd7
 
Harald Hoyer 5f0dd7
+btrfs_uuid() {
Harald Hoyer 5f0dd7
+    btrfs filesystem show "$1" | sed -n '1s/^.*uuid: //p'
Harald Hoyer 5f0dd7
+}
Harald Hoyer 5f0dd7
+
Harald Hoyer 5f0dd7
+freeze_ok_for_btrfs() {
Harald Hoyer 5f0dd7
+    local mnt uuid1 uuid2
Harald Hoyer 5f0dd7
+    # If the output file is on btrfs, we need to make sure that it's
Harald Hoyer 5f0dd7
+    # not on a subvolume of the same file system as the root FS.
Harald Hoyer 5f0dd7
+    # Otherwise, fsfreeze() might freeze the entire system.
Harald Hoyer 5f0dd7
+    # This is most conveniently checked by comparing the FS uuid.
Harald Hoyer 5f0dd7
+
Harald Hoyer 5f0dd7
+    [[ "$(stat -f -c %T -- "/")" == "btrfs" ]] || return 0
Harald Hoyer 5f0dd7
+    mnt=$(stat -c %m -- "$1")
Harald Hoyer 5f0dd7
+    uuid1=$(btrfs_uuid "$mnt")
Harald Hoyer 5f0dd7
+    uuid2=$(btrfs_uuid "/")
Harald Hoyer 5f0dd7
+    [[ "$uuid1" && "$uuid2" && "$uuid1" != "$uuid2" ]]
Harald Hoyer 5f0dd7
+}
Harald Hoyer 5f0dd7
+
Harald Hoyer 5f0dd7
+freeze_ok_for_fstype() {
Harald Hoyer 5f0dd7
+    local outfile=$1
Harald Hoyer 5f0dd7
+    local fstype
Harald Hoyer 5f0dd7
+
Harald Hoyer 5f0dd7
+    [[ "$(stat -c %m -- "$outfile")" == "/" ]] && return 1
Harald Hoyer 5f0dd7
+    fstype=$(stat -f -c %T -- "$outfile")
Harald Hoyer 5f0dd7
+    case $fstype in
Harald Hoyer 5f0dd7
+        msdos)
Harald Hoyer 5f0dd7
+            return 1;;
Harald Hoyer 5f0dd7
+        btrfs)
Harald Hoyer 5f0dd7
+            freeze_ok_for_btrfs "$outfile";;
Harald Hoyer 5f0dd7
+        *)
Harald Hoyer 5f0dd7
+            return 0;;
Harald Hoyer 5f0dd7
+    esac
Harald Hoyer 5f0dd7
+}
Harald Hoyer 5f0dd7
+
Harald Hoyer 5f0dd7
 # We sync/fsfreeze only if we're operating on a live booted system.
Harald Hoyer 5f0dd7
 # It's possible for e.g. `kernel` to be installed as an RPM BuildRequires or equivalent,
Harald Hoyer 5f0dd7
 # and there's no reason to sync, and *definitely* no reason to fsfreeze.
Harald Hoyer 5f0dd7
@@ -2087,7 +2121,7 @@ if test -d $dracutsysrootdir/run/systemd/system; then
Harald Hoyer 5f0dd7
     fi
Harald Hoyer 5f0dd7
 
Harald Hoyer 5f0dd7
     # use fsfreeze only if we're not writing to /
Harald Hoyer 5f0dd7
-    if [[ "$(stat -c %m -- "$outfile")" != "/" && "$(stat -f -c %T -- "$outfile")" != "msdos" ]]; then
Harald Hoyer 5f0dd7
+    if [[ "$(stat -c %m -- "$outfile")" != "/" ]] && freeze_ok_for_fstype "$outfile"; then
Harald Hoyer 5f0dd7
         if ! $(fsfreeze -f $(dirname "$outfile") 2>/dev/null && fsfreeze -u $(dirname "$outfile") 2>/dev/null); then
Harald Hoyer 5f0dd7
             dinfo "dracut: warning: could not fsfreeze $(dirname "$outfile")"
Harald Hoyer 5f0dd7
         fi
Harald Hoyer 5f0dd7