Instantly share code, notes, and snippets.

Embed
What would you like to do?
LUKS encrypted image file
#!/bin/bash
FILENAME="private.img";
FILESIZE="100M";
# Create encrypted volume if it doesn't exist
if [ ! -f $FILENAME ]; then
echo "Creating image file...";
dd if=/dev/zero of=$FILENAME bs=$FILESIZE count=0 seek=1
echo "Setting permissions...";
chmod 600 $FILENAME;
echo "Mounting image file...";
sudo losetup -D;
sudo losetup /dev/loop0 $FILENAME;
echo "Encrypting image file...";
sudo cryptsetup -q -y luksFormat /dev/loop0;
echo "Opening encrypted volume...";
sudo cryptsetup luksOpen /dev/loop0 encrypted;
echo "Zeroing encrypted volume...";
sudo dd if=/dev/zero of=/dev/mapper/encrypted;
echo "Formatting encrypted volume...";
sudo mkfs.ext4 -L "encrypted" /dev/mapper/encrypted;
echo "Closing encrypted volume...";
sudo cryptsetup luksClose /dev/mapper/encrypted;
sudo losetup -D;
fi
echo "Mounting image file...";
sudo losetup -D;
sudo losetup /dev/loop0 $FILENAME;
echo "Decrypting image file...";
sudo cryptsetup luksOpen /dev/loop0 encrypted;
echo "Mounting encrypted volume...";
sudo mount /dev/mapper/encrypted /mnt/encrypted;
vim -i NONE -c 'set noswapfile' -c 'set nobackup' -c 'set noundofile' --cmd 'set undodir=/dev/null' /mnt/encrypted/;
echo "Unmounting encrypted volume...";
sudo umount /mnt/encrypted;
echo "Closing encrypted volume...";
sudo cryptsetup luksClose /dev/mapper/encrypted;
echo "Unmounting image file...";
sudo losetup -D;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment