Skip to content

Instantly share code, notes, and snippets.

@jigpu
Created October 29, 2019 18:03
Show Gist options
  • Select an option

  • Save jigpu/428ea6548809b5c06f9a1f1f2fc4214f to your computer and use it in GitHub Desktop.

Select an option

Save jigpu/428ea6548809b5c06f9a1f1f2fc4214f to your computer and use it in GitHub Desktop.

Revisions

  1. jigpu created this gist Oct 29, 2019.
    35 changes: 35 additions & 0 deletions crypt-squashfs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/bin/sh

    # https://martin.elwin.com/blog/2008/05/backups-with-squashfs-and-luks/

    set -e

    SQUASHFS_IMG="$1"
    LUKS_IMG="$2"
    CRYPTNAME=mkcrypt-$RANDOM
    CRYPTDEV="/dev/mapper/$CRYPTNAME"
    OVERHEAD=32768

    BLOCKCOUNT=$(du --block-size=512 "$SQUASHFS_IMG" | cut -f1)

    dd if=/dev/zero of="$LUKS_IMG" bs=512 count=1 seek=$(($BLOCKCOUNT + $OVERHEAD))
    LOOPDEV=$(losetup -f)
    losetup "$LOOPDEV" "$LUKS_IMG"
    cryptsetup luksFormat "$LOOPDEV"
    cryptsetup luksOpen "$LOOPDEV" "$CRYPTNAME"

    if test $(blockdev --getsize "$CRYPTDEV") -lt $BLOCKCOUNT; then
    DELTA=$(( $BLOCKCOUNT - $(blockdev --getsize "$CRYPTDEV") ))
    echo "Cannot fit image into available space! Increase overhead by $DELTA"

    cryptsetup luksClose "$CRYPTNAME"
    losetup -d "$LOOPDEV"
    rm "$LUKS_IMG"
    exit 1
    fi
    dd if="$SQUASHFS_IMG" of="$CRYPTDEV" status=progress

    cryptsetup luksClose "$CRYPTNAME"
    losetup -d "$LOOPDEV"

    echo "Complete! You may delete $SQUASHFS_IMG after testing the output."