Add getops to script to provide user the ability to skip autoexpanding process of the script

This commit is contained in:
rbaumbach 2016-07-09 20:45:54 -07:00
parent 816e1efa15
commit cad2e0e11a
No known key found for this signature in database
GPG Key ID: E61598E62DF9DE83
2 changed files with 36 additions and 19 deletions

View File

@ -1,9 +1,9 @@
# 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

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,6 +54,8 @@ 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'`
#Check if we should make pi expand rootfs on next boot
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
@ -58,6 +72,9 @@ EOF
chmod +x $mountdir/etc/rc.local chmod +x $mountdir/etc/rc.local
fi fi
umount $mountdir umount $mountdir
else
echo Skipping autoexpanding process...
fi
#Make sure filesystem is ok #Make sure filesystem is ok
e2fsck -f $loopback e2fsck -f $loopback