Last active
August 29, 2015 14:12
-
-
Save bedekelly/4d4a66c98bb4b0caa261 to your computer and use it in GitHub Desktop.
Create a session to mount encrypted files and unmount on exit
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
# 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