summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2017-03-29 11:14:47 -0400
committerAndrew Cady <d@jerkface.net>2017-03-29 11:14:47 -0400
commit6ab3dccf0c6dd96cbdd6031271ee7937899b2a80 (patch)
tree02f23b44bf6bb8ff313cb002b7e62cb5cdbf18f9 /src
parent66bac917e1f17fbb15ad96b4ee68037d94608ce7 (diff)
new util: with-btrfs-seed
Diffstat (limited to 'src')
-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 "$@"