#!/bin/bash
###############################################################################
# AUTHOR BM 20090505
# Copyright (C) 2009 Beppe Medeghini
# Licensed under the terms of the GNU General Public License version 2 (only).
# Source GUIDE:
# http://www.g-loaded.eu/2005/11/10/encrypt-devices-using-dm-crypt-and-luks/
# History: V1.0 2009/05/05
###############################################################################

if [ $# != 2 ]
then
echo Usage: CreateLuksDev.sh device dev_name
echo
echo device is in the form /dev/sdc1
echo dev_name is a string
exit 1
fi

echo DEVICE = $1
echo DEVICE_NAME = $2
echo FSTYPE = FAT32

echo Filling with random data... please wait. This step is slow
echo Approx 5 min per 1 Gb on hard disk
echo If you want to know the actual statei => kill -USR1 `pidof dd`
echo dd if=/dev/urandom of=$1
dd if=/dev/urandom of=$1

echo Creating luks device
cryptsetup --verbose --verify-passphrase luksFormat $1

echo Opening luks device
cryptsetup luksOpen $1 encrdev

echo Creating FAT32 filesystem
mkfs.vfat -v -F 32 -n "$2" /dev/mapper/encrdev

echo Mounting
mkdir /mnt/pen/
mount -t vfat -o rw /dev/mapper/encrdev /mnt/pen/

echo Device mounted on /mnt/pen/

echo "********************************************************"
echo "* TO MOUNT"
echo "* cryptsetup luksOpen $1 encrdev"
echo "* mount /dev/mapper/encrdev /mnt/pen"
echo "********************************************************"

echo "********************************************************"
echo "* TO UNMOUNT"
echo "* umount /mnt/pen/"
echo "* cryptsetup luksClose encrdev"
echo "********************************************************"
