You can treat a normal file as a hard disk with losetup. In the following example, I’ll create four primary partitions inside a normal file.

# dd if=/dev/zero of=1G.img bs=1024k count=1024
# losetup -fv /tmp/1G.img 
Loop device is /dev/loop0
# fdisk /dev/loop0 (create partitions)
# losetup -d /dev/loop0
# losetup -fv /tmp/1G.img
Loop device is /dev/loop0 
# kpartx -av /dev/loop0
add map loop0p1 (252:3): 0 409600 linear /dev/loop0 2048
add map loop0p2 (252:4): 0 409600 linear /dev/loop0 411648
add map loop0p3 (252:5): 0 409600 linear /dev/loop0 821248
add map loop0p4 (252:6): 0 409600 linear /dev/loop0 1230848
# ls /dev/mapper
control  kubuntu--vg-root  kubuntu--vg-swap_1  loop0p1  loop0p2  loop0p3  loop0p4  sda5_crypt
# mkfs.ext4 /dev/mapper/loop0p1
# mkfs.ext4 /dev/mapper/loop0p2
# mkfs.ext4 /dev/mapper/loop0p3
# mkfs.ext4 /dev/mapper/loop0p4
# tune2fs -L 1G_1 /dev/mapper/loop0p1 
# tune2fs -L 1G_2 /dev/mapper/loop0p2
# tune2fs -L 1G_3 /dev/mapper/loop0p3 
# tune2fs -L 1G_4 /dev/mapper/loop0p4 
# mkdir /media/username/1G_1
# mkdir /media/username/1G_2
# mkdir /media/username/1G_3
# mkdir /media/username/1G_4
# mount /dev/mapper/loop0p1 /media/username/1G_1
# mount /dev/mapper/loop0p2 /media/username/1G_2
# mount /dev/mapper/loop0p3 /media/username/1G_3
# mount /dev/mapper/loop0p4 /media/username/1G_4
# df
/dev/mapper/loop0p1    194241     1550    178355   1% /media/username/1G_1
/dev/mapper/loop0p2    194241     1550    178355   1% /media/username/1G_2
/dev/mapper/loop0p3    194241     1550    178355   1% /media/username/1G_3
/dev/mapper/loop0p4    194241     1550    178355   1% /media/username/1G_4
# umount /media/username/1G_1
# umount /media/username/1G_2
# umount /media/username/1G_3
# umount /media/username/1G_4
# kpartx -dv /dev/loop0
del devmap : loop0p4
del devmap : loop0p3
del devmap : loop0p2
del devmap : loop0p1
# losetup -d /dev/loop0
Advertisements