From 9d6392d0c2e71f0e2b1d7d8a7bde7ba09d5c0cc1 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 01:23:54 +0000 Subject: [PATCH 01/12] Use $() for command-substitution instead of backticks --- pishrink.sh | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 19ec060..11f4771 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -29,7 +29,7 @@ if (( EUID != 0 )); then fi #Check that what we need is installed -A=`which parted 2>&1` +A=$(which parted 2>&1) if (( $? != 0 )); then echo "ERROR: parted is not installed." exit -4 @@ -47,20 +47,20 @@ if [ -n "$2" ]; then fi #Gather info -beforesize=`ls -lah "$img" | cut -d ' ' -f 5` -partnum=`parted -m "$img" unit B print | tail -n 1 | cut -d ':' -f 1 | tr -d '\n'` -partstart=`parted -m "$img" unit B print | tail -n 1 | cut -d ':' -f 2 | tr -d 'B\n'` -loopback=`losetup -f --show -o $partstart "$img"` -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'` +beforesize=$(ls -lah "$img" | cut -d ' ' -f 5) +partnum=$(parted -m "$img" unit B print | tail -n 1 | cut -d ':' -f 1 | tr -d '\n') +partstart=$(parted -m "$img" unit B print | tail -n 1 | cut -d ':' -f 2 | tr -d 'B\n') +loopback=$(losetup -f --show -o $partstart "$img") +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') #Check if we should make pi expand rootfs on next boot if [ "$should_skip_autoexpand" = false ]; then #Make pi expand rootfs on next boot - mountdir=`mktemp -d` + mountdir=$(mktemp -d) 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 mv $mountdir/etc/rc.local $mountdir/etc/rc.local.bak #####Do not touch the following lines##### @@ -132,19 +132,19 @@ fi #Make sure filesystem is ok 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 ' ' | tr -d '\n') if [[ $currentsize -eq $minsize ]]; then echo ERROR: Image already shrunk to smallest size exit -6 fi #Add some free space to the end of the filesystem -if [[ `expr $currentsize - $minsize - 5000` -gt 0 ]]; then - minsize=`expr $minsize + 5000 | tr -d '\n'` -elif [[ `expr $currentsize - $minsize - 1000` -gt 0 ]]; then - minsize=`expr $minsize + 1000 | tr -d '\n'` -elif [[ `expr $currentsize - $minsize - 100` -gt 0 ]]; then - minsize=`expr $minsize + 100 | tr -d '\n'` +if [[ $(expr $currentsize - $minsize - 5000) -gt 0 ]]; then + minsize=$(expr $minsize + 5000 | tr -d '\n') +elif [[ $(expr $currentsize - $minsize - 1000) -gt 0 ]]; then + minsize=$(expr $minsize + 1000 | tr -d '\n') +elif [[ $(expr $currentsize - $minsize - 100) -gt 0 ]]; then + minsize=$(expr $minsize + 100 | tr -d '\n') fi #Shrink filesystem @@ -161,14 +161,14 @@ sleep 1 #Shrink partition losetup -d $loopback -partnewsize=`expr $minsize \* $blocksize | tr -d '\n'` -newpartend=`expr $partstart + $partnewsize | tr -d '\n'` -part1=`parted "$img" rm $partnum` -part2=`parted "$img" unit B mkpart primary $partstart $newpartend` +partnewsize=$(expr $minsize \* $blocksize | tr -d '\n') +newpartend=$(expr $partstart + $partnewsize | tr -d '\n') +part1=$(parted "$img" rm $partnum) +part2=$(parted "$img" unit B mkpart primary $partstart $newpartend) #Truncate the file -endresult=`parted -m "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B\n'` +endresult=$(parted -m "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B\n') truncate -s $endresult "$img" -aftersize=`ls -lah "$img" | cut -d ' ' -f 5` +aftersize=$(ls -lah "$img" | cut -d ' ' -f 5) echo "Shrunk $img from $beforesize to $aftersize" From b11de9a8ee01f9ff9cb7c6fcff8d18dcb5b48716 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 01:34:46 +0000 Subject: [PATCH 02/12] Remove unused variables --- pishrink.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 11f4771..1b464a1 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -29,7 +29,7 @@ if (( EUID != 0 )); then fi #Check that what we need is installed -A=$(which parted 2>&1) +which parted 2>&1 >/dev/null if (( $? != 0 )); then echo "ERROR: parted is not installed." exit -4 @@ -163,8 +163,8 @@ sleep 1 losetup -d $loopback partnewsize=$(expr $minsize \* $blocksize | tr -d '\n') newpartend=$(expr $partstart + $partnewsize | tr -d '\n') -part1=$(parted "$img" rm $partnum) -part2=$(parted "$img" unit B mkpart primary $partstart $newpartend) +parted "$img" rm $partnum >/dev/null +parted "$img" unit B mkpart primary $partstart $newpartend >/dev/null #Truncate the file endresult=$(parted -m "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B\n') From b787dd4e66511af274e9230a18039691034e0376 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 01:37:05 +0000 Subject: [PATCH 03/12] Always quote echo strings --- pishrink.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 1b464a1..847caa9 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -61,7 +61,7 @@ if [ "$should_skip_autoexpand" = false ]; then mount $loopback $mountdir 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 #####Do not touch the following lines##### cat <<\EOF1 > $mountdir/etc/rc.local @@ -127,14 +127,14 @@ EOF1 fi umount $mountdir else - echo Skipping autoexpanding process... + echo "Skipping autoexpanding process..." fi #Make sure filesystem is ok e2fsck -p -f $loopback minsize=$(resize2fs -P $loopback | cut -d ':' -f 2 | tr -d ' ' | tr -d '\n') if [[ $currentsize -eq $minsize ]]; then - echo ERROR: Image already shrunk to smallest size + echo "ERROR: Image already shrunk to smallest size" exit -6 fi @@ -150,7 +150,7 @@ fi #Shrink filesystem resize2fs -p $loopback $minsize if [[ $? != 0 ]]; then - echo ERROR: resize2fs failed... + echo "ERROR: resize2fs failed..." mount $loopback $mountdir mv $mountdir/etc/rc.local.bak $mountdir/etc/rc.local umount $mountdir From 0b19d6c6ea631ca2090610d601e7cddfd12f121a Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 01:45:17 +0000 Subject: [PATCH 04/12] Add the -s flag to all parted commands --- pishrink.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 847caa9..936bf97 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -48,8 +48,8 @@ fi #Gather info beforesize=$(ls -lah "$img" | cut -d ' ' -f 5) -partnum=$(parted -m "$img" unit B print | tail -n 1 | cut -d ':' -f 1 | tr -d '\n') -partstart=$(parted -m "$img" unit B print | tail -n 1 | cut -d ':' -f 2 | tr -d 'B\n') +partnum=$(parted -ms "$img" unit B print | tail -n 1 | cut -d ':' -f 1 | tr -d '\n') +partstart=$(parted -ms "$img" unit B print | tail -n 1 | cut -d ':' -f 2 | tr -d 'B\n') loopback=$(losetup -f --show -o $partstart "$img") 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') @@ -163,11 +163,11 @@ sleep 1 losetup -d $loopback partnewsize=$(expr $minsize \* $blocksize | tr -d '\n') newpartend=$(expr $partstart + $partnewsize | tr -d '\n') -parted "$img" rm $partnum >/dev/null -parted "$img" unit B mkpart primary $partstart $newpartend >/dev/null +parted -s "$img" rm $partnum >/dev/null +parted -s "$img" unit B mkpart primary $partstart $newpartend >/dev/null #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\n') truncate -s $endresult "$img" aftersize=$(ls -lah "$img" | cut -d ' ' -f 5) From 49964b512c8e58f3518636e2ae0f86b9e7db7aad Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 02:28:49 +0000 Subject: [PATCH 05/12] Remove the unnecessary `tr -d '\n'` parts --- pishrink.sh | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 936bf97..b1a89ca 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -48,11 +48,11 @@ fi #Gather info beforesize=$(ls -lah "$img" | cut -d ' ' -f 5) -partnum=$(parted -ms "$img" unit B print | tail -n 1 | cut -d ':' -f 1 | tr -d '\n') -partstart=$(parted -ms "$img" unit B print | tail -n 1 | cut -d ':' -f 2 | tr -d 'B\n') +partnum=$(parted -ms "$img" unit B print | tail -n 1 | cut -d ':' -f 1) +partstart=$(parted -ms "$img" unit B print | tail -n 1 | cut -d ':' -f 2 | tr -d 'B') loopback=$(losetup -f --show -o $partstart "$img") -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') +currentsize=$(tune2fs -l $loopback | grep 'Block count' | tr -d ' ' | cut -d ':' -f 2) +blocksize=$(tune2fs -l $loopback | grep 'Block size' | tr -d ' ' | cut -d ':' -f 2) #Check if we should make pi expand rootfs on next boot if [ "$should_skip_autoexpand" = false ]; then @@ -132,7 +132,7 @@ fi #Make sure filesystem is ok 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 echo "ERROR: Image already shrunk to smallest size" exit -6 @@ -140,11 +140,11 @@ fi #Add some free space to the end of the filesystem if [[ $(expr $currentsize - $minsize - 5000) -gt 0 ]]; then - minsize=$(expr $minsize + 5000 | tr -d '\n') + minsize=$(expr $minsize + 5000) elif [[ $(expr $currentsize - $minsize - 1000) -gt 0 ]]; then - minsize=$(expr $minsize + 1000 | tr -d '\n') + minsize=$(expr $minsize + 1000) elif [[ $(expr $currentsize - $minsize - 100) -gt 0 ]]; then - minsize=$(expr $minsize + 100 | tr -d '\n') + minsize=$(expr $minsize + 100) fi #Shrink filesystem @@ -161,13 +161,13 @@ sleep 1 #Shrink partition losetup -d $loopback -partnewsize=$(expr $minsize \* $blocksize | tr -d '\n') -newpartend=$(expr $partstart + $partnewsize | tr -d '\n') +partnewsize=$(expr $minsize \* $blocksize) +newpartend=$(expr $partstart + $partnewsize) parted -s "$img" rm $partnum >/dev/null parted -s "$img" unit B mkpart primary $partstart $newpartend >/dev/null #Truncate the file -endresult=$(parted -ms "$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" aftersize=$(ls -lah "$img" | cut -d ' ' -f 5) From 8784bda9899bbf2d8aba42c992630c154a2ce8a1 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 02:33:33 +0000 Subject: [PATCH 06/12] Avoid calling identical commands multiple times --- pishrink.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index b1a89ca..188ce73 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -48,11 +48,13 @@ fi #Gather info beforesize=$(ls -lah "$img" | cut -d ' ' -f 5) -partnum=$(parted -ms "$img" unit B print | tail -n 1 | cut -d ':' -f 1) -partstart=$(parted -ms "$img" unit B print | tail -n 1 | cut -d ':' -f 2 | tr -d 'B') +parted_output=$(parted -ms "$img" unit B print | tail -n 1) +partnum=$(echo "$parted_output" | cut -d ':' -f 1) +partstart=$(echo "$parted_output" | cut -d ':' -f 2 | tr -d 'B') loopback=$(losetup -f --show -o $partstart "$img") -currentsize=$(tune2fs -l $loopback | grep 'Block count' | tr -d ' ' | cut -d ':' -f 2) -blocksize=$(tune2fs -l $loopback | grep 'Block size' | tr -d ' ' | cut -d ':' -f 2) +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 if [ "$should_skip_autoexpand" = false ]; then From c69f57affe066b480c985df526985f3d05c03908 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 02:34:58 +0000 Subject: [PATCH 07/12] Tighten up grep matches --- pishrink.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 188ce73..6cf98f0 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -53,8 +53,8 @@ partnum=$(echo "$parted_output" | cut -d ':' -f 1) partstart=$(echo "$parted_output" | cut -d ':' -f 2 | tr -d 'B') loopback=$(losetup -f --show -o $partstart "$img") 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) +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 if [ "$should_skip_autoexpand" = false ]; then From ec95ba8097cdec13e3c0124b05e3de331b06ac15 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 02:39:33 +0000 Subject: [PATCH 08/12] `ls` doesn't need `-a` flag when passed a filename --- pishrink.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 6cf98f0..38d7266 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -47,7 +47,7 @@ if [ -n "$2" ]; then fi #Gather info -beforesize=$(ls -lah "$img" | cut -d ' ' -f 5) +beforesize=$(ls -lh "$img" | cut -d ' ' -f 5) parted_output=$(parted -ms "$img" unit B print | tail -n 1) partnum=$(echo "$parted_output" | cut -d ':' -f 1) partstart=$(echo "$parted_output" | cut -d ':' -f 2 | tr -d 'B') @@ -171,6 +171,6 @@ parted -s "$img" unit B mkpart primary $partstart $newpartend >/dev/null #Truncate the file endresult=$(parted -ms "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B') 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" From e3ff4d20aa68fc455b36fcd6352a7eebeb69f143 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 02:50:41 +0000 Subject: [PATCH 09/12] Double-quote all the filenames --- pishrink.sh | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 38d7266..8b45879 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -52,7 +52,7 @@ parted_output=$(parted -ms "$img" unit B print | tail -n 1) partnum=$(echo "$parted_output" | cut -d ':' -f 1) partstart=$(echo "$parted_output" | cut -d ':' -f 2 | tr -d 'B') loopback=$(losetup -f --show -o $partstart "$img") -tune2fs_output=$(tune2fs -l $loopback) +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) @@ -60,13 +60,13 @@ blocksize=$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ': if [ "$should_skip_autoexpand" = false ]; then #Make pi expand rootfs on next boot 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" - 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##### -cat <<\EOF1 > $mountdir/etc/rc.local +cat <<\EOF1 > "$mountdir/etc/rc.local" #!/bin/bash do_expand_rootfs() { ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p') @@ -125,16 +125,16 @@ rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local exit 0 EOF1 #####End no touch zone##### - chmod +x $mountdir/etc/rc.local + chmod +x "$mountdir/etc/rc.local" fi - umount $mountdir + umount "$mountdir" else echo "Skipping autoexpanding process..." fi #Make sure filesystem is ok -e2fsck -p -f $loopback -minsize=$(resize2fs -P $loopback | cut -d ':' -f 2 | tr -d ' ') +e2fsck -p -f "$loopback" +minsize=$(resize2fs -P "$loopback" | cut -d ':' -f 2 | tr -d ' ') if [[ $currentsize -eq $minsize ]]; then echo "ERROR: Image already shrunk to smallest size" exit -6 @@ -150,19 +150,19 @@ elif [[ $(expr $currentsize - $minsize - 100) -gt 0 ]]; then fi #Shrink filesystem -resize2fs -p $loopback $minsize +resize2fs -p "$loopback" $minsize if [[ $? != 0 ]]; then echo "ERROR: resize2fs failed..." - mount $loopback $mountdir - mv $mountdir/etc/rc.local.bak $mountdir/etc/rc.local - umount $mountdir - losetup -d $loopback + mount "$loopback" "$mountdir" + mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local" + umount "$mountdir" + losetup -d "$loopback" exit -7 fi sleep 1 #Shrink partition -losetup -d $loopback +losetup -d "$loopback" partnewsize=$(expr $minsize \* $blocksize) newpartend=$(expr $partstart + $partnewsize) parted -s "$img" rm $partnum >/dev/null From 8331faf8f3c0705975d205c76fbb91c0f9887f32 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 03:05:31 +0000 Subject: [PATCH 10/12] Check for additional required commands --- pishrink.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 8b45879..0f63f18 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -29,11 +29,13 @@ if (( EUID != 0 )); then fi #Check that what we need is installed -which parted 2>&1 >/dev/null -if (( $? != 0 )); then - echo "ERROR: parted is not installed." - exit -4 -fi +for command in parted losetup tune2fs md5sum e2fsck resize2fs; do + which $command 2>&1 >/dev/null + if (( $? != 0 )); then + echo "ERROR: $command is not installed." + exit -4 + fi +done #Copy to new file if requested if [ -n "$2" ]; then From 201db16c933b6a1e6e5dd69aae3bc1c9986e3529 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 03:31:19 +0000 Subject: [PATCH 11/12] Replace `expr` with bash built-in arithmetic, and tidy up free-space calculation --- pishrink.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 0f63f18..aebc4ee 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -143,13 +143,13 @@ if [[ $currentsize -eq $minsize ]]; then fi #Add some free space to the end of the filesystem -if [[ $(expr $currentsize - $minsize - 5000) -gt 0 ]]; then - minsize=$(expr $minsize + 5000) -elif [[ $(expr $currentsize - $minsize - 1000) -gt 0 ]]; then - minsize=$(expr $minsize + 1000) -elif [[ $(expr $currentsize - $minsize - 100) -gt 0 ]]; then - minsize=$(expr $minsize + 100) -fi +extra_space=$(($currentsize - $minsize)) +for space in 5000 1000 100; do + if [[ $extra_space -gt $space ]]; then + minsize=$(($minsize + $space)) + break + fi +done #Shrink filesystem resize2fs -p "$loopback" $minsize @@ -165,8 +165,8 @@ sleep 1 #Shrink partition losetup -d "$loopback" -partnewsize=$(expr $minsize \* $blocksize) -newpartend=$(expr $partstart + $partnewsize) +partnewsize=$(($minsize * $blocksize)) +newpartend=$(($partstart + $partnewsize)) parted -s "$img" rm $partnum >/dev/null parted -s "$img" unit B mkpart primary $partstart $newpartend >/dev/null From 3568798dd45d6f2f03b90e821330fe98f2d831f8 Mon Sep 17 00:00:00 2001 From: Andrew Scheller Date: Mon, 22 Jan 2018 03:41:45 +0000 Subject: [PATCH 12/12] Give the new file the same ownership as the old file (instead of having it always owned just by root) --- pishrink.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pishrink.sh b/pishrink.sh index aebc4ee..cc5a671 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -45,6 +45,8 @@ if [ -n "$2" ]; then echo "ERROR: Could not copy file..." exit -5 fi + old_owner=$(stat -c %u:%g "$1") + chown $old_owner "$2" img="$2" fi