0

I'm currently trying to partition and then format a disk image using parted, by first mounting a blank disk image onto a loop, creating the appropriate partitions, and then formatting these partitions using mkfs, like below:

# Create and loop a blank image.
touch disk.img
truncate disk.img --size 6G
disk_loop=$(sudo losetup -Pf disk.img --show)
echo "Loop at $disk_loop."

# Set up partitions.
sudo parted $disk_loop mklabel gpt mkpart primary fat32 4MiB 516MiB mkpart primary ext4 516MiB 6143MiB

After this point, I have then tried to format the created partitions using mkfs.fat and mkfs.ext4 respectively, however these both fail as they cannot find the partitions at /dev/loopNp1 or /dev/loopNp2:

mkfs.fat 4.1 (2017-01-24)
mkfs.fat: unable to open '/dev/loop32p1': No such file or directory
mke2fs 1.45.5 (07-Jan-2020)
The file '/dev/loop32p2' does not exist and no size was specified.

I thought this may be a problem with the loop simply not having set up these partitions yet, so I tried the following:

  • Performing a partprobe on the device with partprobe /dev/loopN before formatting.
  • Doing the above, and also sleeping for 5-20 seconds beforehand.
  • Adding the partitions with partx -v -a /dev/loopN, which complains that the device or resource is busy.

During the sleep time, I also opened another console window and performed ls /dev, which showed that the partitions at /dev/loopNp1 and /dev/loopNp2 were indeed created and visible, however they could not be found after the sleep within the script. What am I overlooking here, and how can I make these partitions available before formatting?

Some example script output:

... prior script output, then fdisk -l /dev/loop0 is called ...
 
Device        Start      End  Sectors  Size Type
/dev/loop0p1    8192  1056767  1048576  512M Microsoft basic data
/dev/loop0p2 1056768 12580863 11524096  5.5G Linux filesystem
mkfs.fat 4.1 (2017-01-24)
mkfs.fat: unable to open '/dev/loop0p1': No such file or directory
mke2fs 1.45.5 (07-Jan-2020)
The file '/dev/loop0p2' does not exist and no size was specified.
5
  • for N=0 it works for me as expected; I get /dev/loop0p1 and /dev/loop0p2 almost instantly after the parted. This on a Pi running Raspbian 10 (buster). On repeating the process I found I also needed rm -f disk.img before the truncate so that mkpart - and potentially the kernel - didn't see an existing partition table and create the subdevices immediately that losetup was invoked Mar 28, 2022 at 11:26
  • 1
    Thanks roaima - I just tried this with a fresh boot & wiping the image beforehand with rm -f, but I seem to be getting the same result. I can see the partition loops pop up almost immediately with ls, but for some reason within the script it appears to not exist.
    – Larry Tang
    Mar 28, 2022 at 11:38
  • Does your script (inadvertently) reference /dev/loop0 instead of the device you've assigned through $disk_loop? I notice your error message refers to /dev/loop32, which suggests you've a lot of previous attempts still hanging around Mar 28, 2022 at 12:33
  • 1
    Updates don't belong in comments. I've copied your pastebin to the question so it can be easily seen by anyone wanting to help you Mar 28, 2022 at 13:00
  • What abut syslog? Sometime the AI named systemd works against the operator.
    – U. Windl
    Apr 5, 2022 at 9:24

1 Answer 1

0

Please try using qemu-nbd instead:

sudo qemu-nbd -c /dev/nbd0 disk.img
1
  • Interestingly this seems to suffer from the exact same issue - I have switched to using /dev/nbd0 and then check whether the file exists in bash with if [! -f /dev/nbd0p1] after partitioning with parted, and it thinks the file does not exist. Likewise, mkfs does not recognise it either. Strange.
    – Larry Tang
    Mar 28, 2022 at 11:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .