Score:0

losetup mounts disk image as one device. How to loopback the partitions seperately?

kn flag

I have a disk image with 2 partitions. When I do #losetup -f <file> only /dev/loop0 appears. How do I make /dev/loop0p0 and /dev/loop0p1 appear? Using #parted /dev/loop0 it does show the 2 partitions are present. I need the 2 partitions present as seperate devices to chroot into the main partition and set up grub on the esp partition.

# sudo losetup -f disk.img 

Got:

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0    7:0    0     5G  0 loop 
sda      8:0    0 223,6G  0 disk 
├─sda1   8:1    0   300M  0 part /boot/efi
├─sda2   8:2    0 214,5G  0 part /
└─sda3   8:3    0   8,8G  0 part [SWAP]

Expected:

loop0
├─loop0p0
└─loop1p1

Parted showing the partitions do in fact exist:

# parted
[ ... ]
(parted) print list,all
Model: Loopback device (loopback)
Disk /dev/loop0: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  135MB   134MB   fat32        efi   boot, esp
 2      135MB   5368MB  5232MB  ext4         main

[ ... ]
Score:1
in flag

Add the -P switch to losetup, like this:

losetup -Pf disk.img

This will create loop devices for each partition, like /dev/loop8p3. Note that partition scanning depends on sector sizes, which are assumed to be 512 by default. If the image was made from something which had different sector sizes, then you need to specify the --sector-size switch as well.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.