Add GPT fix and correct compress option
This commit is contained in:
parent
8b26638bc7
commit
2db3c59884
25
pishrink.sh
25
pishrink.sh
|
@ -11,7 +11,7 @@ usage() { echo "Usage: $0 [-s] [-z] imagefile.img [newimagefile.img]"; exit -1;
|
||||||
should_skip_autoexpand=false
|
should_skip_autoexpand=false
|
||||||
compress_output=false
|
compress_output=false
|
||||||
|
|
||||||
while getopts ":s:z" opt; do
|
while getopts ":sz" opt; do
|
||||||
case "${opt}" in
|
case "${opt}" in
|
||||||
s) should_skip_autoexpand=true ;;
|
s) should_skip_autoexpand=true ;;
|
||||||
z) compress_output=true ;;
|
z) compress_output=true ;;
|
||||||
|
@ -37,7 +37,7 @@ if (( EUID != 0 )); then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#Check that what we need is installed
|
#Check that what we need is installed
|
||||||
for command in parted losetup tune2fs md5sum e2fsck resize2fs tar; do
|
for command in fdisk gdisk parted losetup tune2fs md5sum e2fsck resize2fs tar; do
|
||||||
which $command 2>&1 >/dev/null
|
which $command 2>&1 >/dev/null
|
||||||
if (( $? != 0 )); then
|
if (( $? != 0 )); then
|
||||||
echo "ERROR: $command is not installed."
|
echo "ERROR: $command is not installed."
|
||||||
|
@ -71,6 +71,11 @@ tune2fs_output=$(tune2fs -l "$loopback")
|
||||||
currentsize=$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -d ':' -f 2)
|
currentsize=$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -d ':' -f 2)
|
||||||
blocksize=$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2)
|
blocksize=$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2)
|
||||||
|
|
||||||
|
isGPT=false
|
||||||
|
if fdisk -l $img | grep -q "gpt"; then
|
||||||
|
isGPT=true
|
||||||
|
fi
|
||||||
|
|
||||||
#Check if we should make pi expand rootfs on next boot
|
#Check if we should make pi expand rootfs on next boot
|
||||||
if [ "$should_skip_autoexpand" = false ]; then
|
if [ "$should_skip_autoexpand" = false ]; then
|
||||||
#Make pi expand rootfs on next boot
|
#Make pi expand rootfs on next boot
|
||||||
|
@ -179,16 +184,26 @@ sleep 1
|
||||||
#Shrink partition
|
#Shrink partition
|
||||||
partnewsize=$(($minsize * $blocksize))
|
partnewsize=$(($minsize * $blocksize))
|
||||||
newpartend=$(($partstart + $partnewsize))
|
newpartend=$(($partstart + $partnewsize))
|
||||||
parted -s -a minimal "$img" rm $partnum >/dev/null
|
parted -s -a minimal "$img" rm $partnum
|
||||||
parted -s "$img" unit B mkpart primary $partstart $newpartend >/dev/null
|
parted -s "$img" unit B mkpart primary $partstart $newpartend
|
||||||
|
|
||||||
#Truncate the file
|
#Truncate the file
|
||||||
endresult=$(parted -ms "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B')
|
endresult=$(parted -ms "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B')
|
||||||
|
if [ "$isGPT" = true ]; then
|
||||||
|
# Have to add an extra 34 Sectors for GPT type
|
||||||
|
endresult=$(($endresult + 34*512));
|
||||||
|
fi
|
||||||
truncate -s $endresult "$img"
|
truncate -s $endresult "$img"
|
||||||
aftersize=$(ls -lh "$img" | cut -d ' ' -f 5)
|
aftersize=$(ls -lh "$img" | cut -d ' ' -f 5)
|
||||||
|
|
||||||
echo "Shrunk $img from $beforesize to $aftersize"
|
echo "Shrunk $img from $beforesize to $aftersize"
|
||||||
|
|
||||||
|
if [ "$isGPT" = true ]; then
|
||||||
|
# Go fix the GPT Partition Table
|
||||||
|
echo "This is a GPT image, you must fix the backup partition table."
|
||||||
|
echo "gdisk will start, press 'w', then 'y' to fix the backup table."
|
||||||
|
gdisk "$img"
|
||||||
|
fi
|
||||||
|
|
||||||
#Compress the new image
|
#Compress the new image
|
||||||
if [ "$compress_output" = true ]; then
|
if [ "$compress_output" = true ]; then
|
||||||
img_dir=$(dirname "$img");
|
img_dir=$(dirname "$img");
|
||||||
|
|
Loading…
Reference in New Issue