Skip to content

Commit ca4de5a

Browse files
committed
Initial Commit
0 parents  commit ca4de5a

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

LICENSE

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Drew Bonasera
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+

README

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PiShrink #
2+
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.
3+
`Usage: ./pishrink imagefile.img`
4+
5+
## Example ##
6+
```bash
7+
[user@localhost PiShrink]$ sudo ./shrink.sh pi.img
8+
e2fsck 1.42.9 (28-Dec-2013)
9+
Pass 1: Checking inodes, blocks, and sizes
10+
Pass 2: Checking directory structure
11+
Pass 3: Checking directory connectivity
12+
Pass 4: Checking reference counts
13+
Pass 5: Checking group summary information
14+
/dev/loop1: 88262/1929536 files (0.2% non-contiguous), 842728/7717632 blocks
15+
resize2fs 1.42.9 (28-Dec-2013)
16+
resize2fs 1.42.9 (28-Dec-2013)
17+
Resizing the filesystem on /dev/loop1 to 773603 (4k) blocks.
18+
Begin pass 2 (max = 100387)
19+
Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
20+
Begin pass 3 (max = 236)
21+
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
22+
Begin pass 4 (max = 7348)
23+
Updating inode references XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
24+
The filesystem on /dev/loop1 is now 773603 blocks long.
25+
26+
Shrunk pi.img from 30G to 3.1G
27+
```
28+
29+
## Credits ##
30+
Thanks to SirLagz for the code to shrink http://sirlagz.net/2013/03/10/script-automatic-rpi-image-downsizer/

pishrink.sh

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
#Args
3+
img=$1
4+
5+
#Usage checks
6+
if [[ -z $img ]]; then
7+
echo "Usage: $0 imagefile.img"
8+
exit -1
9+
fi
10+
if [[ ! -e $img ]]; then
11+
echo "ERROR: $img is not a file..."
12+
exit -2
13+
fi
14+
if (( EUID != 0 )); then
15+
echo "ERROR: You need to be running as root."
16+
exit -3
17+
fi
18+
19+
#Gather info
20+
beforesize=`ls -lah $img | cut -d ' ' -f 5`
21+
partinfo=`parted -m $img unit B print`
22+
partnumber=`echo "$partinfo" | grep ext4 | awk -F: ' { print $img } '`
23+
partstart=`echo "$partinfo" | grep ext4 | awk -F: ' { print substr($2,0,length($2)-1) } '`
24+
loopback=`losetup -f --show -o $partstart $img`
25+
26+
#Make pi expand rootfs on next boot
27+
mountdir=`mktemp -d`
28+
mount $loopback $mountdir
29+
mv $mountdir/etc/rc.local $mountdir/etc/rc.expand.tmp
30+
cat <<\EOF > $mountdir/etc/rc.local
31+
#!/bin/sh
32+
/usr/bin/raspi-config --expand-rootfs; mv -f /etc/rc.expand.tmp /etc/rc.local; reboot
33+
exit 0
34+
EOF
35+
chmod +x $mountdir/etc/rc.local
36+
umount $loopback
37+
38+
#Shrink filesystem
39+
e2fsck -f $loopback
40+
minsize=`resize2fs -P $loopback | awk -F': ' ' { print $2 } '`
41+
minsize=`echo $minsize+20000 | bc`
42+
resize2fs -p $loopback $minsize
43+
sleep 1
44+
45+
#Shrink partition
46+
losetup -d $loopback
47+
partnewsize=`echo "$minsize * 4096" | bc`
48+
newpartend=`echo "$partstart + $partnewsize" | bc`
49+
part1=`parted $img rm 2`
50+
part2=`parted $img unit B mkpart primary $partstart $newpartend`
51+
52+
#Truncate the file
53+
endresult=`parted -m $img unit B print free | tail -1 | awk -F: ' { print substr($2,0,length($2)-1) } '`
54+
truncate -s $endresult $img
55+
aftersize=`ls -lah $img | cut -d ' ' -f 5`
56+
57+
echo "Shrunk $img from $beforesize to $aftersize"

0 commit comments

Comments
 (0)