Skip to content

Commit

Permalink
Update pishrink.sh v24.10.23
Browse files Browse the repository at this point in the history
  • Loading branch information
mgomesborges committed Oct 28, 2024
1 parent 326b7cd commit 0fc58e4
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 78 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ This project is a dockerized version of the [PiShrink bash script](https://githu

## PiShrink options

```shell
```text
pishrink [-adhrsvzZ] IMAGE.img NEW-IMAGE.img
-s Do not expand filesystem when image is booted the first time
-v Enables more verbose output
-r Use advanced filesystem repair option if the normal one fails
-z Compress image after shrinking with gzip
-Z Compress image after shrinking with xz
-a Compress image in parallel using multiple cores
-d Write debug messages in a debug log file
-s Don't expand filesystem when image is booted the first time
-v Be verbose
-n Disable automatic update checking
-r Use advanced filesystem repair option if the normal one fails
-z Compress image after shrinking with gzip
-Z Compress image after shrinking with xz
-a Compress image in parallel using multiple cores
-d Write debug messages in a debug log file
```
If you specify the `NEW-IMAGE.img` parameter, the script will make a copy of `IMAGE.img` and work off that. You will need enough space to make a full copy of the image to use that option.
Expand All @@ -59,6 +60,6 @@ The source code is licensed under the [MIT license](LICENSE.md).
The content of this project itself is licensed under the [Creative Commons Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0).
[release-install-shield]: https://img.shields.io/badge/Release-15--Jul--2024-blue
[release-install-shield]: https://img.shields.io/badge/Release-29--Oct--2024-blue
[license-shield]: https://img.shields.io/github/license/monsieurborges/pishrink-docker
[docker-shield]: https://github.com/monsieurborges/pishrink-docker/actions/workflows/docker-publish.yml/badge.svg
164 changes: 95 additions & 69 deletions scripts/pishrink.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/bin/bash

version="v0.1.4"
# Project: PiShrink
# Description: 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.
# Link: https://github.com/Drewsif/PiShrink

version="v24.10.23"

CURRENT_DIR="$(pwd)"
SCRIPTNAME="${0##*/}"
Expand All @@ -13,7 +17,7 @@ declare -A ZIP_PARALLEL_OPTIONS=( [gzip]="-f9" [xz]="-T0" ) # options for zip to
declare -A ZIPEXTENSIONS=( [gzip]="gz" [xz]="xz" ) # extensions of zipped files

function info() {
echo "$SCRIPTNAME: $1 ..."
echo "$SCRIPTNAME: $1"
}

function error() {
Expand Down Expand Up @@ -88,15 +92,15 @@ function set_autoexpand() {
info "An existing /etc/rc.local was not found, autoexpand may fail..."
fi

if [[ -f "$mountdir/etc/rc.local" ]] && [[ "$(md5sum "$mountdir/etc/rc.local" | cut -d ' ' -f 1)" != "5c286b336c0606ed8e6f87708f7802eb" ]]; then
if ! grep -q "## PiShrink https://github.com/Drewsif/PiShrink ##" "$mountdir/etc/rc.local"; then
echo "Creating new /etc/rc.local"
if [ -f "$mountdir/etc/rc.local" ]; then
mv "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak"
fi

#####Do not touch the following lines#####
cat <<\EOF1 > "$mountdir/etc/rc.local"
cat <<'EOFRC' > "$mountdir/etc/rc.local"
#!/bin/bash
## PiShrink https://github.com/Drewsif/PiShrink ##
do_expand_rootfs() {
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
Expand Down Expand Up @@ -155,8 +159,8 @@ if [[ -f /etc/rc.local.bak ]]; then
/etc/rc.local
fi
exit 0
EOF1
#####End no touch zone#####
EOFRC

chmod +x "$mountdir/etc/rc.local"
fi
umount "$mountdir"
Expand All @@ -165,10 +169,11 @@ EOF1
help() {
local help
read -r -d '' help << EOM
Usage: $0 [-adhrsvzZ] imagefile.img [newimagefile.img]
Usage: $0 [-adhnrsvzZ] imagefile.img [newimagefile.img]
-s Don't expand filesystem when image is booted the first time
-v Be verbose
-n Disable automatic update checking
-r Use advanced filesystem repair option if the normal one fails
-z Compress image after shrinking with gzip
-Z Compress image after shrinking with xz
Expand All @@ -181,15 +186,17 @@ EOM

should_skip_autoexpand=false
debug=false
update_check=true
repair=false
parallel=false
verbose=false
ziptool=""

while getopts ":adhrsvzZ" opt; do
while getopts ":adnhrsvzZ" opt; do
case "${opt}" in
a) parallel=true;;
d) debug=true;;
n) update_check=false;;
h) help;;
r) repair=true;;
s) should_skip_autoexpand=true ;;
Expand All @@ -208,7 +215,16 @@ if [ "$debug" = true ]; then
exec 2> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&2)
fi

echo "${0##*/} $version"
echo -e "PiShrink $version - https://github.com/Drewsif/PiShrink\n"

# Try and check for updates
if $update_check; then
latest_release=$(curl -m 5 https://api.github.com/repos/Drewsif/PiShrink/releases/latest 2>/dev/null | grep -i "tag_name" 2>/dev/null | awk -F '"' '{print $4}' 2>/dev/null)
if [[ $? ]] && [ "$latest_release" \> "$version" ]; then
echo "WARNING: You do not appear to be running the latest version of PiShrink. Head on over to https://github.com/Drewsif/PiShrink to grab $latest_release"
echo ""
fi
fi

#Args
src="$1"
Expand All @@ -235,7 +251,6 @@ export LANGUAGE=POSIX
export LC_ALL=POSIX
export LANG=POSIX


# check selected compression tool is supported and installed
if [[ -n $ziptool ]]; then
if [[ ! " ${ZIPTOOLS[@]} " =~ $ziptool ]]; then
Expand Down Expand Up @@ -330,69 +345,80 @@ fi
minsize=$(cut -d ':' -f 2 <<< "$minsize" | tr -d ' ')
logVariables $LINENO currentsize minsize
if [[ $currentsize -eq $minsize ]]; then
error $LINENO "Image already shrunk to smallest size"
exit 11
fi
info "Filesystem already shrunk to smallest size. Skipping filesystem shrinking"
else
#Add some free space to the end of the filesystem
extra_space=$(($currentsize - $minsize))
logVariables $LINENO extra_space
for space in 5000 1000 100; do
if [[ $extra_space -gt $space ]]; then
minsize=$(($minsize + $space))
break
fi
done
logVariables $LINENO minsize

#Add some free space to the end of the filesystem
extra_space=$(($currentsize - $minsize))
logVariables $LINENO extra_space
for space in 5000 1000 100; do
if [[ $extra_space -gt $space ]]; then
minsize=$(($minsize + $space))
break
#Shrink filesystem
info "Shrinking filesystem"
if [ -z "$mountdir" ]; then
mountdir=$(mktemp -d)
fi
done
logVariables $LINENO minsize

#Shrink filesystem
info "Shrinking filesystem"
resize2fs -p "$loopback" $minsize
rc=$?
if (( $rc )); then
error $LINENO "resize2fs failed with rc $rc"
mount "$loopback" "$mountdir"
mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local"
umount "$mountdir"
losetup -d "$loopback"
exit 12
fi
sleep 1

#Shrink partition
partnewsize=$(($minsize * $blocksize))
newpartend=$(($partstart + $partnewsize))
logVariables $LINENO partnewsize newpartend
parted -s -a minimal "$img" rm "$partnum"
rc=$?
if (( $rc )); then
error $LINENO "parted failed with rc $rc"
exit 13
fi
resize2fs -p "$loopback" $minsize
rc=$?
if (( $rc )); then
error $LINENO "resize2fs failed with rc $rc"
mount "$loopback" "$mountdir"
mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local"
umount "$mountdir"
losetup -d "$loopback"
exit 12
else
info "Zeroing any free space left"
mount "$loopback" "$mountdir"
cat /dev/zero > "$mountdir/PiShrink_zero_file" 2>/dev/null
info "Zeroed $(ls -lh "$mountdir/PiShrink_zero_file" | cut -d ' ' -f 5)"
rm -f "$mountdir/PiShrink_zero_file"
umount "$mountdir"
fi
sleep 1

#Shrink partition
info "Shrinking partition"
partnewsize=$(($minsize * $blocksize))
newpartend=$(($partstart + $partnewsize))
logVariables $LINENO partnewsize newpartend
parted -s -a minimal "$img" rm "$partnum"
rc=$?
if (( $rc )); then
error $LINENO "parted failed with rc $rc"
exit 13
fi

parted -s "$img" unit B mkpart "$parttype" "$partstart" "$newpartend"
rc=$?
if (( $rc )); then
error $LINENO "parted failed with rc $rc"
exit 14
fi
parted -s "$img" unit B mkpart "$parttype" "$partstart" "$newpartend"
rc=$?
if (( $rc )); then
error $LINENO "parted failed with rc $rc"
exit 14
fi

#Truncate the file
info "Shrinking image"
endresult=$(parted -ms "$img" unit B print free)
rc=$?
if (( $rc )); then
error $LINENO "parted failed with rc $rc"
exit 15
fi
#Truncate the file
info "Truncating image"
endresult=$(parted -ms "$img" unit B print free)
rc=$?
if (( $rc )); then
error $LINENO "parted failed with rc $rc"
exit 15
fi

endresult=$(tail -1 <<< "$endresult" | cut -d ':' -f 2 | tr -d 'B')
logVariables $LINENO endresult
truncate -s "$endresult" "$img"
rc=$?
if (( $rc )); then
error $LINENO "trunate failed with rc $rc"
exit 16
endresult=$(tail -1 <<< "$endresult" | cut -d ':' -f 2 | tr -d 'B')
logVariables $LINENO endresult
truncate -s "$endresult" "$img"
rc=$?
if (( $rc )); then
error $LINENO "truncate failed with rc $rc"
exit 16
fi
fi

# handle compression
Expand Down Expand Up @@ -426,4 +452,4 @@ fi
aftersize=$(ls -lh "$img" | cut -d ' ' -f 5)
logVariables $LINENO aftersize

info "Shrunk $img from $beforesize to $aftersize"
info "Shrunk $img from $beforesize to $aftersize"

0 comments on commit 0fc58e4

Please sign in to comment.