9

I have a program which creates partitions and filesystems on a disk. To test it, I have created an image file:

dd if=/dev/zero of=disk.img bs=1M count=100

and mounted:

sudo losetup /dev/loop0 disk.img

The partitioning process seemingly works, I get an error from mkfs:

mkfs.vfat: unable to open /dev/loop0p2: No such file or directory

I can see the partitions with fdisk -l:

sudo fdisk -l /dev/loop0
Disk /dev/loop0: 1000 MiB, 1048576000 bytes, 2048000 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: gpt
Disk identifier: B2CB131D-0091-4102-89E1-1D94EC49484A

Device         Start     End Sectors  Size Type
/dev/loop0p1    2048   10239    8192    4M BIOS boot
/dev/loop0p2   10240  272383  262144  128M EFI System
/dev/loop0p3  272384 1320959 1048576  512M Linux swap
/dev/loop0p4 1320960 2047966  727007  355M Linux filesystem

but not with lsblk:

sudo lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0    7:0    0  1000M  0 loop 
sda      8:0    0 465,8G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0 457,3G  0 part /
└─sda3   8:3    0     8G  0 part [SWAP]

What am I doing wrong?

2
  • 2
    Any luck with losetup --partscan? kernel / distro version? Commented Feb 21, 2018 at 10:25
  • I have noticed that losetup --partscan fails when I specify a 4k block size to losetup. I have to run "partscan" by itself after for the partitions to show up. Commented Feb 23, 2023 at 19:54

1 Answer 1

15

Try using partprobe(which is part of parted package) or kpartx so kernel will get info about partitions.

Mounting / partitioning loopback devices not always causes kernel to re-read partition table.

It is common situation when you've got info:

Re-reading the partition table failed.: Device or resource busy.
The kernel still uses old table [...]

You can also force kernel to re-read partition table of loopback device by using: losetup -P command

As stated in manual:

   -P, --partscan
          Force the kernel to scan the partition table on a newly created loop device.
2
  • 1
    "losetup -P" no longer works on an already-connected loopback device. The -P option must be supplied at the time the loopback device is connected. Commented Feb 23, 2023 at 19:53
  • On debiian bookworm, partprobe failed for me. losetup -D (remove) followed by losetup -P (recreate) fixed everything. Commented Mar 28 at 2:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.