Last active
July 4, 2022 21:40
-
-
Save shvchk/77dfb8a77692e9188d62846814c72cfb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env sh | |
| snaps_dir="/var/lib/snapd/snaps" | |
| snaps_mnt="/snap" | |
| nsfs_mnt="/run/snapd/ns" | |
| def_compression="xz" | |
| new_compression="zstd" | |
| main() { | |
| snap list --all | awk '{print $1, $3}' | tail -n +2 | | |
| while read name rev; do | |
| echo "Processing $name" | |
| snap_file="${snaps_dir}/${name}_${rev}.snap" | |
| snap_compression=$(unsquashfs -s "$snap_file" | grep Compression | awk '{print $2}') | |
| if [ "$snap_compression" = "$def_compression" ]; then | |
| echo "$snap_file is compressed with $def_compression, recompressing" | |
| recompress "$name" "$rev" | |
| unset name rev | |
| fi | |
| done | |
| } | |
| recompress() { | |
| name="$1" | |
| rev="$2" | |
| name_esc="$(printf $name | sed 's|-|\\x2d|g')" | |
| snap_file="${snaps_dir}/${name}_${rev}.snap" | |
| snap_file_recompressed="${snap_file}.recompressed" | |
| snap_mnt_unit="snap-${name_esc}-${rev}.mount" | |
| # Default compression level is 15, which is pretty slow (decompression is fast as usual) | |
| mksquashfs "${snaps_mnt}/${name}/${rev}" "$snap_file_recompressed" -noappend -comp "$new_compression" | |
| chmod 600 "$snap_file_recompressed" | |
| systemctl stop "$snap_mnt_unit" | |
| umount "${snaps_mnt}/${name}/${rev}" | |
| umount "${nsfs_mnt}/${name}.mnt" | |
| rm -f "$snap_file" | |
| mv "$snap_file_recompressed" "$snap_file" | |
| systemctl start "$snap_mnt_unit" | |
| } | |
| set_niceness() { | |
| if [ -x "$(command -v nice)" ]; then | |
| renice -n 19 $$ | |
| fi | |
| if [ -x "$(command -v ionice)" ]; then | |
| ionice -c 3 -p $$ | |
| fi | |
| } | |
| set_niceness | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment