From 6ab3dccf0c6dd96cbdd6031271ee7937899b2a80 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Wed, 29 Mar 2017 11:14:47 -0400 Subject: new util: with-btrfs-seed --- src/with-btrfs-seed | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 src/with-btrfs-seed (limited to 'src') diff --git a/src/with-btrfs-seed b/src/with-btrfs-seed new file mode 100755 index 0000000..9f0147e --- /dev/null +++ b/src/with-btrfs-seed @@ -0,0 +1,63 @@ +#!/bin/sh + +usage() +{ + print >&2 < [command] [args ...] + + default command is bash +EOF + exit $1 +} + +error() +{ + printf '%s: Error: %s\n' "$0" "$*" >&2 + exit 1 +} + +warning() +{ + printf '%s: Warning: %s\n' "$0" "$*" >&2 +} + +have_root() +{ + [ "$(id -u)" = 0 ] +} + +main() +{ + have_root || error "you are not root" + [ $# = 0 ] && usage 0 + + iso_file=$1 + shift + + iso_file_clone=${iso_file}.clone.tmp + mountpoint=${iso_file%.iso}.mnt.tmp + + [ -f "$iso_file" ] || error "not a file: $iso_file" + [ ! -f "$iso_file_clone" ] || error "refusing to clobber existing file: $iso_file_clone" + [ -d "$mountpoint" ] || mkdir "$mountpoint" || error "could not create directory $mountpoint" + ! mountpoint -q "$mountpoint" || error "directory is already a mountpoint: $mountpoint" + + cp --reflink=always "$iso_file" "$iso_file_clone" || error "cp failed" + trap 'rm -f "$iso_file_clone"' EXIT + btrfstune -f -S 0 "$iso_file_clone" 2>/dev/null || error "btrfstune failed" + mount -o rw,loop "$iso_file_clone" "$mountpoint" || error "failed to mount $iso_file" + + chroot "$mountpoint" "$@" + result=$? + umount "$mountpoint" || warning "umount $mountpoint failed! You had better do something about it" + rmdir "$mountpoint" + if [ "$result" = 0 ]; then + btrfstune -S 1 "$iso_file_clone" || error "btrfstune failed. The original ISO is unmodified." + mv "$iso_file_clone" "$iso_file" + else + warning "The command executed within the chroot failed. The original ISO is unmodified." + rm "$iso_file_clone" + fi +} + +main "$@" -- cgit v1.2.3