losetup -P automation
Method mentioned by https://superuser.com/a/684707/128124 (added in util-linux v2.21, added Ubuntu 16.04) , here are functions to automate it further. Usage:
$ los my.img
/dev/loop0
/mnt/loop0p1
/mnt/loop0p2
$ ls /mnt/loop0p1
/whatever
/files
/youhave
/there
$ sudo losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO
/dev/loop1 0 0 0 0 /full/path/to/my.img
$ # 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
if [ "$part" = "${dev}p*" ]; then
part="${dev}"
fi
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
if [ "$part" = "${dev}p*" ]; then
part="${dev}"
fi
dst="/mnt/$(basename "$part")"
sudo umount "$dst"
done
sudo losetup -d "$dev"
)
loop module max_part config
Decent method before util-linux v2.21.
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: https://askubuntu.com/questions/51226/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