summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/with-btrfs-seed59
1 files changed, 47 insertions, 12 deletions
diff --git a/src/with-btrfs-seed b/src/with-btrfs-seed
index 9f0147e..f78417b 100755
--- a/src/with-btrfs-seed
+++ b/src/with-btrfs-seed
@@ -2,10 +2,13 @@
2 2
3usage() 3usage()
4{ 4{
5 print >&2 <<EOF 5 cat >&2 <<EOF
6usage: $0 <file.iso> [command] [args ...] 6usage: $0 [options] <file.iso> [command] [args ...]
7 7
8 default command is bash 8 options:
9 --resize <size>
10 --btrfs-options <mount options> (default: compress)
11 --no-chroot
9EOF 12EOF
10 exit $1 13 exit $1
11} 14}
@@ -29,7 +32,6 @@ have_root()
29main() 32main()
30{ 33{
31 have_root || error "you are not root" 34 have_root || error "you are not root"
32 [ $# = 0 ] && usage 0
33 35
34 iso_file=$1 36 iso_file=$1
35 shift 37 shift
@@ -37,17 +39,31 @@ main()
37 iso_file_clone=${iso_file}.clone.tmp 39 iso_file_clone=${iso_file}.clone.tmp
38 mountpoint=${iso_file%.iso}.mnt.tmp 40 mountpoint=${iso_file%.iso}.mnt.tmp
39 41
40 [ -f "$iso_file" ] || error "not a file: $iso_file" 42 [ -f "$iso_file" ] || error "not a file: $iso_file"
41 [ ! -f "$iso_file_clone" ] || error "refusing to clobber existing file: $iso_file_clone" 43 [ ! -f "$iso_file_clone" ] || error "refusing to clobber existing file: $iso_file_clone"
42 [ -d "$mountpoint" ] || mkdir "$mountpoint" || error "could not create directory $mountpoint" 44 [ -d "$mountpoint" ] || mkdir "$mountpoint" || error "could not create directory $mountpoint"
43 ! mountpoint -q "$mountpoint" || error "directory is already a mountpoint: $mountpoint" 45 ! mountpoint -q "$mountpoint" || error "directory is already a mountpoint: $mountpoint"
44 46
45 cp --reflink=always "$iso_file" "$iso_file_clone" || error "cp failed" 47 cp --reflink=always "$iso_file" "$iso_file_clone" || error "cp failed"
46 trap 'rm -f "$iso_file_clone"' EXIT 48 trap 'rm -f "$iso_file_clone"' EXIT
47 btrfstune -f -S 0 "$iso_file_clone" 2>/dev/null || error "btrfstune failed"
48 mount -o rw,loop "$iso_file_clone" "$mountpoint" || error "failed to mount $iso_file"
49 49
50 chroot "$mountpoint" "$@" 50 if [ "$RESIZE" ]; then
51 truncate --size="$RESIZE" "$iso_file_clone" || error "failed to resize image with truncate"
52 fi
53
54 btrfstune -f -S 0 "$iso_file_clone" 2>/dev/null || error "btrfstune failed"
55 mount_options=rw,loop${BTRFS_OPTIONS:+,$BTRFS_OPTIONS}
56 mount -o "$mount_options" "$iso_file_clone" "$mountpoint" || error "failed to mount $iso_file"
57
58 if [ "$RESIZE" ]; then
59 btrfs filesystem resize max "$mountpoint" || error "btrfs filesystem resize"
60 fi
61
62 if [ "$CHROOT" ]; then
63 chroot "$mountpoint" "$@"
64 else
65 "$@" "$mountpoint"
66 fi
51 result=$? 67 result=$?
52 umount "$mountpoint" || warning "umount $mountpoint failed! You had better do something about it" 68 umount "$mountpoint" || warning "umount $mountpoint failed! You had better do something about it"
53 rmdir "$mountpoint" 69 rmdir "$mountpoint"
@@ -60,4 +76,23 @@ main()
60 fi 76 fi
61} 77}
62 78
79[ $# = 0 ] && usage 0
80
81CHROOT=y
82eval set -- "$(getopt -o '' --long no-chroot,resize:,fork:,btrfs-options: -n "$0" -- "$@")" || error 'getopt error'
83while [ $# -gt 0 ]; do
84 case "$1" in
85 --) shift; break;;
86 --resize) RESIZE=$2; shift 2;;
87 --fork) FORK=$2; shift 2;;
88 --btrfs-options) BTRFS_OPTIONS=$2; shift 2;;
89 --no-chroot) CHROOT=; shift;;
90 *) error "unrecognized option: $1"
91 esac
92done
93
94: ${BTRFS_OPTIONS:=compress}
95
96[ "$FORK" ] && error "unimplemented option: --fork"
97
63main "$@" 98main "$@"