Mounting a LUKS encrypted physical medium is a straightforward enough process, per the man page for cryptsetup:
cryptsetup open --type luks /dev/loop0 mapped-name
mount /dev/mapper/mapped-name /mnt/location
Note while the man page encourages using the --type luks flag the format will typically be autodetected when omitted.
The same syntax may be used to mount a straight partition image. Things become a little more complicated however when dealing with a full disk image containing multiple partitions, as we are not able to specify partitions with the same ease that a /dev block device avails.
Those familiar with administrating Xen virtualized environs will no doubt be accustomed to using the lomount binary that comes with the distribution for gaining easy access to partitions contained in full-disk images, yet one may find one's self in a position where default repositories don't provide a simple means of installing this tool, or it might necessitate installing a full Xen implementation that may be intrusive and certainly top-heavy in storage-constrained circumstances. I encountered such a situation using a fresh installation of Kali Linux.
One may take one's chances clunkily setting up loop devices with losetup and its -o|--offset flag, but fortunately a shell-scripted drop-in has been published by Pádraig Brady at http://www.pixelbeat.org/scripts/lomount.sh:
#!/bin/sh
# Mount partitions within a disk image file
# License: LGPLv2
# Author: [email protected]
# V1.0 29 Jun 2005 Initial release
# V1.1 01 Dec 2005 Handle bootable (DOS) parititons
# v1.2 25 Jan 2013 Glen Gray: Handle GPT partitions
if [ "$#" -ne "3" ]; then
echo "Usage: `basename $0` <image_filename> <partition # (1,2,...)> <mount point>" >&2
exit 1
fi
FILE=$1
PART=$2
DEST=$3
if parted --version >/dev/null 2>&1; then # Prefer as supports GPT partitions
UNITS=$(parted -s $FILE unit s print 2>/dev/null | grep " $PART " |
tr -d 's' | awk '{print $2}')
elif fdisk -v >/dev/null 2>&1; then
UNITS=$(fdisk -lu $FILE 2>/dev/null | grep "$FILE$PART " |
tr -d '*' | awk '{print $2}')
else
echo "Can't find the fdisk or parted utils. Are you root?" >&2
exit 1
fi
OFFSET=`expr 512 '*' $UNITS`
mount -o loop,offset=$OFFSET $FILE $DEST
This is an excellent, almost drop-in solution for mounting standard-issue partitions residing in disk image files, however it is not intended for dealing with encrypted volumes. As such, using it as a starting point I have made some modifications:
#!/bin/sh
# Mount encrypted partitions within a disk image file
# License: LGPLv2
# Original Author: [email protected]
# Encryption added by: karma @ https://foxpa.ws/mounting-luks-encrypted-volumes
# V1.0 29 Jun 2005 Initial release
# V1.1 01 Dec 2005 Handle bootable (DOS) parititons
# v1.2 25 Jan 2013 Glen Gray: Handle GPT partitions
# v2.0 07 Jun 2023 karma @ foxpa.ws: Modified for LUKS encrypted volumes
if [ "$#" -ne "5" ]; then
echo "Usage: `basename $0` <image_filename> <partition # (1,2,...)> </dev/loopX> <dm-name> <mount point>" >&2
echo "\n [*] Run losetup -a first to determine free loop devices (i.e. compare assigned loops to available nodes at /dev/loop[0,1,...])" >&2
echo "\t [*] Provide a user-friendly label for dm-name where the volume will be mapped to under /dev/mapper/label" >&2
echo "\n You will be interactively prompted to provide a passphrase; often this is a string of random data that has itself been gpg-encrypted with a passphrase to allow for altering the password used to authenticate without changing the so-called master passphrase which would necessitate re-encrypting the entire volume. Use gpg -d keyfile if this applies to you." >&2
echo "\n To tear down and free the loop device after umounting your volume, run: cryptsetup close dm-name before disconnecting/ejecting or otherwise removing access to the image." >&2
exit 1
fi
FILE=$1
PART=$2
LOOP=$3
NAME=$4
DEST=$5
if parted --version >/dev/null 2>&1; then # Prefer as supports GPT partitions
UNITS=$(parted -s $FILE unit s print 2>/dev/null | grep " $PART " |
tr -d 's' | awk '{print $2}')
elif fdisk -v >/dev/null 2>&1; then
UNITS=$(fdisk -lu $FILE 2>/dev/null | grep "$FILE$PART " |
tr -d '*' | awk '{print $2}')
else
echo "Can't find the fdisk or parted utils. Are you root?" >&2
exit 1
fi
OFFSET=`expr 512 '*' $UNITS`
losetup -o $OFFSET $LOOP $FILE
cryptsetup open --type luks $LOOP $NAME
mount /dev/mapper/$NAME $DEST