diff options
author | Andrew Cady <d@jerkface.net> | 2017-03-29 21:35:31 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2017-03-29 21:35:31 -0400 |
commit | 0ec9e1b2b1f4c08d8588f9cfe01712c34a6eb8ab (patch) | |
tree | c199774e6c9885f6573f4a60c210971531c0a95f /src | |
parent | 6ab3dccf0c6dd96cbdd6031271ee7937899b2a80 (diff) |
add more options to with-btrfs-seed
Diffstat (limited to 'src')
-rwxr-xr-x | src/with-btrfs-seed | 59 |
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 | ||
3 | usage() | 3 | usage() |
4 | { | 4 | { |
5 | print >&2 <<EOF | 5 | cat >&2 <<EOF |
6 | usage: $0 <file.iso> [command] [args ...] | 6 | usage: $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 | ||
9 | EOF | 12 | EOF |
10 | exit $1 | 13 | exit $1 |
11 | } | 14 | } |
@@ -29,7 +32,6 @@ have_root() | |||
29 | main() | 32 | main() |
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 | |||
81 | CHROOT=y | ||
82 | eval set -- "$(getopt -o '' --long 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 | *) error "unrecognized option: $1" | ||
91 | esac | ||
92 | done | ||
93 | |||
94 | : ${BTRFS_OPTIONS:=compress} | ||
95 | |||
96 | [ "$FORK" ] && error "unimplemented option: --fork" | ||
97 | |||
63 | main "$@" | 98 | main "$@" |