[HowTo] – Cómo montar en Linux particiones contenidas dentro de imágenes de disco.

Ahora si la explicación larga:

Lo que sigue es todo consola de comandos y nivel medio por lo que no me voy a detener demasiado a explicarte con lujo de detalles todos los comandos que no estén directamente involucrados con la finalidad de este post: cómo usar kpartx en Linux.

Para el ejemplo voy a crear un archivo nuevo de 1 GB de tamaño que voy a particionar en tres: 200 MB, 300 MB y 500 MB. La sintaxis de los comandos es auto-explicativa. En negrita lo que voy tipeando:

# Crear un archivo de 1GB:
malditonerd@Desktop:~# fallocate -l 1G imagen.bin


# Particionarlo con fdisk:
malditonerd@Desktop:~# fdisk imagen.bin 

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xa4477aba.

Command (m for help): n

Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2097151, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-2097151, default 2097151): +200M

Created a new partition 1 of type 'Linux' and of size 200 MiB.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (411648-2097151, default 411648):      
Last sector, +sectors or +size{K,M,G,T,P} (411648-2097151, default 2097151): +300M

Created a new partition 2 of type 'Linux' and of size 300 MiB.

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): <enter>
First sector (1026048-2097151, default 1026048): <enter>
Last sector, +sectors or +size{K,M,G,T,P} (1026048-2097151, default 2097151): <enter>

Created a new partition 3 of type 'Linux' and of size 523 MiB.

Command (m for help): w
The partition table has been altered.
Syncing disks.

En este punto ya tengo el equivalente a una imagen de disco con tres particiones. Se puede verficar el contenido muy fácilmente ejecutando:

malditonerd@Desktop:~# fdisk -l imagen.bin

Que devolverá esta salida listando las tres particiones:

Disk imagen.bin: 1 GiB, 1073741824 bytes, 2097152 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: 0xa4477aba

Device      Boot   Start     End Sectors  Size Id Type
imagen.bin1         2048  411647  409600  200M 83 Linux
imagen.bin2       411648 1026047  614400  300M 83 Linux
imagen.bin3      1026048 2097151 1071104  523M 83 Linux

Con lo que únicamente restaría conocer cómo las identificará kpartx al momento de generar los dispositivos que luego utilizaremos en /dev/mapper. Para ello utilizar el siguiente comando. (Según la configuración de tu computadora y suponiendo que no sos root, podrías necesitar anteponer un sudo delante del comando kpartx):

malditonerd@Desktop:~# kpartx -l imagen.bin
loop30p1 : 0 409600 /dev/loop30 2048
loop30p2 : 0 614400 /dev/loop30 411648
loop30p3 : 0 1071104 /dev/loop30 1026048
loop deleted : /dev/loop30

En el ejemplo de arriba, en mi computadora ya mismo, ejecutar kpartx -a -v sobre la imagen de disco imagen.bin como explicaba al principio de este post de mierda, generará en /dev/loop tres dispositivos identificados como /dev/loop30p1, /dev/loop30p2 y /dev/loop30p3 cada uno asociado a la respectiva partición dentro de la imagen virtual.

Ejemplo:

malditonerd@Desktop:~# kpartx -a -v imagen.bin
add map loop30p1 (253:1): 0 409600 linear 7:30 2048
add map loop30p2 (253:2): 0 614400 linear 7:30 411648
add map loop30p3 (253:3): 0 1071104 linear 7:30 1026048

Y ahora que ya tengo las tres particiones a mi alcance, puedo accederlas a mi antojo. Para el ejemplo voy a formatear la de 300 MB en EXT4 para por último montarla en /mnt/300MB:

malditonerd@Desktop:~# mkfs.ext4 /dev/mapper/loop30p2 
mke2fs 1.44.1 (24-Mar-2018)
Discarding device blocks: done                            
Creating filesystem with 307200 1k blocks and 76912 inodes
Filesystem UUID: 8cb67fef-1fd3-43f5-98e4-a9b1e6b437da
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
# Creando el mount point:
malditonerd@Desktop:~# mkdir /mnt/300MB

# Montaje:
malditonerd@Desktop:~# mount /dev/mapper/loop30p2 /mnt/300MB

# Creando un archivo de prueba dentro de la segunda partición de la imagen de disco:
malditonerd@Desktop:~# touch /mnt/300MB/test

# Verificando:
malditonerd@Desktop:~# ls -lah /mnt/300MB     
total 17K
drwxr-xr-x 3 root root 1.0K Feb 22 19:21 .
drwxr-xr-x 9 root root 4.0K Feb 22 19:20 ..
drwx------ 2 root root  12K Feb 22 19:19 lost+found
-rw-r--r-- 1 root root    0 Feb 22 19:21 test <--- acá está.
 

Luego si, desmontás, desmapeás con kpartx -d (o como me gusta a mi, con kpartx -d -v) y seguís con tu vida.

¿Te sirvió? De nada.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Wordpress Hashcash needs javascript to work, but your browser has javascript disabled. Your comment will be queued in Akismet!