summaryrefslogtreecommitdiff
path: root/other/shrink2fs_partition
diff options
context:
space:
mode:
authorroot <root@vps-18a7a2b7.vps.ovh.ca>2023-04-13 18:49:04 -0400
committerroot <root@vps-18a7a2b7.vps.ovh.ca>2023-04-13 18:49:04 -0400
commitad0f80e4850bf0ba25db234e85b4d37c2c713150 (patch)
tree2942d37c704f3c054e0f797d654cb78df36f14d2 /other/shrink2fs_partition
parentd29e7f661ae2c8f761035e691b9b6d9f535b9e9d (diff)
other hosting stuff
Diffstat (limited to 'other/shrink2fs_partition')
-rwxr-xr-xother/shrink2fs_partition78
1 files changed, 78 insertions, 0 deletions
diff --git a/other/shrink2fs_partition b/other/shrink2fs_partition
new file mode 100755
index 0000000..2674158
--- /dev/null
+++ b/other/shrink2fs_partition
@@ -0,0 +1,78 @@
1#!/bin/sh
2DEPENDENCIES='tune2fs parted lsblk blkid'
3READ_ONLY=y
4
5die()
6{
7 echo "Error: $*" >&2
8 exit 1
9}
10
11parted()
12{
13 if [ "$READ_ONLY" ]
14 then
15 echo parted "$@"
16 else
17 (set -x; command parted "$@")
18 fi
19}
20
21check_deps()
22{
23 for dep in $DEPENDENCIES
24 do
25 command -v "$dep" >/dev/null || die "Missing dependency: $dep"
26 done
27}
28
29filesystem_size()
30{
31 [ "$1" ] || return
32 fstype=$(blkid -o value -s TYPE "$1") || die "blkid could not determine FS type"
33 case "$fstype" in
34 ext[234]) filesystem_size_e2fs "$1";;
35 *) die "Unsupported filesystem type: $FSTYPE";;
36 esac
37}
38
39filesystem_size_e2fs()
40{
41 tune2fs -l "$1" | {
42 unset block_count block_size
43 while read line
44 do
45 case "$line" in
46 'Block count:'*) block_count=${line#*:} ;;
47 'Block size:'*) block_size=${line#*:} ;;
48 esac
49 done
50 [ "$block_count" -a "$block_size" ] || return
51 echo $((block_count * block_size))
52 }
53}
54
55get_block_device_names() # Outputs: $PARTNAME $DEVNAME
56{
57 [ -b "$1" ] || die "Not a block device: $1"
58 PARTNAME=/dev/$(lsblk -no KNAME "$1") || die "lsblk failed to get kernel device name for $1"
59
60 [ -b "$PARTNAME" ] || die "Not a block device: $PARTNAME"
61 DEVNAME=/dev/$(lsblk -no PKNAME "$PARTNAME") || die "lsblk failed to get parent device name for $PARTNAME"
62
63 [ -b "$DEVNAME" ] || die "Not a block device: $DEVNAME"
64}
65
66shrink_partition()
67{
68 get_block_device_names "$1"
69 partnum=${PARTNAME#$DEVNAME}
70 [ "$PARTNAME" = "$DEVNAME$partnum" ] || die "Cannot compute partition number; (partname, devname) = ($PARTNAME, $DEVNAME)"
71
72 partsize=$(filesystem_size "$PARTNAME") || die "Failed to obtain fs size"
73 parted "$DEVNAME" resizepart "$partnum" $((partsize))B
74}
75
76check_deps
77
78shrink_partition "$1"