Skip to content

Instantly share code, notes, and snippets.

@bedekelly
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bedekelly/4d4a66c98bb4b0caa261 to your computer and use it in GitHub Desktop.
Save bedekelly/4d4a66c98bb4b0caa261 to your computer and use it in GitHub Desktop.
Create a session to mount encrypted files and unmount on exit
# access_crypt.py
# Makes using encrypted storage easier.
# Mounts the storage, opens a shell, and unmounts when the shells closes (however ungracefully)
# Works under Ubuntu 14.04, following these instructions to set up your encrypted storage file:
# https://www.digitalocean.com/community/tutorials/how-to-use-dm-crypt-to-create-an-encrypted-volume-on-an-ubuntu-vps
from contextlib import contextmanager
from pysh import sh
FILE_LOCATION = "Your encrypted storage file's location on disk"
MOUNT_NAME = "The name you'd like to give your mounted storage system"
@contextmanager
def mount_volume():
try:
sh.cryptsetup("luksOpen", FILE_LOCATION, MOUNT_NAME)
sh.mount("/dev/mapper/{}".format(MOUNT_NAME), "/mnt/{}".format(MOUNT_NAME))
yield
finally:
sh.umount("/mnt/{}".format(MOUNT_NAME))
sh.cryptsetup("luksClose", MOUNT_NAME)
def main():
with mount_volume():
sh.bash()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment