commit
8dccf7d154
92
pishrink.sh
92
pishrink.sh
|
@ -29,11 +29,13 @@ if (( EUID != 0 )); then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#Check that what we need is installed
|
#Check that what we need is installed
|
||||||
A=`which parted 2>&1`
|
for command in parted losetup tune2fs md5sum e2fsck resize2fs; do
|
||||||
if (( $? != 0 )); then
|
which $command 2>&1 >/dev/null
|
||||||
echo "ERROR: parted is not installed."
|
if (( $? != 0 )); then
|
||||||
exit -4
|
echo "ERROR: $command is not installed."
|
||||||
fi
|
exit -4
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
#Copy to new file if requested
|
#Copy to new file if requested
|
||||||
if [ -n "$2" ]; then
|
if [ -n "$2" ]; then
|
||||||
|
@ -43,28 +45,32 @@ if [ -n "$2" ]; then
|
||||||
echo "ERROR: Could not copy file..."
|
echo "ERROR: Could not copy file..."
|
||||||
exit -5
|
exit -5
|
||||||
fi
|
fi
|
||||||
|
old_owner=$(stat -c %u:%g "$1")
|
||||||
|
chown $old_owner "$2"
|
||||||
img="$2"
|
img="$2"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#Gather info
|
#Gather info
|
||||||
beforesize=`ls -lah "$img" | cut -d ' ' -f 5`
|
beforesize=$(ls -lh "$img" | cut -d ' ' -f 5)
|
||||||
partnum=`parted -m "$img" unit B print | tail -n 1 | cut -d ':' -f 1 | tr -d '\n'`
|
parted_output=$(parted -ms "$img" unit B print | tail -n 1)
|
||||||
partstart=`parted -m "$img" unit B print | tail -n 1 | cut -d ':' -f 2 | tr -d 'B\n'`
|
partnum=$(echo "$parted_output" | cut -d ':' -f 1)
|
||||||
loopback=`losetup -f --show -o $partstart "$img"`
|
partstart=$(echo "$parted_output" | cut -d ':' -f 2 | tr -d 'B')
|
||||||
currentsize=`tune2fs -l $loopback | grep 'Block count' | tr -d ' ' | cut -d ':' -f 2 | tr -d '\n'`
|
loopback=$(losetup -f --show -o $partstart "$img")
|
||||||
blocksize=`tune2fs -l $loopback | grep 'Block size' | tr -d ' ' | cut -d ':' -f 2 | tr -d '\n'`
|
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)
|
||||||
|
|
||||||
#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
|
||||||
mountdir=`mktemp -d`
|
mountdir=$(mktemp -d)
|
||||||
mount $loopback $mountdir
|
mount "$loopback" "$mountdir"
|
||||||
|
|
||||||
if [ `md5sum $mountdir/etc/rc.local | cut -d ' ' -f 1` != "0542054e9ff2d2e0507ea1ffe7d4fc87" ]; then
|
if [ $(md5sum "$mountdir/etc/rc.local" | cut -d ' ' -f 1) != "0542054e9ff2d2e0507ea1ffe7d4fc87" ]; then
|
||||||
echo Creating new /etc/rc.local
|
echo "Creating new /etc/rc.local"
|
||||||
mv $mountdir/etc/rc.local $mountdir/etc/rc.local.bak
|
mv "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak"
|
||||||
#####Do not touch the following lines#####
|
#####Do not touch the following lines#####
|
||||||
cat <<\EOF1 > $mountdir/etc/rc.local
|
cat <<\EOF1 > "$mountdir/etc/rc.local"
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
do_expand_rootfs() {
|
do_expand_rootfs() {
|
||||||
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
|
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
|
||||||
|
@ -123,52 +129,52 @@ rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local
|
||||||
exit 0
|
exit 0
|
||||||
EOF1
|
EOF1
|
||||||
#####End no touch zone#####
|
#####End no touch zone#####
|
||||||
chmod +x $mountdir/etc/rc.local
|
chmod +x "$mountdir/etc/rc.local"
|
||||||
fi
|
fi
|
||||||
umount $mountdir
|
umount "$mountdir"
|
||||||
else
|
else
|
||||||
echo Skipping autoexpanding process...
|
echo "Skipping autoexpanding process..."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#Make sure filesystem is ok
|
#Make sure filesystem is ok
|
||||||
e2fsck -p -f $loopback
|
e2fsck -p -f "$loopback"
|
||||||
minsize=`resize2fs -P $loopback | cut -d ':' -f 2 | tr -d ' ' | tr -d '\n'`
|
minsize=$(resize2fs -P "$loopback" | cut -d ':' -f 2 | tr -d ' ')
|
||||||
if [[ $currentsize -eq $minsize ]]; then
|
if [[ $currentsize -eq $minsize ]]; then
|
||||||
echo ERROR: Image already shrunk to smallest size
|
echo "ERROR: Image already shrunk to smallest size"
|
||||||
exit -6
|
exit -6
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#Add some free space to the end of the filesystem
|
#Add some free space to the end of the filesystem
|
||||||
if [[ `expr $currentsize - $minsize - 5000` -gt 0 ]]; then
|
extra_space=$(($currentsize - $minsize))
|
||||||
minsize=`expr $minsize + 5000 | tr -d '\n'`
|
for space in 5000 1000 100; do
|
||||||
elif [[ `expr $currentsize - $minsize - 1000` -gt 0 ]]; then
|
if [[ $extra_space -gt $space ]]; then
|
||||||
minsize=`expr $minsize + 1000 | tr -d '\n'`
|
minsize=$(($minsize + $space))
|
||||||
elif [[ `expr $currentsize - $minsize - 100` -gt 0 ]]; then
|
break
|
||||||
minsize=`expr $minsize + 100 | tr -d '\n'`
|
fi
|
||||||
fi
|
done
|
||||||
|
|
||||||
#Shrink filesystem
|
#Shrink filesystem
|
||||||
resize2fs -p $loopback $minsize
|
resize2fs -p "$loopback" $minsize
|
||||||
if [[ $? != 0 ]]; then
|
if [[ $? != 0 ]]; then
|
||||||
echo ERROR: resize2fs failed...
|
echo "ERROR: resize2fs failed..."
|
||||||
mount $loopback $mountdir
|
mount "$loopback" "$mountdir"
|
||||||
mv $mountdir/etc/rc.local.bak $mountdir/etc/rc.local
|
mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local"
|
||||||
umount $mountdir
|
umount "$mountdir"
|
||||||
losetup -d $loopback
|
losetup -d "$loopback"
|
||||||
exit -7
|
exit -7
|
||||||
fi
|
fi
|
||||||
sleep 1
|
sleep 1
|
||||||
|
|
||||||
#Shrink partition
|
#Shrink partition
|
||||||
losetup -d $loopback
|
losetup -d "$loopback"
|
||||||
partnewsize=`expr $minsize \* $blocksize | tr -d '\n'`
|
partnewsize=$(($minsize * $blocksize))
|
||||||
newpartend=`expr $partstart + $partnewsize | tr -d '\n'`
|
newpartend=$(($partstart + $partnewsize))
|
||||||
part1=`parted "$img" rm $partnum`
|
parted -s "$img" rm $partnum >/dev/null
|
||||||
part2=`parted "$img" unit B mkpart primary $partstart $newpartend`
|
parted -s "$img" unit B mkpart primary $partstart $newpartend >/dev/null
|
||||||
|
|
||||||
#Truncate the file
|
#Truncate the file
|
||||||
endresult=`parted -m "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B\n'`
|
endresult=$(parted -ms "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B')
|
||||||
truncate -s $endresult "$img"
|
truncate -s $endresult "$img"
|
||||||
aftersize=`ls -lah "$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"
|
||||||
|
|
Loading…
Reference in New Issue