limeshrink.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #!/bin/bash
  2. version="v0.1.2"
  3. CURRENT_DIR="$(pwd)"
  4. SCRIPTNAME="${0##*/}"
  5. MYNAME="${SCRIPTNAME%.*}"
  6. LOGFILE="${CURRENT_DIR}/${SCRIPTNAME%.*}.log"
  7. REQUIRED_TOOLS="parted losetup tune2fs md5sum e2fsck resize2fs"
  8. ZIPTOOLS=("gzip xz")
  9. declare -A ZIP_PARALLEL_TOOL=( [gzip]="pigz" [xz]="xz" ) # parallel zip tool to use in parallel mode
  10. declare -A ZIP_PARALLEL_OPTIONS=( [gzip]="-f9" [xz]="-T0" ) # options for zip tools in parallel mode
  11. declare -A ZIPEXTENSIONS=( [gzip]="gz" [xz]="xz" ) # extensions of zipped files
  12. function info() {
  13. echo "$SCRIPTNAME: $1 ..."
  14. }
  15. function error() {
  16. echo -n "$SCRIPTNAME: ERROR occured in line $1: "
  17. shift
  18. echo "$@"
  19. }
  20. function cleanup() {
  21. if losetup "$loopback" &>/dev/null; then
  22. losetup -d "$loopback"
  23. fi
  24. if [ "$debug" = true ]; then
  25. local old_owner=$(stat -c %u:%g "$src")
  26. chown "$old_owner" "$LOGFILE"
  27. fi
  28. }
  29. function logVariables() {
  30. if [ "$debug" = true ]; then
  31. echo "Line $1" >> "$LOGFILE"
  32. shift
  33. local v var
  34. for var in "$@"; do
  35. eval "v=\$$var"
  36. echo "$var: $v" >> "$LOGFILE"
  37. done
  38. fi
  39. }
  40. function checkFilesystem() {
  41. info "Checking filesystem"
  42. e2fsck -pf "$loopback"
  43. (( $? < 4 )) && return
  44. info "Filesystem error detected!"
  45. info "Trying to recover corrupted filesystem"
  46. e2fsck -y "$loopback"
  47. (( $? < 4 )) && return
  48. if [[ $repair == true ]]; then
  49. info "Trying to recover corrupted filesystem - Phase 2"
  50. e2fsck -fy -b 32768 "$loopback"
  51. (( $? < 4 )) && return
  52. fi
  53. error $LINENO "Filesystem recoveries failed. Giving up..."
  54. exit 9
  55. }
  56. function set_autoexpand() {
  57. #Make pi expand rootfs on next boot
  58. mountdir=$(mktemp -d)
  59. partprobe "$loopback"
  60. mount "$loopback" "$mountdir"
  61. if [ ! -d "$mountdir/etc" ]; then
  62. info "/etc not found, autoexpand will not be enabled"
  63. umount "$mountdir"
  64. return
  65. fi
  66. if [[ -f "$mountdir/etc/rc.local" ]] && [[ "$(md5sum "$mountdir/etc/rc.local" | cut -d ' ' -f 1)" != "1c579c7d5b4292fd948399b6ece39009" ]]; then
  67. echo "Creating new /etc/rc.local"
  68. if [ -f "$mountdir/etc/rc.local" ]; then
  69. mv "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak"
  70. fi
  71. #####Do not touch the following lines#####
  72. cat <<\EOF1 > "$mountdir/etc/rc.local"
  73. #!/bin/bash
  74. do_expand_rootfs() {
  75. ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
  76. PART_NUM=${ROOT_PART#mmcblk0p}
  77. if [ "$PART_NUM" = "$ROOT_PART" ]; then
  78. echo "$ROOT_PART is not an SD card. Don't know how to expand"
  79. return 0
  80. fi
  81. # Get the starting offset of the root partition
  82. PART_START=$(parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g')
  83. [ "$PART_START" ] || return 1
  84. # Return value will likely be error for fdisk as it fails to reload the
  85. # partition table because the root fs is mounted
  86. fdisk /dev/mmcblk0 <<EOF
  87. p
  88. d
  89. $PART_NUM
  90. n
  91. p
  92. $PART_NUM
  93. $PART_START
  94. p
  95. w
  96. EOF
  97. cat <<EOF > /etc/rc.local &&
  98. #!/bin/sh
  99. echo "Expanding /dev/$ROOT_PART"
  100. resize2fs /dev/$ROOT_PART
  101. rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local
  102. EOF
  103. reboot
  104. exit
  105. }
  106. # raspi_config_expand() {
  107. # /usr/bin/env raspi-config --expand-rootfs
  108. # if [[ $? != 0 ]]; then
  109. # return -1
  110. # else
  111. # rm -f /etc/rc.local; cp -f /etc/rc.local.bak /etc/rc.local; /etc/rc.local
  112. # reboot
  113. # exit
  114. # fi
  115. # }
  116. # raspi_config_expand
  117. echo "WARNING: Using backup expand..."
  118. sleep 5
  119. do_expand_rootfs
  120. echo "ERROR: Expanding failed..."
  121. sleep 5
  122. if [[ -f /etc/rc.local.bak ]]; then
  123. cp -f /etc/rc.local.bak /etc/rc.local
  124. /etc/rc.local
  125. fi
  126. exit 0
  127. EOF1
  128. #####End no touch zone#####
  129. chmod +x "$mountdir/etc/rc.local"
  130. fi
  131. umount "$mountdir"
  132. }
  133. help() {
  134. local help
  135. read -r -d '' help << EOM
  136. Usage: $0 [-adhrspvzZ] imagefile.img [newimagefile.img]
  137. -s Don't expand filesystem when image is booted the first time
  138. -v Be verbose
  139. -r Use advanced filesystem repair option if the normal one fails
  140. -z Compress image after shrinking with gzip
  141. -Z Compress image after shrinking with xz
  142. -a Compress image in parallel using multiple cores
  143. -p Remove logs, apt archives, dhcp leases and ssh hostkeys
  144. -d Write debug messages in a debug log file
  145. EOM
  146. echo "$help"
  147. exit 1
  148. }
  149. should_skip_autoexpand=false
  150. debug=false
  151. repair=false
  152. parallel=false
  153. verbose=false
  154. prep=false
  155. ziptool=""
  156. while getopts ":adhprsvzZ" opt; do
  157. case "${opt}" in
  158. a) parallel=true;;
  159. d) debug=true;;
  160. h) help;;
  161. p) prep=true;;
  162. r) repair=true;;
  163. s) should_skip_autoexpand=true ;;
  164. v) verbose=true;;
  165. z) ziptool="gzip";;
  166. Z) ziptool="xz";;
  167. *) help;;
  168. esac
  169. done
  170. shift $((OPTIND-1))
  171. if [ "$debug" = true ]; then
  172. info "Creating log file $LOGFILE"
  173. rm "$LOGFILE" &>/dev/null
  174. exec 1> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&1)
  175. exec 2> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&2)
  176. fi
  177. echo "${0##*/} $version"
  178. #Args
  179. src="$1"
  180. img="$1"
  181. #Usage checks
  182. if [[ -z "$img" ]]; then
  183. help
  184. fi
  185. if [[ ! -f "$img" ]]; then
  186. error $LINENO "$img is not a file..."
  187. exit 2
  188. fi
  189. if (( EUID != 0 )); then
  190. error $LINENO "You need to be running as root."
  191. exit 3
  192. fi
  193. # check selected compression tool is supported and installed
  194. if [[ -n $ziptool ]]; then
  195. if [[ ! " ${ZIPTOOLS[@]} " =~ $ziptool ]]; then
  196. error $LINENO "$ziptool is an unsupported ziptool."
  197. exit 17
  198. else
  199. if [[ $parallel == true && $ziptool == "gzip" ]]; then
  200. REQUIRED_TOOLS="$REQUIRED_TOOLS pigz"
  201. else
  202. REQUIRED_TOOLS="$REQUIRED_TOOLS $ziptool"
  203. fi
  204. fi
  205. fi
  206. #Check that what we need is installed
  207. for command in $REQUIRED_TOOLS; do
  208. command -v $command >/dev/null 2>&1
  209. if (( $? != 0 )); then
  210. error $LINENO "$command is not installed."
  211. exit 4
  212. fi
  213. done
  214. #Copy to new file if requested
  215. if [ -n "$2" ]; then
  216. f="$2"
  217. if [[ -n $ziptool && "${f##*.}" == "${ZIPEXTENSIONS[$ziptool]}" ]]; then # remove zip extension if zip requested because zip tool will complain about extension
  218. f="${f%.*}"
  219. fi
  220. info "Copying $1 to $f..."
  221. cp --reflink=auto --sparse=always "$1" "$f"
  222. if (( $? != 0 )); then
  223. error $LINENO "Could not copy file..."
  224. exit 5
  225. fi
  226. old_owner=$(stat -c %u:%g "$1")
  227. chown "$old_owner" "$f"
  228. img="$f"
  229. fi
  230. # cleanup at script exit
  231. trap cleanup EXIT
  232. #Gather info
  233. info "Gathering data"
  234. beforesize="$(ls -lh "$img" | cut -d ' ' -f 5)"
  235. parted_output="$(parted -ms "$img" unit B print)"
  236. rc=$?
  237. if (( $rc )); then
  238. error $LINENO "parted failed with rc $rc"
  239. info "Possibly invalid image. Run 'parted $img unit B print' manually to investigate"
  240. exit 6
  241. fi
  242. partnum="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 1)"
  243. partstart="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 2 | tr -d 'B')"
  244. if [ -z "$(parted -s "$img" unit B print | grep "$partstart" | grep logical)" ]; then
  245. parttype="primary"
  246. else
  247. parttype="logical"
  248. fi
  249. loopback="$(losetup -f --show -o "$partstart" "$img")"
  250. tune2fs_output="$(tune2fs -l "$loopback")"
  251. rc=$?
  252. if (( $rc )); then
  253. echo "$tune2fs_output"
  254. error $LINENO "tune2fs failed. Unable to shrink this type of image"
  255. exit 7
  256. fi
  257. currentsize="$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -d ':' -f 2)"
  258. blocksize="$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2)"
  259. logVariables $LINENO beforesize parted_output partnum partstart parttype tune2fs_output currentsize blocksize
  260. #Check if we should make pi expand rootfs on next boot
  261. if [ "$parttype" == "logical" ]; then
  262. echo "WARNING: PiShrink does not yet support autoexpanding of this type of image"
  263. elif [ "$should_skip_autoexpand" = false ]; then
  264. set_autoexpand
  265. else
  266. echo "Skipping autoexpanding process..."
  267. fi
  268. if [[ $prep == true ]]; then
  269. info "Syspreping: Removing logs, apt archives, dhcp leases and ssh hostkeys"
  270. mountdir=$(mktemp -d)
  271. mount "$loopback" "$mountdir"
  272. rm -rvf $mountdir/var/cache/apt/archives/* $mountdir/var/lib/dhcpcd5/* $mountdir/var/log/* $mountdir/var/tmp/* $mountdir/tmp/* $mountdir/etc/ssh/*_host_*
  273. umount "$mountdir"
  274. fi
  275. #Make sure filesystem is ok
  276. checkFilesystem
  277. if ! minsize=$(resize2fs -P "$loopback"); then
  278. rc=$?
  279. error $LINENO "resize2fs failed with rc $rc"
  280. exit 10
  281. fi
  282. minsize=$(cut -d ':' -f 2 <<< "$minsize" | tr -d ' ')
  283. logVariables $LINENO currentsize minsize
  284. if [[ $currentsize -eq $minsize ]]; then
  285. error $LINENO "Image already shrunk to smallest size"
  286. exit 11
  287. fi
  288. #Add some free space to the end of the filesystem
  289. extra_space=$(($currentsize - $minsize))
  290. logVariables $LINENO extra_space
  291. for space in 5000 1000 100; do
  292. if [[ $extra_space -gt $space ]]; then
  293. minsize=$(($minsize + $space))
  294. break
  295. fi
  296. done
  297. logVariables $LINENO minsize
  298. #Shrink filesystem
  299. info "Shrinking filesystem"
  300. resize2fs -p "$loopback" $minsize
  301. rc=$?
  302. if (( $rc )); then
  303. error $LINENO "resize2fs failed with rc $rc"
  304. mount "$loopback" "$mountdir"
  305. mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local"
  306. umount "$mountdir"
  307. losetup -d "$loopback"
  308. exit 12
  309. fi
  310. sleep 1
  311. #Shrink partition
  312. partnewsize=$(($minsize * $blocksize))
  313. newpartend=$(($partstart + $partnewsize))
  314. logVariables $LINENO partnewsize newpartend
  315. parted -s -a minimal "$img" rm "$partnum"
  316. rc=$?
  317. if (( $rc )); then
  318. error $LINENO "parted failed with rc $rc"
  319. exit 13
  320. fi
  321. parted -s "$img" unit B mkpart "$parttype" "$partstart" "$newpartend"
  322. rc=$?
  323. if (( $rc )); then
  324. error $LINENO "parted failed with rc $rc"
  325. exit 14
  326. fi
  327. #Truncate the file
  328. info "Shrinking image"
  329. endresult=$(parted -ms "$img" unit B print free)
  330. rc=$?
  331. if (( $rc )); then
  332. error $LINENO "parted failed with rc $rc"
  333. exit 15
  334. fi
  335. endresult=$(tail -1 <<< "$endresult" | cut -d ':' -f 2 | tr -d 'B')
  336. logVariables $LINENO endresult
  337. truncate -s "$endresult" "$img"
  338. rc=$?
  339. if (( $rc )); then
  340. error $LINENO "trunate failed with rc $rc"
  341. exit 16
  342. fi
  343. # handle compression
  344. if [[ -n $ziptool ]]; then
  345. options=""
  346. envVarname="${MYNAME^^}_${ziptool^^}" # PISHRINK_GZIP or PISHRINK_XZ environment variables allow to override all options for gzip or xz
  347. [[ $parallel == true ]] && options="${ZIP_PARALLEL_OPTIONS[$ziptool]}"
  348. [[ -v $envVarname ]] && options="${!envVarname}" # if environment variable defined use these options
  349. [[ $verbose == true ]] && options="$options -v" # add verbose flag if requested
  350. if [[ $parallel == true ]]; then
  351. parallel_tool="${ZIP_PARALLEL_TOOL[$ziptool]}"
  352. info "Using $parallel_tool on the shrunk image"
  353. if ! $parallel_tool ${options} "$img"; then
  354. rc=$?
  355. error $LINENO "$parallel_tool failed with rc $rc"
  356. exit 18
  357. fi
  358. else # sequential
  359. info "Using $ziptool on the shrunk image"
  360. if ! $ziptool ${options} "$img"; then
  361. rc=$?
  362. error $LINENO "$ziptool failed with rc $rc"
  363. exit 19
  364. fi
  365. fi
  366. img=$img.${ZIPEXTENSIONS[$ziptool]}
  367. fi
  368. aftersize=$(ls -lh "$img" | cut -d ' ' -f 5)
  369. logVariables $LINENO aftersize
  370. info "Shrunk $img from $beforesize to $aftersize"