losetup -P automation
losetup -P is the best method starting in Ubuntu 16.04 as mentioned at https://askubuntu.com/a/496576/52975 , here are functions to automate if further. Usage:
$ los my.img
/dev/loop0
/mnt/loop0p1
/mnt/loop0p2
$ ls /mnt/loop0p1
/whatever
/files
/youhave
/there
$ # Cleanup.
$ losd 0
$ ls /mnt/loop0p1
$ ls /dev | grep loop0
loop0
Source:
los() (
img="$1"
dev="$(sudo losetup --show -f -P "$img")"
echo "$dev"
for part in "$dev"?*; do
dst="/mnt/$(basename "$part")"
echo "$dst"
sudo mkdir -p "$dst"
sudo mount "$part" "$dst"
done
)
losd() (
dev="/dev/loop$1"
for part in "$dev"?*; do
dst="/mnt/$(basename "$part")"
sudo umount "$dst"
done
sudo losetup -d "$dev"
)
loop module max_part config
This is a decent method before 16.04.
loop is a kernel module, built into the kernel in Ubuntu 14.04.
If you configure it right, Linux automatically splits up the devices for you.
cat /sys/module/loop/parameters/max_part
says how many partitions loop devices can generate.
It is 0 by default on Ubuntu 14.04 which is why no auto-splitting happens.
To change it, we can either add:
options loop max_part=31
to a file in /etc/modprobe, or:
GRUB_CMDLINE_LINUX="loop.max_part=31"
to /etc/default/grub and then sudo update-grub.
How to set a module parameter is also covered at: How to add kernel module parameters?
After a reboot, when you do:
sudo losetup -f --show my.img
it mounts the image to a /dev/loopX device, and automatically mounts the partitions to /dev/loopXpY devices.
So this is the most convenient method if you are willing to reboot.
See also