Loopback Device: Difference between revisions

no edit summary
[unchecked revision][unchecked revision]
No edit summary
Line 101:
|}
That leaves us with a nice sized file full of zeros that we'll use for our disk image.
 
==== Mounting ====
 
Now we attach the file to a loopback device. This lets us treat the file as though it were a physical disk.
 
Note: Under a normal Linux setup you will need to be root to use the losetup command (The same applies to most commands we'll be using).
 
losetup /dev/loop0 /path/to/c.img
 
Explanation:
{| {{Wikitable}}
|-
| losetup
| Linux command to setup and control loop devices.
|-
| /dev/loop0
| Loopback device 0. This will become our raw 'disk' device.
|-
| /path/to/c.img
| File to attach to the the loopback device is our disk image.
|}
 
If you run ps ax, you should now see a [loop0] process.
 
==== Partitioning ====
Line 129 ⟶ 106:
Now to create the MBR and partition table on the disk image (Usually you need to be root).
 
fdisk -u -C#cylinders -S63 -H16 /devpath/loop0to/c.img
 
Explanation:
Line 149 ⟶ 126:
| Set the heads/track to 16.
|-
| /devpath/loop0to/c.img
| fdisk is capable of partitioning image files directly.
| Thanks to losetup this device represents our raw 'disk'.
|}
 
Line 162 ⟶ 139:
You should end up with a screen that looks something like this:
 
Disk /devpath/loop0to/c.img: 516 MB, 516096000 bytes
16 heads, 63 sectors/track, 1000 cylinders, total 1008000 sectors
Units = sectors of 1 * 512 = 512 bytes
 
Device Boot Start End Blocks Id System
/devpath/loop0p1to/c.img1 * 63 1007999 503968+ 83 Linux
 
Obviously the cylinder count, partition end and blocks will be different depending on the size of your image.
Line 183 ⟶ 160:
Unfortunately this also means that from here on out we have to account for the fact that our partition does not start at byte 0 of the image.
 
==== Mounting ====
==== Detach our disk image from the loopback device. ====
 
Command:
 
losetup -d /dev/loop0
 
Explanation:
{| {{Wikitable}}
|-
| -d
| Detach whatever is on the loopback device
|-
| /dev/loop0
| The virtual drive
|}
 
Ok, now we attach the file to the loopback device again, but in such a way that we skip everything before the start of our partition.
 
losetup -o32256 /dev/loop0 /path/to/c.img
Line 210 ⟶ 173:
|}
 
The reason we move 32256 bytes into the file is this is where the partition starts. Remember I said to note the start sector of the partition (63 is usual)? Well, since each sector is 512 bytes long we therefore know the starting byte of the partition is 32256 (63*512) bytes into the file. The reason behind this gap is that most (Therethere is no real standard) fdisk programs don't use the first track for anything but the MBR. That space isn't always wasted though, some bootloaders (Eg GRUB) use it to store parts of their program.
 
''Note: If you aren't using the suggested geometry then you'll have to calculate this for yourself.''
448

edits