├── README.md └── autosizer.sh /README.md: -------------------------------------------------------------------------------- 1 | # RaspberryPi-ImgAutoSizer 2 | This is a bash script to automatically resize Raspberry Pi images. 3 | Initially designed with only basic SD cards in mind, NOOBS images will not work. 4 | 5 | Copyright (C) 2017 SirLagz 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see 19 | -------------------------------------------------------------------------------- /autosizer.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # Automatic Image file resizer 3 | # Written by SirLagz 4 | # https://github.com/SirLagz/RaspberryPi-ImgAutoSizer 5 | strImgFile=$1 6 | extraSpaceMB=$2 7 | 8 | if [[ ! $(whoami) =~ "root" ]]; then 9 | echo "" 10 | echo "**********************************" 11 | echo "*** This should be run as root ***" 12 | echo "**********************************" 13 | echo "" 14 | exit 15 | fi 16 | 17 | if [[ -z $1 ]]; then 18 | echo "Usage: ./autosizer.sh []" 19 | exit 20 | fi 21 | 22 | if [[ -z $2 ]]; then 23 | extraSpaceMB=4 24 | fi 25 | 26 | if [[ ! -e $1 || ! $(file $1) =~ "x86" ]]; then 27 | echo "Error : Not an image file, or file doesn't exist" 28 | exit 29 | fi 30 | 31 | partinfo=`parted -s -m $1 unit B print` 32 | partnumber=`echo "$partinfo" | grep ext4 | awk -F: ' { print $1 } '` 33 | partstart=`echo "$partinfo" | grep ext4 | awk -F: ' { print substr($2,0,length($2)-1) } '` 34 | loopback=`losetup -f --show -o $partstart $1` 35 | e2fsck -f $loopback 36 | minsize=`resize2fs -P $loopback | awk -F': ' ' { print $2 } '` 37 | blocksize=$(dumpe2fs -h $loopback | grep 'Block size' | awk -F': ' ' { print $2 }') 38 | blocksize=${blocksize// /} 39 | 40 | let minsize=$minsize+$extraSpaceMB*1048576/$blocksize 41 | resize2fs -p $loopback $minsize 42 | sleep 1 43 | losetup -d $loopback 44 | let partnewsize=$minsize*$blocksize 45 | let newpartend=$partstart+$partnewsize 46 | part1=`parted -s $1 rm 2` 47 | part2=`parted -s $1 unit B mkpart primary $partstart $newpartend` 48 | endresult=`parted -s -m $1 unit B print free | tail -1 | awk -F: ' { print substr($2,0,length($2)-1) } '` 49 | truncate -s $endresult $1 50 | --------------------------------------------------------------------------------