Merge pull request #5 from rbaumbach/master

Add getops to script to provide user the ability to skip autoexpanding
This commit is contained in:
Drew Bonasera 2016-07-13 15:44:08 -04:00 committed by GitHub
commit abb17e217e
2 changed files with 36 additions and 19 deletions

View File

@ -1,13 +1,13 @@
# PiShrink # # PiShrink #
PiShrink is a bash script that automatically shrink a pi image that will then resize to the max size of the SD card on boot. This will make putting the image back onto the SD card faster and the shrunk images will compress better. PiShrink is a bash script that automatically shrink a pi image that will then resize to the max size of the SD card on boot. This will make putting the image back onto the SD card faster and the shrunk images will compress better.
`Usage: ./pishrink imagefile.img [newimagefile.img]` `Usage: ./pishrink [-s] imagefile.img [newimagefile.img]`
If you specify the `newimagefile.img` parameter, the script will make a copy of `imagefile.img` and work off that. You will need enough space to make a full copy of the image to use that option. If the `-s` option is given the script will skip the autoexpanding part of the process. If you specify the `newimagefile.img` parameter, the script will make a copy of `imagefile.img` and work off that. You will need enough space to make a full copy of the image to use that option.
## Example ## ## Example ##
```bash ```bash
[user@localhost PiShrink]$ sudo ./shrink.sh pi.img [user@localhost PiShrink]$ sudo ./shrink.sh pi.img
e2fsck 1.42.9 (28-Dec-2013) e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure Pass 2: Checking directory structure

View File

@ -1,11 +1,23 @@
#!/bin/bash #!/bin/bash
usage() { echo "Usage: $0 [-s] imagefile.img [newimagefile.img]"; exit -1; }
should_skip_autoexpand=false
while getopts ":s" opt; do
case "${opt}" in
s) should_skip_autoexpand=true ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
#Args #Args
img=$1 img=$1
#Usage checks #Usage checks
if [[ -z $img ]]; then if [[ -z $img ]]; then
echo "Usage: $0 imagefile.img [newimagefile.img]" usage
exit -1
fi fi
if [[ ! -e $img ]]; then if [[ ! -e $img ]]; then
echo "ERROR: $img is not a file..." echo "ERROR: $img is not a file..."
@ -42,22 +54,27 @@ loopback=`losetup -f --show -o $partstart $img`
currentsize=`tune2fs -l $loopback | grep 'Block count' | tr -d ' ' | cut -d ':' -f 2 | tr -d '\n'` currentsize=`tune2fs -l $loopback | grep 'Block count' | tr -d ' ' | cut -d ':' -f 2 | tr -d '\n'`
blocksize=`tune2fs -l $loopback | grep 'Block size' | tr -d ' ' | cut -d ':' -f 2 | tr -d '\n'` blocksize=`tune2fs -l $loopback | grep 'Block size' | tr -d ' ' | cut -d ':' -f 2 | tr -d '\n'`
#Make pi expand rootfs on next boot #Check if we should make pi expand rootfs on next boot
mountdir=`mktemp -d` if [ "$should_skip_autoexpand" = false ]; then
mount $loopback $mountdir #Make pi expand rootfs on next boot
mountdir=`mktemp -d`
mount $loopback $mountdir
if [ `md5sum $mountdir/etc/rc.local | cut -d ' ' -f 1` != "a27a4d8192ea6ba713d2ddd15a55b1df" ]; then if [ `md5sum $mountdir/etc/rc.local | cut -d ' ' -f 1` != "a27a4d8192ea6ba713d2ddd15a55b1df" ]; 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
cat <<\EOF > $mountdir/etc/rc.local cat <<\EOF > $mountdir/etc/rc.local
#!/bin/bash #!/bin/bash
/usr/bin/raspi-config --expand-rootfs /usr/bin/raspi-config --expand-rootfs
rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; reboot rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; reboot
exit 0 exit 0
EOF EOF
chmod +x $mountdir/etc/rc.local chmod +x $mountdir/etc/rc.local
fi
umount $mountdir
else
echo Skipping autoexpanding process...
fi fi
umount $mountdir
#Make sure filesystem is ok #Make sure filesystem is ok
e2fsck -f $loopback e2fsck -f $loopback
@ -100,4 +117,4 @@ endresult=`parted -m $img unit B print free | tail -1 | cut -d ':' -f 2 | tr -d
truncate -s $endresult $img truncate -s $endresult $img
aftersize=`ls -lah $img | cut -d ' ' -f 5` aftersize=`ls -lah $img | cut -d ' ' -f 5`
echo "Shrunk $img from $beforesize to $aftersize" echo "Shrunk $img from $beforesize to $aftersize"