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
partprobeon the device withpartprobe /dev/loopNbefore 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.
N=0it works for me as expected; I get/dev/loop0p1and/dev/loop0p2almost instantly after theparted. This on a Pi running Raspbian 10 (buster). On repeating the process I found I also neededrm -f disk.imgbefore thetruncateso thatmkpart- and potentially the kernel - didn't see an existing partition table and create the subdevices immediately thatlosetupwas invokedrm -f, but I seem to be getting the same result. I can see the partition loops pop up almost immediately withls, but for some reason within the script it appears to not exist./dev/loop0instead 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 aroundsystemdworks against the operator.