Linux creating loop device

Loopback Device

A Loopback Device is a mechanism used to interpret files as real devices. The main advantage of this method is that all tools used on real disks can be used with a loopback device.

Note: This article only covers UNIX environments (including Cygwin). For information on how to use loopback devices on Windows, see diskpart.

Contents

  • 1 Loopback Device under Linux
    • 1.1 Floppy Disk Images With GRUB and EXT2
    • 1.2 Floppy Disk Images With FAT16
    • 1.3 Floppy Disk Images With FAT12
    • 1.4 Hard Disk Images
      • 1.4.1 Creating an image
      • 1.4.2 Partitioning
      • 1.4.3 Mounting
      • 1.4.4 Formatting the partition
      • 1.4.5 Mount Partition
      • 1.4.6 Unmount, Detach
      • 1.4.7 Making it Easier
      • 1.4.8 The End

      Loopback Device under Linux

      The linux loopback device can be used by root only, and needs to be enabled in the kernel before use.

      Floppy Disk Images With GRUB and EXT2

      First, lets create an empty image.

      dd if=/dev/zero of=floppy.img bs=512 count=2880

      Now, lets set it up for mounting.

      losetup /dev/loop0 floppy.img

      Now lets make it EXT2 formatted.

      mount -t ext2 /dev/loop0 /mnt/myfloppy
      cd /mnt/myfloppy mkdir grub

      Copy GRUB’s second stage files. (GRUB stage[12] could also be located in /usr/lib/grub/)

      cp /lib/grub/i386-pc/stage[12] /mnt/myfloppy/grub

      Create a device mapping for the GRUB installation. You need quotations around the first part.

      echo "(fd0) /dev/loop0" > /mnt/myfloppy/grub/device.map

      Start GRUB console for installation into the boot record.

      grub --device-map=/mnt/myfloppy/grub/device.map /dev/loop0

      NOTE: You must unmount /mnt/myfloppy before using a emulator to directly read /dev/loop0, such as:

      NOTE: When deleting the loop device, the original floppy.img file will be saved with the modified contents.

      Floppy Disk Images With FAT16

      dd if=/dev/zero of=floppy.img bs=512 count=2880
      losetup /dev/loop0 floppy.img
      mount -t msdos /dev/loop0 /mnt/myfloppy

      Floppy Disk Images With FAT12

      The steps mentioned below will be useful for the BrokenThorn Entertainment tutorials

      dd if=/dev/zero of=floppy.img bs=512 count=2880
      losetup /dev/loop0 floppy.img
      mount /dev/loop0 /mnt -t msdos -o "fat=12"

      Hard Disk Images

      A hard disk image contains an MBR, then a number of partitions, but the ‘mount’ instruction in Linux works with disk partitions, not full disks. To mount a partition contained in our disk image, we need to make sure the ‘mount’ command only sees our partition, not the whole disk.

      Creating an image

      First create the empty file that we will use for our disk image. We will assume a disk geometry of #cylinders, 16 heads, 63 sectors/track, 512 bytes/sector, which means that each cylinder contains 516096 bytes (16*63*512). Decide how large you want your disk image to be, and choose an appropriate number of cylinders (I’ll be using #cylinders throughout).

      Example: If I want a 500Mb disk, I would choose 1000 cylinders (approximation of (500*1000*1024)/516096).

      Write the disk image (I’ll assume the filename c.img throughout):

      dd if=/dev/zero of=/path/to/c.img bs=516096c count=#cylinders

      >>»>

      fdisk Linux DOS partition maintenance program.
      -u Display units in sectors not cylinders (We will need this).
      -C#cylinders Set the cylinders of disk to our value.
      -S63 Set the sectors/track to 63.
      -H16 Set the heads/track to 16.
      /path/to/c.img fdisk is capable of partitioning image files directly.

      Within fdisk use the following commands:

      o - Create a new empty DOS partition table. n - Create a new partition (For simplicity just make 1 primary partition covering the whole disk). a - Toggle the bootable flag (Optional). p - Print the partition table.

      You should end up with a screen that looks something like this:

      Disk /path/to/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 /path/to/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.

      Make a note of the start sector (63 here) and the block count (503968 here).

      Note: If you are intending to format the partition to something other than ext2fs then change the partition id here using the t command. I should also point out that disk manufacturers and programmers don’t agree on how many bytes are in a megabyte.

      w - Write partition table to our 'disk' and exit.

      Ignore any errors about rereading the partition table. Since it’s not a physical device we really don’t care.

      We now have a partition table on our disk image.

      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

      Ok, now we attach the file to the loopback device, in such a way that we skip everything before the start of our partition.

      losetup -o32256 /dev/loop0 /path/to/c.img

      >>»>

      mke2fs Create an EXT2 filesytem
      -b1024 Use block size of 1024
      /dev/loop0 Device to make the filesystem on (Here /dev/loop0 is our ‘partition’)
      #blocks Remember I said to note the number of blocks from the fdisk section? This is why.

      This gives us a clean EXT2 formatted partition.

      Note: mke2fs is smart enough to figure out block size and #blocks for itself, but if you ever want to use multiple partitions you’ll need to know how to use those values.

      mkdosfs -F32 /dev/loop0 #blocks

      >>»>

      mount Linux command to mount a filesystem
      -text2 / -tvfat Filesystem being used, Linux can usually figure this out on its own.
      /dev/loop0 The device representing our partition
      /mnt/wherever A directory to mount the partition on.

      This should leave you with a nicely mounted partition. If you run df -Th you should end up with a line similar to:

      Filesystem Type Size Used Avail Use% Mounted on /dev/loop0 vfat 492M 4.0K 492M 1% /mnt/wherever
      Filesystem Type Size Used Avail Use% Mounted on /dev/loop0 ext2 477M 13K 452M 1% /mnt/wherever

      (Yup, these are for the same disk image. By default ext2fs reserves/uses quite a bit of space even empty.)

      Unmount, Detach

      Ok, unmount the partition and detach the loopback device.

      umount /dev/loop0 losetup -d /dev/loop0

      Источник

      Читайте также:  How to run python script on linux
      Оцените статью
      Adblock
      detector