mirror of
https://github.com/lisanet/PiShrink-macOS.git
synced 2026-03-14 19:43:50 +00:00
rename to pishrink,since we move this in /usr/local/bin and chmod a+x, add a Makefile
This commit is contained in:
committed by
Simone Karin Lehmann
parent
ea26712efe
commit
35e91edc75
93
pishrink
Executable file
93
pishrink
Executable file
@@ -0,0 +1,93 @@
|
||||
#!/bin/bash
|
||||
|
||||
usage() { echo "Usage: $0 imagefile.img [newimagefile.img]"; exit -1; }
|
||||
|
||||
#Args
|
||||
img="$1"
|
||||
|
||||
#Usage checks
|
||||
if [[ -z "$img" ]]; then
|
||||
usage
|
||||
fi
|
||||
if [[ ! -f "$img" ]]; then
|
||||
echo "ERROR: $img is not a file..."
|
||||
exit -2
|
||||
fi
|
||||
|
||||
#Check that what we need is installed
|
||||
for command in tune2fs e2fsck resize2fs; do
|
||||
which $command 2>&1 >/dev/null
|
||||
if (( $? != 0 )); then
|
||||
echo "ERROR: $command is not installed."
|
||||
exit -4
|
||||
fi
|
||||
done
|
||||
|
||||
#Copy to new file if requested
|
||||
if [ -n "$2" ]; then
|
||||
echo "Copying $1 to $2..."
|
||||
cp "$1" "$2"
|
||||
if (( $? != 0 )); then
|
||||
echo "ERROR: Could not copy file..."
|
||||
exit -5
|
||||
fi
|
||||
img="$2"
|
||||
fi
|
||||
|
||||
#Gather info
|
||||
beforesize=$(ls -lh "$img" | tr -s " " | cut -d " " -f 5)
|
||||
partnum=$(fdisk "$img" | grep Linux | cut -d ":" -f1 | tr -d " ")
|
||||
partstart=$(fdisk -d "$img" | grep 0x83 | cut -d "," -f1)
|
||||
loopback=$(hdiutil attach -nomount "$img" | grep Linux | cut -d " " -f1)
|
||||
tune2fs_output=$(tune2fs -l "$loopback")
|
||||
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)
|
||||
|
||||
#Make sure filesystem is ok, force yes on all questions
|
||||
e2fsck -y -f "$loopback"
|
||||
minsize=$(resize2fs -P "$loopback" | cut -d ':' -f 2 | tr -d ' ')
|
||||
|
||||
# add ~200 MB of extra space, the system may need it to run
|
||||
minsize=$(($minsize + 50000))
|
||||
|
||||
if [[ $minsize -gt $currentsize ]]; then
|
||||
echo "ERROR: Image already shrunk to smallest size"
|
||||
exit -6
|
||||
fi
|
||||
|
||||
#Shrink filesystem
|
||||
resize2fs -p "$loopback" $minsize
|
||||
if [[ $? != 0 ]]; then
|
||||
echo "ERROR: resize2fs failed..."
|
||||
hdiutil detach $loopback
|
||||
exit -7
|
||||
fi
|
||||
sleep 1
|
||||
|
||||
#Shrink partition
|
||||
hdiutil detach $loopback
|
||||
# fdisk uses a block size of 512 Bytes!
|
||||
partnewsize=$(($minsize * $blocksize / 512))
|
||||
newpartend=$(($partstart + $partnewsize))
|
||||
|
||||
# now use fdisk to change the partition
|
||||
fdisk -e "$img" <<EOF2
|
||||
e $partnum
|
||||
|
||||
|
||||
|
||||
$newpartend
|
||||
w
|
||||
q
|
||||
|
||||
EOF2
|
||||
sleep 1
|
||||
|
||||
#Truncate the file
|
||||
endresult=$(($partnewsize + 1))
|
||||
dd if="$img" of="$img-shrinked.img" bs=512 count=$endresult
|
||||
aftersize=$(ls -lh "$img-shrinked.img" | tr -s " " | cut -d " " -f 5)
|
||||
rm -f "$img"
|
||||
mv "$img-shrinked.img" "$img"
|
||||
|
||||
echo "Shrunk $img from $beforesize to $aftersize"
|
||||
Reference in New Issue
Block a user