summaryrefslogtreecommitdiff
path: root/src/with-btrfs-seed
diff options
context:
space:
mode:
Diffstat (limited to 'src/with-btrfs-seed')
-rwxr-xr-xsrc/with-btrfs-seed63
1 files changed, 63 insertions, 0 deletions
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 @@
1#!/bin/sh
2
3usage()
4{
5 print >&2 <<EOF
6usage: $0 <file.iso> [command] [args ...]
7
8 default command is bash
9EOF
10 exit $1
11}
12
13error()
14{
15 printf '%s: Error: %s\n' "$0" "$*" >&2
16 exit 1
17}
18
19warning()
20{
21 printf '%s: Warning: %s\n' "$0" "$*" >&2
22}
23
24have_root()
25{
26 [ "$(id -u)" = 0 ]
27}
28
29main()
30{
31 have_root || error "you are not root"
32 [ $# = 0 ] && usage 0
33
34 iso_file=$1
35 shift
36
37 iso_file_clone=${iso_file}.clone.tmp
38 mountpoint=${iso_file%.iso}.mnt.tmp
39
40 [ -f "$iso_file" ] || error "not a file: $iso_file"
41 [ ! -f "$iso_file_clone" ] || error "refusing to clobber existing file: $iso_file_clone"
42 [ -d "$mountpoint" ] || mkdir "$mountpoint" || error "could not create directory $mountpoint"
43 ! mountpoint -q "$mountpoint" || error "directory is already a mountpoint: $mountpoint"
44
45 cp --reflink=always "$iso_file" "$iso_file_clone" || error "cp failed"
46 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
50 chroot "$mountpoint" "$@"
51 result=$?
52 umount "$mountpoint" || warning "umount $mountpoint failed! You had better do something about it"
53 rmdir "$mountpoint"
54 if [ "$result" = 0 ]; then
55 btrfstune -S 1 "$iso_file_clone" || error "btrfstune failed. The original ISO is unmodified."
56 mv "$iso_file_clone" "$iso_file"
57 else
58 warning "The command executed within the chroot failed. The original ISO is unmodified."
59 rm "$iso_file_clone"
60 fi
61}
62
63main "$@"