For fun, I wanted to create a custom /etc/fstab mount script to mount an encrypted disk with my own parameters. This is more a wrapper around the standard cryptsetup command than anything, but hopefully illustrates the ability to customize mount operations.

The mount script is installed at /usr/bin/mount.custom-mount and is executable.

#!/usr/bin/env python3

import sys
import subprocess

device = sys.argv[1]
mountpoint = sys.argv[2]
options = sys.argv[4]

name = ''
password = ''

standard_options=[]

for option in options.split(','):
    tokenized = option.split('=')

    if len(tokenized) == 2:
        if tokenized[0] == 'name':
            name = tokenized[1]
            continue
        elif tokenized[0] == 'password':
            password = tokenized[1]
            continue

    standard_options.append(option)

if not name:
    print('name of dev mapper not specified in options')
    sys.exit(1)

output = subprocess.run(
    ["/usr/bin/cryptsetup", "open", device, name, "-"],
    input="{}\n".format(password),
    encoding='ascii',
    capture_output=True
)

if output.returncode != 0 and output.stderr != "Device {} already exists.\n".format(name):
    print("error unlocking")
    print(output.stderr)
    sys.exit(output.returncode)

mount_command = ["mount"]
if len(standard_options) > 0:
    mount_command += ['-o', ','.join(standard_options)]
mount_command += ["/dev/mapper/{}".format(name), mountpoint]

output = subprocess.run(
    mount_command,
    capture_output=True
)

if output.returncode != 0 and output.stderr.decode('UTF-8') != "mount: {}: /dev/mapper/{} already mounted on {}.\n".format(mountpoint, name, mountpoint):
    print("error mounting")
    print(output.stderr.decode('UTF-8'))
    sys.exit(output.returncode)

Here I mount the disk by UUID to /mymount using my custom-mount script and a mix of standard fstab parameters and my custom name and password parameters. The name will be the /dev/mapper/name and the password here is my password! in plain text that will be used with cryptsetup.

UUID=2664fce5-2277-446b-b2c2-d3b97f86f0aa    /mymount    custom-mount    ro,nofail,name=mymount,password=my\040password!    0 0