losetup /dev/loop0 /path/to/your/image.img
losetup -d /dev/loop0
Everything that you will (probably) need is here: http://libguestfs.org
If you happen to need something that isn't already included, the APIs make it pretty easy to build your own tools as well.
Conveniently, those tools are also already packaged up for your distro (unless you use some esoteric distribution, perhaps).
Wonder why kpartx exists; will have to look into that. Maybe not everything can be partprobed.
I might be mis-remebering but I think parted used to be considered lame before fdisk lagged to support GPT.
I don’t like how parted will automatically commit your changes as you go, though this is useful for scripting your changes and is my preferred method for disk geometry modifications when preparing maintenances etc
Right tool for the job and all that.
From what I remember partprobe has had a history of occasional unreliable behaviour under RHEL - but I understand it works properly under RHEL 7.3+
A lot of StackOverflow questions have answers that use kpartx, and those answers are out of date.
# losetup -Pf /path/to/disk.img
# mount /dev/loop0pX /mnt
# vnconfig vnd0 install63.fs
# disklabel vnd0
disk: vnd device
total sectors: 738240
# size offset fstype [fsize bsize cpg]
a: 736256 1024 4.2BSD 2048 16384 16142
c: 738240 0 unused
i: 960 64 MSDOS
# mount /dev/vnd0a /mnt
# mdconfig -f smartos-latest-USB.img
# gpart list md0
1. Name: md0s1
# mount -o ro -t msdosfs /dev/md0s1 /mnt
// (or, use labels)
# mount -o ro -t msdosfs /dev/msdosfs/SMARTOSBOOT /mnt
# mount -t cd9660 /dev/$(mdconfig -f image.iso) /mnt
Apparently that's been a little while, however. According to vnconfig(8):
> The mdconfig utility first appeared in FreeBSD 5.0 as a cleaner replacement for the vn(4) and vnconfig(8) combo.
5.0 was released just over 15 years ago.
Despite reading all of the documentation I could find, and even going so far as to read kernel source code, I could never get the system to recognize my striped block data. That is, I tried to generate three files composing a RAID-5 "by hand", but mdadm would refuse to mount them. I never figured out if there was some additional id-block I was missing, or if my striping algorithm was not correct.
$ guestfish -a img.dd -m /dev/sda1
><fs> ll /
# fallocate -l 256M disk.bin
# zpool create filepool /root/disk.bin
# zfs set mountpoint=/root/filepool filepool
# df -h /root/filepool
Filesystem Size Used Avail Use% Mounted on
filepool 208M 0 208M 0% /root/filepool
> sudo kpartx -a test.raw
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 4G 0 loop
└─loop0p1 253:0 0 4G 0 part
> sudo kpartx -d test.raw
> mount -t -o ro,offset=$((51296390)) x.dd partition_2
Isn't the mount type "-t" value missing?
I.E. libguestfs is safer when dealing with images of unknown origin, and losetup has inbuilt support now for partition scanning.
Here's an old fdisk/parted wrapper I used to use:
How about setting a boot flag and writing it back to e.g. an USB stick?
~# dd if=/dev/zero of=image.img bs=1M count=100
~# LODEVICE=$(losetup --find --show image.img)
~# fdisk $LODEVICE
~# losetup -d $LODEVICE
edit: this page explains using offsets for mke2fs and doesn't require losetup: https://superuser.com/questions/737072/userspace-manipulatio...
also, losetup can now scan the device for partition tables and make partition-specific loopbacks! https://stackoverflow.com/a/15200862
That was before I found multistrap.
fallocate -l 1G disk.img