Skip to content

Instantly share code, notes, and snippets.

@andrew-stclair
Last active March 31, 2023 05:09
Show Gist options
  • Save andrew-stclair/0e9a0128094ef108c0d1024c4abf65e6 to your computer and use it in GitHub Desktop.
Save andrew-stclair/0e9a0128094ef108c0d1024c4abf65e6 to your computer and use it in GitHub Desktop.
LUKS Encrypted Disks scripts

LUKS Encrypted disks scripts

The following are scripts that can help with the use of LUKS encrypted disks, though they are very rudementry and require sudo.

The onlt script you will need is the crypt-format script, it's what creates the encrypted partition or file, the others just mount and unmount, which gnome can do on it's own (Other might be able to as well, but i have tested gnome)

Usage

Optional: Create mountable image

Run the following command to crate a mountable image of a pre-configured size

sudo dd if=/dev/zero bs=1000000000 count=8 of=crypt.img status=progress

Change the count=8 to the size you wish the image to be in GB, the example will create an 8GB image

Format partition/file

Run the following to format the partition or file to be an encrypted LUKS ext4 partition

crypt-format crypt.img

This will format the image created earlier, adjust crypt.img to point to the partition or file

Mount partition/file

Run the following to mount the partition or file

mkdir -p /media/crypt
crypt-mount crypt.img /media/crypt

Adjust as neccesary. This can be done via the GUI by just opening the image or inserting the drive with the encrypted partition

Unmount partition/file

Run the following to unmount

crypt-umount /media/crypt
rm -rf /media/crypt

Adjust as neccesary. This can be done via the GUI using the standard unmount procedure for any drive

#!/bin/bash
## Format a partition with LUKS
## Usage: crypt-format <partition>
echo "---- Formatting open partition with LUKS ----"
sudo cryptsetup luksFormat $1
echo "---- Formatting luks partition with ext4 ----"
sudo cryptsetup luksOpen $1 cryptpart
sudo mkfs.ext4 /dev/mapper/cryptpart
sudo cryptsetup luksClose cryptpart
#!/bin/bash
## Mount LUKS partiton
## Usage: crypt-mount <partition> <mount point>
sudo cryptsetup luksOpen $1 cryptpart
sudo mkdir -p /media/crypt
sudo mount /dev/mapper/cryptpart $2
#!/bin/bash
## Unmount LUKS partition
## Usage: crypt-umount <mount point>
sudo umount $1
sudo cryptsetup luksClose cryptpart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment