summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2017-03-29 22:50:05 -0400
committerAndrew Cady <d@jerkface.net>2017-03-29 22:50:30 -0400
commitb30a2be0105bae41479e518711364a4ae21fbcbd (patch)
tree857c96f118541671d2ad18572bc54491818e53f6
parent0ec9e1b2b1f4c08d8588f9cfe01712c34a6eb8ab (diff)
split btrfs-shrink out of btarfs
-rw-r--r--Makefile4
-rwxr-xr-xsrc/btrfs-utils/btarfs (renamed from src/btarfs)0
-rwxr-xr-xsrc/btrfs-utils/btrfs-shrink52
-rwxr-xr-xsrc/btrfs-utils/with-btrfs-seed (renamed from src/with-btrfs-seed)3
4 files changed, 57 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 2389535..1ed07ed 100644
--- a/Makefile
+++ b/Makefile
@@ -17,7 +17,9 @@ samizdat-pinentry: src/samizdat-pinentry.c
17wait_for_files: src/wait_for_files.c 17wait_for_files: src/wait_for_files.c
18 $(CC) $(CFLAGS) $< -o $@ 18 $(CC) $(CFLAGS) $< -o $@
19 19
20bin_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} 20bin_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
22btrfs_utils=$(addprefix src/btrfs-utils/, btarfs btrfs-shrink with-btrfs-seed)
21 23
22initrd_files:=$(wildcard src/initrd/*) dynmenu samizdat-pinentry wait_for_files 24initrd_files:=$(wildcard src/initrd/*) dynmenu samizdat-pinentry wait_for_files
23initramfs_conf_files:=$(wildcard initramfs-tools/*) 25initramfs_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
3die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; }
4
5[ "$(id -u)" = 0 ] || die 'you are not root'
6
7shrink()
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
20btrfs_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
30main()
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
52main "$@"
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
81CHROOT=y 81CHROOT=y
82eval set -- "$(getopt -o '' --long no-chroot,resize:,fork:,btrfs-options: -n "$0" -- "$@")" || error 'getopt error' 82eval set -- "$(getopt -o '' --long shrink,no-chroot,resize:,fork:,btrfs-options: -n "$0" -- "$@")" || error 'getopt error'
83while [ $# -gt 0 ]; do 83while [ $# -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
92done 93done