Skip to content

Instantly share code, notes, and snippets.

@dbehnke
Created December 30, 2014 20:25
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dbehnke/ad19ca8f1ccf80aebca5 to your computer and use it in GitHub Desktop.
Save dbehnke/ad19ca8f1ccf80aebca5 to your computer and use it in GitHub Desktop.
luks encryption with loopback file
#!/bin/bash
loopdevice=/dev/loop0
loopfile=crypt.loop
#megabytes
loopsize=256
#/dev/mapper/xxxxx when open
cryptmapper=myCrypt
makefilesystem=ext4
#mountpoint of uncrypted device
mountpoint=$HOME/crypt
#creates a new file
create() {
echo creating a file with size ${loopsize}M with random bits.. this could take a while..
dd if=/dev/urandom of=$loopfile bs=1M count=$loopsize
losetup $loopdevice $loopfile
cryptsetup luksFormat -y $loopdevice
cryptsetup open $loopdevice $cryptmapper
sudo mkfs.$makefilesystem /dev/mapper/$cryptmapper
cryptsetup close $cryptmapper
losetup -d $loopdevice
losetup -a
}
#mounts crypted loopback file
open() {
losetup $loopdevice $loopfile
cryptsetup open $loopdevice $cryptmapper
mount /dev/mapper/$cryptmapper $mountpoint
}
#unmounts previously mounted loopback file
close() {
umount $mountpoint
cryptsetup close $cryptmapper
losetup -d $loopdevice
}
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
echo loopdevice $loopdevice
echo loopfile $loopfile
echo loopsize $loopsize
echo cryptmapper $cryptmapper
echo filesystem $makefilesystem
echo mountpoint $mountpoint
echo command $1
$1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment