Mounting filesystem images
From FBSD_tips
Contents |
[edit] Rationale and goals
Disk images that contain files systems within them have been around for quite some time on many platforms. It is quite a natural arrangement under unix, with it's "everything is a file" philosophy. So we will examine how we create and mount these images in this article.
[edit] Setup and use
[edit] UFS2 file system
STEP 1 : Create a file to put the file system in. This one is 1 MB.
dd if=/dev/zero of=ufs1.img bs=1M count=10000
Or alternately,
truncate -s 10G ufs1.img
STEP 2 : Attach it to an md device. The returned string is the device that got attached.
> mdconfig -a -t vnode -f ufs1.img md0
STEP 3 : Use slice 1 for the whole drive.
> fdisk -iB md0
******* Working on device /dev/md0 *******
parameters extracted from in-core disklabel are:
cylinders=1274 heads=255 sectors/track=63 (16065 blks/cyl)
Figures below won't work with BIOS for partitions not in cyl 1
parameters to be used for BIOS calculations are:
cylinders=1274 heads=255 sectors/track=63 (16065 blks/cyl)
Do you want to change our idea of what BIOS thinks ? [n]
fdisk: invalid fdisk partition table found
Media sector size is 512
Warning: BIOS sector numbering starts with sector 1
Information from DOS bootblock is:
The data for partition 1 is:
sysid 165 (0xa5),(FreeBSD/NetBSD/386BSD)
start 63, size 20466747 (9993 Meg), flag 80 (active)
beg: cyl 0/ head 1/ sector 1;
end: cyl 249/ head 254/ sector 63
Do you want to change it? [n]
The data for partition 2 is:
<UNUSED>
Do you want to change it? [n]
The data for partition 3 is:
<UNUSED>
Do you want to change it? [n]
The data for partition 4 is:
<UNUSED>
Do you want to change it? [n]
Partition 1 is marked active
Do you want to change the active partition? [n]
We haven't changed the partition table yet. This is your last chance.
parameters extracted from in-core disklabel are:
cylinders=1274 heads=255 sectors/track=63 (16065 blks/cyl)
Figures below won't work with BIOS for partitions not in cyl 1
parameters to be used for BIOS calculations are:
cylinders=1274 heads=255 sectors/track=63 (16065 blks/cyl)
Information from DOS bootblock is:
1: sysid 165 (0xa5),(FreeBSD/NetBSD/386BSD)
start 63, size 20466747 (9993 Meg), flag 80 (active)
beg: cyl 0/ head 1/ sector 1;
end: cyl 249/ head 254/ sector 63
2: <UNUSED>
3: <UNUSED>
4: <UNUSED>
Should we write new partition table? [n] y
STEP 4 : A standard label is written. This will put all the space into the 'a' partiion as well as make a 'c' (whole disk) partition.
> bsdlabel -wB /dev/md0s1
STEP 5 : A file system added to it:
> newfs /dev/md0s1a
/dev/md0s1a: 9993.5MB (20466728 sectors) block size 16384, fragment size 2048
using 55 cylinder groups of 183.77MB, 11761 blks, 23552 inodes.
super-block backups (for fsck -b #) at:
160, 376512, 752864, 1129216, 1505568, 1881920, 2258272, 2634624, 3010976, 3387328, 3763680, 4140032, 4516384, 4892736,
5269088, 5645440, 6021792, 6398144, 6774496, 7150848, 7527200, 7903552, 8279904, 8656256, 9032608, 9408960, 9785312,
10161664, 10538016, 10914368, 11290720, 11667072, 12043424, 12419776, 12796128, 13172480, 13548832, 13925184, 14301536,
14677888, 15054240, 15430592, 15806944, 16183296, 16559648, 16936000, 17312352, 17688704, 18065056, 18441408, 18817760,
19194112, 19570464, 19946816, 20323168
STEP 6 : Now mount the file system.
> mount /dev/md0s1a /tmpmnt > df /tmpmnt Filesystem 1K-blocks Used Avail Capacity Mounted on /dev/md0s1a 9907682 4 9115064 0% /tmpmnt
STEP 7 : Cleaning up. Once you are done with the filesystem, umount and detatch from the md device.
umount /tmpmnt mdconfig -d -u md0
Once the filesystem in a file is made, you only need steps 2 and 6 in the future to mount it again.
[edit] Expanding the image
You may find that the contents of the filesystem overflow the space you allotted. Thie is how you can grow the file system. First we will make an additional image to append to the UFS filesystem of 1 GB and attach it to an md device and find out how many additional sectors it contains with fdisk.
> dd if=/dev/zero of=ufs2.img bs=1M count=1000
> mdconfig -a -t vnode -f ufs
md1
> fdisk -i md1
******* Working on device /dev/md1 *******
parameters extracted from in-core disklabel are:
cylinders=127 heads=255 sectors/track=63 (16065 blks/cyl)
parameters to be used for BIOS calculations are:
cylinders=127 heads=255 sectors/track=63 (16065 blks/cyl)
Do you want to change our idea of what BIOS thinks ? [n]
fdisk: invalid fdisk partition table found
Media sector size is 512
Warning: BIOS sector numbering starts with sector 1
Information from DOS bootblock is:
The data for partition 1 is:
sysid 165 (0xa5),(FreeBSD/NetBSD/386BSD)
start 63, size 2040192 (996 Meg), flag 80 (active)
beg: cyl 0/ head 1/ sector 1;
end: cyl 126/ head 254/ sector 63
Do you want to change it? [n] ^C
> mdconfig -d -u 1
You can break out of fdisk at this point, all we needed was the number of sectors (2040192). Now extend the original image with the 2nd image, first detaching it from the md device.
> mdconfig -d -u 0 > cat ufs2.img >> ufs1.img
Now we will reattach the expanded image to an md device and add the sectors to fdisk and bsdlabel, then growfs into them.
> mdconfig -a -t vnode -f ufs1.img md0
The slice is extened to 24563259 (2040192 + 22523067 = 24563259) with fdisk.
> fdisk -i md0
******* Working on device /dev/md0 *******
parameters extracted from in-core disklabel are:
cylinders=1402 heads=255 sectors/track=63 (16065 blks/cyl)
Figures below won't work with BIOS for partitions not in cyl 1
parameters to be used for BIOS calculations are:
cylinders=1402 heads=255 sectors/track=63 (16065 blks/cyl)
Do you want to change our idea of what BIOS thinks ? [n]
fdisk: invalid fdisk partition table found
Media sector size is 512
Warning: BIOS sector numbering starts with sector 1
Information from DOS bootblock is:
The data for partition 1 is:
sysid 165 (0xa5),(FreeBSD/NetBSD/386BSD)
start 63, size 22523067 (10997 Meg), flag 80 (active)
beg: cyl 0/ head 1/ sector 1;
end: cyl 377/ head 254/ sector 63
Do you want to change it? [n] y
The static data for the slice 1 has been reinitialized to:
sysid 165 (0xa5),(FreeBSD/NetBSD/386BSD)
start 63, size 22523067 (10997 Meg), flag 80 (active)
beg: cyl 0/ head 1/ sector 1;
end: cyl 377/ head 254/ sector 63
Supply a decimal value for "sysid (165=FreeBSD)" [165]
Supply a decimal value for "start" [63]
Supply a decimal value for "size" [22523067] 24563259
Explicitly specify beg/end address ? [n]
sysid 165 (0xa5),(FreeBSD/NetBSD/386BSD)
start 63, size 24563259 (11993 Meg), flag 80 (active)
beg: cyl 0/ head 1/ sector 1;
end: cyl 504/ head 253/ sector 63
Are we happy with this entry? [n] y
The data for partition 2 is:
<UNUSED>
Do you want to change it? [n]
The data for partition 3 is:
<UNUSED>
Do you want to change it? [n]
The data for partition 4 is:
<UNUSED>
Do you want to change it? [n]
Partition 1 is marked active
Do you want to change the active partition? [n]
We haven't changed the partition table yet. This is your last chance.
parameters extracted from in-core disklabel are:
cylinders=1402 heads=255 sectors/track=63 (16065 blks/cyl)
Figures below won't work with BIOS for partitions not in cyl 1
parameters to be used for BIOS calculations are:
cylinders=1402 heads=255 sectors/track=63 (16065 blks/cyl)
Information from DOS bootblock is:
1: sysid 165 (0xa5),(FreeBSD/NetBSD/386BSD)
start 63, size 24563259 (11993 Meg), flag 80 (active)
beg: cyl 0/ head 1/ sector 1;
end: cyl 504/ head 253/ sector 63
2: <UNUSED>
3: <UNUSED>
4: <UNUSED>
Should we write new partition table? [n] y
Now we will expand the BSD disk label within the slice by 2040192 sectors as well, to
> bsdlabel -e /dev/md0s1
From :
# /dev/md0s1: 8 partitions: # size offset fstype [fsize bsize bps/cpg] a: 20466731 16 4.2BSD 2048 16384 28552 c: 20466747 0 unused 0 0 # "raw" part, don't edit
To :
# /dev/md0s1: 8 partitions: # size offset fstype [fsize bsize bps/cpg] a: 22506923 16 4.2BSD 2048 16384 28552 c: 22506923 0 unused 0 0 # "raw" part, don't edit
Now, we growfs the file system into the newly expanded label.
> growfs /dev/md0s1a We strongly recommend you to make a backup before growing the Filesystem Did you backup your data (Yes/No) ? Yes new file systemsize is: 5626730 frags Warning: 302152 sector(s) cannot be allocated. growfs: 10842.2MB (22204768 sectors) block size 16384, fragment size 2048 using 59 cylinder groups of 183.77MB, 11761 blks, 23552 inodes. super-block backups (for fsck -b #) at: 20699520, 21075872, 21452224, 21828576
And mount the expanded file system to see that it is indeed bigger:
> mount /dev/md0s1a /tmpmnt > df /tmpmnt Filesystem 1K-blocks Used Avail Capacity Mounted on /dev/md0s1a 10753022 442238 9450544 4% /tmpmnt
And we see that it is indeed about 1 GB larger.
[edit] ISO9660 (CDRom) filesystem
STEP 1 : Make or otherwise obtain an iso9660 file system. The port cdrtools is nessesary for this step. Options here are : -R rockridge extensions, -J joliet extensions, -U allow all manner of 'illegal' filefilenames, -D don't relocate deep directory trees and -o writes to the .iso file.
mkisofs -R -J -U -D -o temp.iso ./temp/
This set of options makes for a VERY non standard CD image, but it conveys a more unix-ey file system to the host, and it mounts fine on freebsd. The other option is to download a CD image like the a FreeBSD install cd.
fetch ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/ISO-IMAGES/6.3/6.3-RELEASE-i386-disc1.iso
STEP 2 : Connect the image to an md device.
> mdconfig -a -t vnode -f 6.3-RELEASE-i386-disc1.iso md0
STEP 3 : Mount the device.
mount -t cd9660 /dev/md0 /tmpmnt
And we see that nin fact the device is mounted.
> df /tmpmnt Filesystem 1K-blocks Used Avail Capacity Mounted on /dev/md0 639718 639718 0 100% /tmpmnt
[edit] Discussion
Using file backed vnodes is a bit more difficult than using real hardware, as the hardware provides much more information regarding geometry than the vnodes can or do provide.
The handbook has a section on mounting file backed disk image here, but it does not use a slice, rather putting the bsd label on the whole disk (aka "dangerously dedicated"). I wanted to illustrate putting the bsd label in a slice (or DOS partition).
If one was going to use a file backed UFS filer system for jails for example, using geom labels might be a very good idea to keep the partitions matched up to the jail it belongs to. A point to remember tho is that geom labels are at the end of the partition so if you expand file system into newly added space, reapplying the geom label may be nessesary.
I also datached the image form the md device whenever I altered it it's size. I don't know if the md device would track addition to it after attaching to the file.
