-
-
Save jadedgnome/9f3763fe1eff3b41a310 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#This is free and unencumbered software released into the public domain. | |
#Because this script needs root to do some things, excessive amounts of | |
#checking have been done to terminate the script if anything fails. | |
#Read all instructions very carefully. When it asks to type "yes", it must be | |
#all uppercase or it will fail. | |
#Don't try to use absolute paths, it will probably break things. | |
#Make sure cryptsetup is installed | |
cryptCheck(){ | |
command -v cryptsetup > /dev/null | |
return "${PIPESTATUS[0]}" | |
} | |
#check to see if loop is loaded | |
loopCheck(){ | |
grep -e "^loop" /proc/modules > /dev/null | |
return "${PIPESTATUS[0]}" | |
} | |
#check to see if ext4 is loaded | |
ext4Check(){ | |
grep -e "^ext4" /proc/modules > /dev/null | |
return "${PIPESTATUS[0]}" | |
} | |
#Create new container. | |
createContainer(){ | |
if [[ ! "$2" =~ ^-?[0-9]+$ ]] | |
then | |
echo "second number should be a number of bytes" | |
echo "example: createContainer CONTAINER_NAME 128" | |
return 1 | |
fi | |
cryptCheck | |
if [[ ! $? == 0 ]] | |
then | |
echo "cryptsetup is not installed" | |
return 1 | |
fi | |
loopCheck | |
if [[ ! $? == 0 ]] | |
then | |
echo "loop kernel module not loaded" | |
echo "run: sudo modprobe loop" | |
return 1 | |
fi | |
ext4Check | |
if [[ ! $? == 0 ]] | |
then | |
echo "ext4 kernel module not loaded" | |
echo "try restarting the machine" | |
return 1 | |
fi | |
if [[ -f "$1" ]] | |
then | |
echo "The file ""$1"" already exists, pick a different filename" | |
return 1 | |
fi | |
echo "please wait... files over a few megabytes might take awhile" | |
dd if=/dev/zero of="$PWD"/"$1" bs=1M count="$2" | |
if [[ ! $? == 0 ]] | |
then return 1 | |
fi | |
LOOPDEV=$(losetup -f) | |
sudo losetup "$LOOPDEV" "$PWD"/"$1" | |
if [[ ! $? == 0 ]] | |
then return 1 | |
fi | |
sudo cryptsetup \ | |
--verbose \ | |
--verify-passphrase \ | |
--iter-time 4000 \ | |
luksFormat "$LOOPDEV" | |
if [[ ! $? == 0 ]] | |
then return 1 | |
fi | |
MAPNAME=""$1"MAP" | |
sudo cryptsetup luksOpen "$LOOPDEV" "$MAPNAME" | |
sudo mkfs.ext4 "/dev/mapper/"$MAPNAME"" | |
if [[ ! $? == 0 ]] | |
then return 1 | |
fi | |
sudo cryptsetup luksClose "$MAPNAME" | |
if [[ ! $? == 0 ]] | |
then | |
echo "failed to close "$MAPNAME"" | |
return 1 | |
fi | |
sudo losetup --detach "$LOOPDEV" | |
if [[ ! $? == 0 ]] | |
then return 1 | |
fi | |
} | |
#mount container | |
mountContainer(){ | |
LOOPDEV=$(losetup -f) | |
MAPNAME=""$1"MAP" | |
sudo losetup "$LOOPDEV" "$1" | |
if [[ ! $? == 0 ]] | |
then return 1 | |
fi | |
sudo cryptsetup luksOpen "$LOOPDEV" "$MAPNAME" | |
if [[ ! $? == 0 ]] | |
then return 1 | |
fi | |
CRYPTDIR=""$HOME"/PRIVATEDIRECTORY/"$MAPNAME"" | |
mkdir -p "$CRYPTDIR" | |
sudo mount --options uid=$UID /dev/mapper/"$MAPNAME" "$CRYPTDIR" | |
} | |
#unmount container | |
#The arguement used here should be the exact name of the container file that | |
#is mounted. | |
#example: unmountContainer CONTAINERFILE | |
unmountContainer(){ | |
MAPNAME=""$1"MAP" | |
sudo umount ""$HOME"/PRIVATEDIRECTORY/"$MAPNAME"" | |
if [[ ! $? == 0 ]] | |
then | |
echo "failed to unmount "$1"" | |
return 1 | |
fi | |
LOOPDEV=$(losetup -a | grep "$1" | cut -c -10) | |
sudo cryptsetup luksClose "$MAPNAME" | |
if [[ ! $? == 0 ]] | |
then return 1 | |
fi | |
sudo losetup -d "$LOOPDEV" | |
if [[ ! $? == 0 ]] | |
then return 1 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment