forked from marcone/teslausb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize-image.sh
executable file
·97 lines (81 loc) · 2.12 KB
/
resize-image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash -eu
function usage {
echo "usage: $0 <size> <image>"
}
function dehumanize () {
echo $(($(echo "$1" | sed 's/GB/G/;s/MB/M/;s/KB/K/;s/G/*1024M/;s/M/*1024K/;s/K/*1024/')))
}
function closeenough () {
DIFF=$(($1-$2))
if [ $DIFF -ge 0 ] && [ $DIFF -lt 1048576 ]
then
true
return
elif [ $DIFF -lt 0 ] && [ $DIFF -gt -1048576 ]
then
true
return
fi
false
}
if [[ $# -ne 2 ]]
then
usage
exit
fi
NEWSIZE=$(dehumanize "$1")
FILE=$2
if [ ! -e "$FILE" ]
then
echo "No such file: $FILE"
usage
exit
fi
# install fatresize if needed
if ! hash fatresize &> /dev/null
then
/root/bin/remountfs_rw
apt install -y fatresize
fi
if findmnt /mnt/cam > /dev/null
then
echo "cam drive is mounted. Please ensure no archiving operation is in progress"
exit
fi
if findmnt /mnt/music > /dev/null
then
echo "music drive is mounted. Please ensure no music sync operation is in progress"
exit
fi
# remove device from any attached host
/root/bin/disable_gadget.sh
# fsck the image, since we may have just yanked it out from under the host.
# Use -p repair arg. It works with vfat and exfat.
DEVLOOP=$(losetup --show -P -f "$FILE")
PARTLOOP=${DEVLOOP}p1
fsck "$PARTLOOP" -- -p > /dev/null || true
# get size of the image file and the partition within
CURRENT_PARTITION_SIZE=$(($(partx -o SECTORS -g -n 1 "$FILE") * 512 + 512))
PARTITION_OFFSET=$(($(partx -o START -g -n 1 "$FILE") * 512))
# fatresize doesn't seem to like extending partitions to the very end of the file
# and sometimes segfault in that case, so add some padding
PARTITION_PADDING=65536
if closeenough $CURRENT_PARTITION_SIZE "$NEWSIZE"
then
echo "no sizing needed"
elif [ $CURRENT_PARTITION_SIZE -lt "$NEWSIZE" ]
then
echo "growing"
fallocate -l $((PARTITION_OFFSET + NEWSIZE + PARTITION_PADDING)) "$FILE"
fatresize -s "$NEWSIZE" "$FILE" > /dev/null
else
echo "shrinking"
if fatresize -s "$NEWSIZE" "$FILE" > /dev/null
then
PARTITION_END=$(($(partx -o END -g -n 1 "$FILE") * 512 + 512))
truncate -s $((PARTITION_END + PARTITION_PADDING)) "$FILE"
else
echo "resize failed, skipping image resizing"
fi
fi
losetup -d "$DEVLOOP"