From 87d9019314d90c13c357628e56ae59196eb16bd8 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Wed, 13 Jun 2018 17:00:19 -0400 Subject: making btrfs subvolumes should be idempotent --- src/initrd/btrfs-create.sh | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'src/initrd/btrfs-create.sh') diff --git a/src/initrd/btrfs-create.sh b/src/initrd/btrfs-create.sh index ff09bf7..567ad97 100644 --- a/src/initrd/btrfs-create.sh +++ b/src/initrd/btrfs-create.sh @@ -109,19 +109,34 @@ copy_execs() done } -initialize_root_filesystem() +is_subvolume() { - rm -r /root/root - btrfs subvolume create /root/root || return - mv /gpg/gnupghome /root/root/.gnupg || return + btrfs subvolume show "$1" >/dev/null 2>&1 +} + +make_subvolume_idem() +{ + is_subvolume "$1" && return + if [ -d "$1" ]; then + rmdir "$1" || mv "$1" "$1"~ + btrfs subvolume create "$1" + mv "$1"~/* "$1"~/.* "$1"/ 2>&1 + else + if [ -e "$1" ]; then + rm -f "$1" + fi + btrfs subvolume create "$1" + fi +} - rmdir /root/srv - btrfs subvolume create /root/srv - rm -r /root/var/cache/apt/archives - btrfs subvolume create /root/var/cache/apt/archives || return +initialize_root_filesystem() +{ + for d in /root/root /root/srv /root/var/cache/apt/archives /root/home; do + make_subvolume_idem "$d" + done - rmdir /root/home - btrfs subvolume create /root/home || return + mv /root/root/.gnupg /root/root/.gnupg~ + mv /gpg/gnupghome /root/root/.gnupg || return copy_execs sbin mdadm dmsetup cryptsetup fsck.hfsplus copy_execs bin btrfs rsync gpg gpg2 gpg-agent -- cgit v1.2.3