Exploring .img Files on Linux and create your custom one

Enviado por keopx el Jue, 26/12/2019 - 11:35
  • Discover the hidden secrets of a monolithic .img file
  • Mount the partitions in an .img file using losetup, kpartx and mount
  • Create your own .img files and use them as virtual disks
  • Write out your virtual disk image to a thumb drive (or any drive for that matter) for use later

Install packages

$ sudo aptitude install kpartx xz-utils

Decompress image

Once its downloaded you will want to uncompress it with xz --decompress:

$ xz --decompress Retrobro128KillerHomeConsole.img.img.xz

Mount image

We’re going to attach the image file to what is known as a loopback device.

$ sudo losetup /dev/loop0 Retrobro128KillerHomeConsole.img

Note: We used /dev/loop0 in this example. If /dev/loop0 wasn’t available to us (that is, it was already in use), we could have chosen /dev/loop1, etc.

$ sudo losetup -a 
/dev/loop0: [65026]:28970069 (/home/keopx/Retrobro128KillerHomeConsole.img)

Now /dev/loop0 is attached. How about look at the partition table with fdisk?

$ sudo fdisk -l /dev/loop0
Disco /dev/loop0: 119,8 GiB, 128579534848 bytes, 251131904 sectores
Unidades: sectores de 1 * 512 = 512 bytes
Tamaño de sector (lógico/físico): 512 bytes / 512 bytes
Tamaño de E/S (mínimo/óptimo): 512 bytes / 512 bytes
Tipo de etiqueta de disco: dos
Identificador del disco: 0xf2d3cb4f

Disposit.    Inicio Comienzo     Final  Sectores Tamaño Id Tipo
/dev/loop0p1 *          8192    124927    116736    57M  e W95 FAT16 (LBA)
/dev/loop0p2          124928 251131903 251006976 119,7G 83 Linux

To see what kpartx would map, run it with the -l option:

$ sudo kpartx -l /dev/loop0
loop0p1 : 0 116736 /dev/loop0 8192
loop0p2 : 0 251006976 /dev/loop0 124928

Let’s go ahead and run it and add the maps:

$ sudo kpartx -a /dev/loop0 

Now you can see new devices on dolphin.

Running a shell:

Now that the partitions are mapped, let’s examine the filesystems on each partition with file and the --special-files and --dereference options.

$ sudo  file -sL /dev/mapper/loop0p1
/dev/mapper/loop0p1: DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 4, reserved sectors 4, root entries 512, Media descriptor 0xf8, sectors/FAT 116, sectors/track 32, heads 64, sectors 116736 (volumes > 32 MB), serial number 0xb13de1e5, label: "boot       ", FAT (16 bit)
$ sudo  file -sL /dev/mapper/loop0p2
/dev/mapper/loop0p2: Linux rev 1.0 ext4 filesystem data, UUID=c50e928e-5669-44d0-8e3c-a59d21ad3da2, volume name "retropie" (extents) (64bit) (large files)

Now that we have our partitions mapped, we can mount them. Create two directories to serve as mountpoints:

$ mkdir boot         # We will mount the FAT partition here
$ mkdir retropie     # We will mount the ext4 partition here

Once they are created, mount the filesystems (only system in my case).

$ sudo mount /dev/mapper/loop0p2 retropie

Umount image

Once you are done and want to “let go” of the .img file, reverse the process with:

$ sudo umount retropie
$ sudo kpartx -d /dev/loop0
$ sudo losetup -d /dev/loop0


Añadir nuevo comentario

El contenido de este campo se mantiene privado y no se mostrará públicamente.

HTML Restringido

  • Etiquetas HTML permitidas: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Saltos automáticos de líneas y de párrafos.
  • Las direcciones de correos electrónicos y páginas web se convierten en enlaces automáticamente.