Skip to content

Instantly share code, notes, and snippets.

Created December 30, 2014 20:25
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
luks encryption with loopback file
#/dev/mapper/xxxxx when open
#mountpoint of uncrypted device
#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
echo loopdevice $loopdevice
echo loopfile $loopfile
echo loopsize $loopsize
echo cryptmapper $cryptmapper
echo filesystem $makefilesystem
echo mountpoint $mountpoint
echo command $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment