If you've got an image of a file system or an ISO, you can generally do something like
# mount /path/to/image /mnt/image
(No need to run a separate loop command or even pass -o loop these days, and the filesystem type (-t fstype) can usually be auto-detected.)
And then access it as normal
You can create a filesystem image by the following
$ dd if=/dev/zero of=fs.img count=0 bs=1 seek=5G $ /sbin/mkfs.xfs fs.imgi.e
$ dd if=/dev/zero of=fs.img count=0 bs=1 seek=5G 0+0 records in 0+0 records out 0 bytes (0 B) copied, 0.000133533 s, 0.0 kB/s $ /sbin/mkfs.xfs fs.img meta-data=fs.img isize=256 agcount=4, agsize=327680 blks = sectsz=512 attr=2, projid32bit=0 data = bsize=4096 blocks=1310720, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 $ ls -ohs fs.img 11M -rw-r--r-- 1 andrew 5.0G Feb 1 13:19 fs.img $ file fs.img fs.img: SGI XFS filesystem data (blksz 4096, inosz 256, v2 dirs)
What we did was create a 5GB sparse file and then put a XFS filesystem on it.
As you can see even though the file seems to be 5GB in size it is actually only using 11MB of space.
We can then mount that image and use it as we would any other filesystem.
$ sudo mount fs.img /mnt/img $ df -Th /mnt/img Filesystem Type Size Used Avail Use% Mounted on /dev/loop0 xfs 5.0G 33M 5.0G 1% /mnt/img $ sudo cp -a /etc /mnt/img/ $ df -Th /mnt/img Filesystem Type Size Used Avail Use% Mounted on /dev/loop0 xfs 5.0G 67M 5.0G 2% /mnt/img $ sudo umount /mnt/img
If you have an image of a disk (with partitions) like
# fdisk -l /opt/vm/windows_xp.img You must set cylinders. You can do this from the extra functions menu. Disk /opt/vm/windows_xp.img: 0 MB, 0 bytes 255 heads, 63 sectors/track, 0 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xbdcbbdcb Device Boot Start End Blocks Id System /opt/vm/windows_xp.img1 * 1 621 4988151 b W95 FAT32
That's an image containing a windows xp install
You can then do
# kpartx -a /opt/vm/windows_xp.img
This will create files under /dev/mapper representing the image disk partitions.
# ls -l /dev/mapper total 0 crw-rw---- 1 root root 10, 60 2008-06-03 15:40 control brw-rw---- 1 root disk 253, 0 2008-06-04 15:43 loop0p1
You can mount this as normal
# mount /dev/mapper/loop0p1 /mnt/image # ls -l /mnt/image total 327540 -rwxr-xr-x 1 root root 0 2008-05-21 17:03 autoexec.bat -rwxr-xr-x 1 root root 211 2008-05-21 16:18 boot.ini -rwxr-xr-x 1 root root 0 2008-05-21 17:03 config.sys drwxr-xr-x 7 root root 4096 2008-05-21 15:13 Documents and Settings -rwxr-xr-x 1 root root 133746688 2008-06-02 15:21 hiberfil.sys -r-xr-xr-x 1 root root 0 2008-05-21 17:03 io.sys -r-xr-xr-x 1 root root 0 2008-05-21 17:03 msdos.sys -r-xr-xr-x 1 root root 47564 2004-08-04 13:00 ntdetect.com -r-xr-xr-x 1 root root 250032 2004-08-04 13:00 ntldr -rwxr-xr-x 1 root root 201326592 2008-06-02 15:21 pagefile.sys dr-xr-xr-x 18 root root 4096 2008-05-21 16:54 Program Files drwxr-xr-x 3 root root 4096 2008-05-22 09:26 System Volume Information drwxr-xr-x 39 root root 8192 2008-05-21 14:34 windows
When finished
# umount /mnt/image # kaprtx -d /opt/vm/windows_xp.img
We can create our own partitioned image with a filesystem like
$ dd if=/dev/zero of=fs.img count=0 bs=1 seek=5G 0+0 records in 0+0 records out 0 bytes (0 B) copied, 0.000695086 s, 0.0 kB/s $ fdisk fs.img # Create one or more partitions $ fdisk -l fs.img Disk fs.img: 5 GiB, 5368709120 bytes, 10485760 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x731179da Device Boot Start End Blocks Id System fs.img1 2048 10485759 5241856 83 Linux $ sudo kpartx -a fs.img $ ls -l /dev/mapper total 0 crw------- 1 root root 10, 236 Feb 1 13:51 control lrwxrwxrwx 1 root root 7 Feb 1 14:00 loop0p1 -> ../dm-0 $ sudo mkfs.xfs /dev/dm-0 meta-data=/dev/dm-0 isize=256 agcount=4, agsize=327616 blks = sectsz=512 attr=2, projid32bit=0 data = bsize=4096 blocks=1310464, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 $ sudo mount /dev/dm-0 /mnt/img $ df -Th /mnt/img Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/loop0p1 xfs 5.0G 33M 5.0G 1% /mnt/img $ sudo umount /mnt/img $ sudo kpartx -d fs.img loop deleted : /dev/loop0