diff options
author | Andrew Cady <d@jerkface.net> | 2017-03-29 22:50:05 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2017-03-29 22:50:30 -0400 |
commit | b30a2be0105bae41479e518711364a4ae21fbcbd (patch) | |
tree | 857c96f118541671d2ad18572bc54491818e53f6 | |
parent | 0ec9e1b2b1f4c08d8588f9cfe01712c34a6eb8ab (diff) |
split btrfs-shrink out of btarfs
-rw-r--r-- | Makefile | 4 | ||||
-rwxr-xr-x | src/btrfs-utils/btarfs (renamed from src/btarfs) | 0 | ||||
-rwxr-xr-x | src/btrfs-utils/btrfs-shrink | 52 | ||||
-rwxr-xr-x | src/btrfs-utils/with-btrfs-seed (renamed from src/with-btrfs-seed) | 3 |
4 files changed, 57 insertions, 2 deletions
@@ -17,7 +17,9 @@ samizdat-pinentry: src/samizdat-pinentry.c | |||
17 | wait_for_files: src/wait_for_files.c | 17 | wait_for_files: src/wait_for_files.c |
18 | $(CC) $(CFLAGS) $< -o $@ | 18 | $(CC) $(CFLAGS) $< -o $@ |
19 | 19 | ||
20 | bin_programs=$(addprefix src/, xorriso-usb.sh btrfs-functions.sh btrfs-receive-root.sh btrfs-send-root.sh var.sh grub-efi.sh keygen.sh initrd.sh qemu.sh btarfs dnsmasq-dhcp-script.sh samizdat-password-agent samizdat-gpg-agent publish-ip.sh) samizdat-paths.sh ${cc_files} | 20 | bin_programs=$(addprefix src/, xorriso-usb.sh btrfs-functions.sh btrfs-receive-root.sh btrfs-send-root.sh var.sh grub-efi.sh keygen.sh initrd.sh qemu.sh dnsmasq-dhcp-script.sh samizdat-password-agent samizdat-gpg-agent publish-ip.sh) samizdat-paths.sh ${cc_files} ${btrfs_utils} |
21 | |||
22 | btrfs_utils=$(addprefix src/btrfs-utils/, btarfs btrfs-shrink with-btrfs-seed) | ||
21 | 23 | ||
22 | initrd_files:=$(wildcard src/initrd/*) dynmenu samizdat-pinentry wait_for_files | 24 | initrd_files:=$(wildcard src/initrd/*) dynmenu samizdat-pinentry wait_for_files |
23 | initramfs_conf_files:=$(wildcard initramfs-tools/*) | 25 | initramfs_conf_files:=$(wildcard initramfs-tools/*) |
diff --git a/src/btarfs b/src/btrfs-utils/btarfs index a51b2ef..a51b2ef 100755 --- a/src/btarfs +++ b/src/btrfs-utils/btarfs | |||
diff --git a/src/btrfs-utils/btrfs-shrink b/src/btrfs-utils/btrfs-shrink new file mode 100755 index 0000000..f3dbbb0 --- /dev/null +++ b/src/btrfs-utils/btrfs-shrink | |||
@@ -0,0 +1,52 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; } | ||
4 | |||
5 | [ "$(id -u)" = 0 ] || die 'you are not root' | ||
6 | |||
7 | shrink() | ||
8 | { | ||
9 | shrinkmegs=100 | ||
10 | while true; do | ||
11 | while ! btrfs filesystem resize -${shrinkmegs}M "$mountpoint"/; do | ||
12 | shrinkmegs=$((shrinkmegs - 10 )) | ||
13 | if [ $shrinkmegs -lt 10 ]; then | ||
14 | return | ||
15 | fi | ||
16 | done | ||
17 | done | ||
18 | } | ||
19 | |||
20 | btrfs_truncate() | ||
21 | { | ||
22 | local img="$1" bytes | ||
23 | bytes=$(file "$img" | sed -ne 's?.*/\([0-9]*\)/[0-9]* bytes used.*?\1?p') | ||
24 | # 548044800/1176715264 bytes used | ||
25 | if [ "$bytes" ]; then | ||
26 | truncate -s "$bytes" "$img" | ||
27 | fi | ||
28 | } | ||
29 | |||
30 | main() | ||
31 | { | ||
32 | if [ -d "$1" ]; then | ||
33 | mountpoint -q "$1" || die "not a mountpoint: $1" | ||
34 | shrink "$1" | ||
35 | elif [ -f "$1" ]; then | ||
36 | mkdir "$1".mnt.tmp | ||
37 | mount -t btrfs "$1" "$1".mnt.tmp | ||
38 | result=$? | ||
39 | if [ $result = 0 ]; then | ||
40 | shrink "$1".mnt.tmp | ||
41 | result=$? | ||
42 | fi | ||
43 | umount "$1".mnt.tmp | ||
44 | rmdir "$1".mnt.tmp | ||
45 | btrfs_truncate "$1" | ||
46 | return $result | ||
47 | else | ||
48 | die "not a file or directory: $1" | ||
49 | fi | ||
50 | } | ||
51 | |||
52 | main "$@" | ||
diff --git a/src/with-btrfs-seed b/src/btrfs-utils/with-btrfs-seed index f78417b..637af5a 100755 --- a/src/with-btrfs-seed +++ b/src/btrfs-utils/with-btrfs-seed | |||
@@ -79,7 +79,7 @@ main() | |||
79 | [ $# = 0 ] && usage 0 | 79 | [ $# = 0 ] && usage 0 |
80 | 80 | ||
81 | CHROOT=y | 81 | CHROOT=y |
82 | eval set -- "$(getopt -o '' --long no-chroot,resize:,fork:,btrfs-options: -n "$0" -- "$@")" || error 'getopt error' | 82 | eval set -- "$(getopt -o '' --long shrink,no-chroot,resize:,fork:,btrfs-options: -n "$0" -- "$@")" || error 'getopt error' |
83 | while [ $# -gt 0 ]; do | 83 | while [ $# -gt 0 ]; do |
84 | case "$1" in | 84 | case "$1" in |
85 | --) shift; break;; | 85 | --) shift; break;; |
@@ -87,6 +87,7 @@ while [ $# -gt 0 ]; do | |||
87 | --fork) FORK=$2; shift 2;; | 87 | --fork) FORK=$2; shift 2;; |
88 | --btrfs-options) BTRFS_OPTIONS=$2; shift 2;; | 88 | --btrfs-options) BTRFS_OPTIONS=$2; shift 2;; |
89 | --no-chroot) CHROOT=; shift;; | 89 | --no-chroot) CHROOT=; shift;; |
90 | --shrink) SHRINK=y; shift;; | ||
90 | *) error "unrecognized option: $1" | 91 | *) error "unrecognized option: $1" |
91 | esac | 92 | esac |
92 | done | 93 | done |