Merge pull request #40 from lurch/various-tweaks

Various tweaks
This commit is contained in:
Drew Bonasera 2018-02-03 16:53:34 -05:00 committed by GitHub
commit 8dccf7d154
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 43 deletions

View File

@ -29,11 +29,13 @@ if (( EUID != 0 )); then
fi fi
#Check that what we need is installed #Check that what we need is installed
A=`which parted 2>&1` for command in parted losetup tune2fs md5sum e2fsck resize2fs; do
if (( $? != 0 )); then which $command 2>&1 >/dev/null
echo "ERROR: parted is not installed." if (( $? != 0 )); then
echo "ERROR: $command is not installed."
exit -4 exit -4
fi fi
done
#Copy to new file if requested #Copy to new file if requested
if [ -n "$2" ]; then if [ -n "$2" ]; then
@ -43,28 +45,32 @@ if [ -n "$2" ]; then
echo "ERROR: Could not copy file..." echo "ERROR: Could not copy file..."
exit -5 exit -5
fi fi
old_owner=$(stat -c %u:%g "$1")
chown $old_owner "$2"
img="$2" img="$2"
fi fi
#Gather info #Gather info
beforesize=`ls -lah "$img" | cut -d ' ' -f 5` beforesize=$(ls -lh "$img" | cut -d ' ' -f 5)
partnum=`parted -m "$img" unit B print | tail -n 1 | cut -d ':' -f 1 | tr -d '\n'` parted_output=$(parted -ms "$img" unit B print | tail -n 1)
partstart=`parted -m "$img" unit B print | tail -n 1 | cut -d ':' -f 2 | tr -d 'B\n'` partnum=$(echo "$parted_output" | cut -d ':' -f 1)
loopback=`losetup -f --show -o $partstart "$img"` partstart=$(echo "$parted_output" | cut -d ':' -f 2 | tr -d 'B')
currentsize=`tune2fs -l $loopback | grep 'Block count' | tr -d ' ' | cut -d ':' -f 2 | tr -d '\n'` loopback=$(losetup -f --show -o $partstart "$img")
blocksize=`tune2fs -l $loopback | grep 'Block size' | tr -d ' ' | cut -d ':' -f 2 | tr -d '\n'` tune2fs_output=$(tune2fs -l "$loopback")
currentsize=$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -d ':' -f 2)
blocksize=$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2)
#Check if we should make pi expand rootfs on next boot #Check if we should make pi expand rootfs on next boot
if [ "$should_skip_autoexpand" = false ]; then 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"
if [ `md5sum $mountdir/etc/rc.local | cut -d ' ' -f 1` != "0542054e9ff2d2e0507ea1ffe7d4fc87" ]; then if [ $(md5sum "$mountdir/etc/rc.local" | cut -d ' ' -f 1) != "0542054e9ff2d2e0507ea1ffe7d4fc87" ]; 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"
#####Do not touch the following lines##### #####Do not touch the following lines#####
cat <<\EOF1 > $mountdir/etc/rc.local cat <<\EOF1 > "$mountdir/etc/rc.local"
#!/bin/bash #!/bin/bash
do_expand_rootfs() { do_expand_rootfs() {
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p') ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
@ -123,52 +129,52 @@ rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local
exit 0 exit 0
EOF1 EOF1
#####End no touch zone##### #####End no touch zone#####
chmod +x $mountdir/etc/rc.local chmod +x "$mountdir/etc/rc.local"
fi fi
umount $mountdir umount "$mountdir"
else else
echo Skipping autoexpanding process... echo "Skipping autoexpanding process..."
fi fi
#Make sure filesystem is ok #Make sure filesystem is ok
e2fsck -p -f $loopback e2fsck -p -f "$loopback"
minsize=`resize2fs -P $loopback | cut -d ':' -f 2 | tr -d ' ' | tr -d '\n'` minsize=$(resize2fs -P "$loopback" | cut -d ':' -f 2 | tr -d ' ')
if [[ $currentsize -eq $minsize ]]; then if [[ $currentsize -eq $minsize ]]; then
echo ERROR: Image already shrunk to smallest size echo "ERROR: Image already shrunk to smallest size"
exit -6 exit -6
fi fi
#Add some free space to the end of the filesystem #Add some free space to the end of the filesystem
if [[ `expr $currentsize - $minsize - 5000` -gt 0 ]]; then extra_space=$(($currentsize - $minsize))
minsize=`expr $minsize + 5000 | tr -d '\n'` for space in 5000 1000 100; do
elif [[ `expr $currentsize - $minsize - 1000` -gt 0 ]]; then if [[ $extra_space -gt $space ]]; then
minsize=`expr $minsize + 1000 | tr -d '\n'` minsize=$(($minsize + $space))
elif [[ `expr $currentsize - $minsize - 100` -gt 0 ]]; then break
minsize=`expr $minsize + 100 | tr -d '\n'` fi
fi done
#Shrink filesystem #Shrink filesystem
resize2fs -p $loopback $minsize resize2fs -p "$loopback" $minsize
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
echo ERROR: resize2fs failed... echo "ERROR: resize2fs failed..."
mount $loopback $mountdir mount "$loopback" "$mountdir"
mv $mountdir/etc/rc.local.bak $mountdir/etc/rc.local mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local"
umount $mountdir umount "$mountdir"
losetup -d $loopback losetup -d "$loopback"
exit -7 exit -7
fi fi
sleep 1 sleep 1
#Shrink partition #Shrink partition
losetup -d $loopback losetup -d "$loopback"
partnewsize=`expr $minsize \* $blocksize | tr -d '\n'` partnewsize=$(($minsize * $blocksize))
newpartend=`expr $partstart + $partnewsize | tr -d '\n'` newpartend=$(($partstart + $partnewsize))
part1=`parted "$img" rm $partnum` parted -s "$img" rm $partnum >/dev/null
part2=`parted "$img" unit B mkpart primary $partstart $newpartend` parted -s "$img" unit B mkpart primary $partstart $newpartend >/dev/null
#Truncate the file #Truncate the file
endresult=`parted -m "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B\n'` endresult=$(parted -ms "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B')
truncate -s $endresult "$img" truncate -s $endresult "$img"
aftersize=`ls -lah "$img" | cut -d ' ' -f 5` aftersize=$(ls -lh "$img" | cut -d ' ' -f 5)
echo "Shrunk $img from $beforesize to $aftersize" echo "Shrunk $img from $beforesize to $aftersize"