diff options
Diffstat (limited to 'src/btrfs-utils/with-btrfs-seed')
-rwxr-xr-x | src/btrfs-utils/with-btrfs-seed | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/btrfs-utils/with-btrfs-seed b/src/btrfs-utils/with-btrfs-seed new file mode 100755 index 0000000..637af5a --- /dev/null +++ b/src/btrfs-utils/with-btrfs-seed | |||
@@ -0,0 +1,99 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | usage() | ||
4 | { | ||
5 | cat >&2 <<EOF | ||
6 | usage: $0 [options] <file.iso> [command] [args ...] | ||
7 | |||
8 | options: | ||
9 | --resize <size> | ||
10 | --btrfs-options <mount options> (default: compress) | ||
11 | --no-chroot | ||
12 | EOF | ||
13 | exit $1 | ||
14 | } | ||
15 | |||
16 | error() | ||
17 | { | ||
18 | printf '%s: Error: %s\n' "$0" "$*" >&2 | ||
19 | exit 1 | ||
20 | } | ||
21 | |||
22 | warning() | ||
23 | { | ||
24 | printf '%s: Warning: %s\n' "$0" "$*" >&2 | ||
25 | } | ||
26 | |||
27 | have_root() | ||
28 | { | ||
29 | [ "$(id -u)" = 0 ] | ||
30 | } | ||
31 | |||
32 | main() | ||
33 | { | ||
34 | have_root || error "you are not root" | ||
35 | |||
36 | iso_file=$1 | ||
37 | shift | ||
38 | |||
39 | iso_file_clone=${iso_file}.clone.tmp | ||
40 | mountpoint=${iso_file%.iso}.mnt.tmp | ||
41 | |||
42 | [ -f "$iso_file" ] || error "not a file: $iso_file" | ||
43 | [ ! -f "$iso_file_clone" ] || error "refusing to clobber existing file: $iso_file_clone" | ||
44 | [ -d "$mountpoint" ] || mkdir "$mountpoint" || error "could not create directory $mountpoint" | ||
45 | ! mountpoint -q "$mountpoint" || error "directory is already a mountpoint: $mountpoint" | ||
46 | |||
47 | cp --reflink=always "$iso_file" "$iso_file_clone" || error "cp failed" | ||
48 | trap 'rm -f "$iso_file_clone"' EXIT | ||
49 | |||
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 | ||
67 | result=$? | ||
68 | umount "$mountpoint" || warning "umount $mountpoint failed! You had better do something about it" | ||
69 | rmdir "$mountpoint" | ||
70 | if [ "$result" = 0 ]; then | ||
71 | btrfstune -S 1 "$iso_file_clone" || error "btrfstune failed. The original ISO is unmodified." | ||
72 | mv "$iso_file_clone" "$iso_file" | ||
73 | else | ||
74 | warning "The command executed within the chroot failed. The original ISO is unmodified." | ||
75 | rm "$iso_file_clone" | ||
76 | fi | ||
77 | } | ||
78 | |||
79 | [ $# = 0 ] && usage 0 | ||
80 | |||
81 | CHROOT=y | ||
82 | eval set -- "$(getopt -o '' --long shrink,no-chroot,resize:,fork:,btrfs-options: -n "$0" -- "$@")" || error 'getopt error' | ||
83 | while [ $# -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 | --shrink) SHRINK=y; shift;; | ||
91 | *) error "unrecognized option: $1" | ||
92 | esac | ||
93 | done | ||
94 | |||
95 | : ${BTRFS_OPTIONS:=compress} | ||
96 | |||
97 | [ "$FORK" ] && error "unimplemented option: --fork" | ||
98 | |||
99 | main "$@" | ||