Add getops to script to provide user the ability to skip autoexpanding process of the script
This commit is contained in:
parent
816e1efa15
commit
cad2e0e11a
|
@ -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
|
||||||
|
|
47
pishrink.sh
47
pishrink.sh
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue