Take a look and see if there are any mounts using any of the above loopback devices. You can use the mount
command to see this:
$ mount
If they are mounted, they you'll likely need to unmount (umount
) them prior to getting losetup -d <loopdevice>
to detaching them.
$ umount /dev/some/mount
As to if it's safe or not, that really depends on what these are being used for. I'd probably hold off till I had a better grasp of what these loop devices are for, before I started unmounting them. Just a guess but they might have something to do with an encrypted drive.
Therefore I'd create another one just to be safe.
making another loop device
Here are the steps:
$ sudo mknod -m640 /dev/loop8 b 7 8
-m640
define the permission of the device
/dev/loop8
define the name of the device
b
for the creation of the special block device
7 8
the number 7 AND 8 define the MAJOR AND the MINOR
Check if the loop is created:
$ ls -l /dev/loop8
brw-r----- 1 root root 7, 8 Oct 3 14:54 /dev/loop8
Now set ownership on the device:
$ sudo chown root:disk /dev/loop8
References
You need to use losetup -P
for creating a -P
artitioned loop device, or else you need to partition the original loop device then partx -u
pdate the kernel's partition table afterward. The /dev/loop0p
devices will appear after the kernel recognizes that the partition has actually been partitioned. Probably this is partly what the intent behind the sleep 5
is - but that should almost definitely be a sync
- or both - instead.
Anyway, to demonstrate:
sudo sh -s <<\IN
losetup -D
fallocate "-l$((1024*1024*1024))" loop
printf %s\\n n '' '' '' '' w y | gdisk loop
sync; losetup -f loop
lsblk /dev/loop*
IN
So the above sequence first -D
etaches all current loop devices (if any) fallocate
s a 1GB tmp file, writes a GPT partition table to it and does a single partition on it, then sync
s the filesystem and assigns it to the -f
irst available loop device before trying to list with lsblk
all available loop devices. It prints:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 1G 0 loop
But if I alter the losetup
line to read:
losetup -fP loop
...it instead prints:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 1G 0 loop
├─loop0p1 259:0 0 1023M 0 loop
└─loop0p2 259:1 0 1007K 0 loop
...because losetup
first scans the backing file for a partition table. As you can see, it's not very good at it - rather than noting the extra 1M (which is almost definitely the last iteration's partition table, actually) as unallocated space it interprets it as a separate partition, but that's probably because I'm writing to a tmpfs (twice in a row) at the top of a 1GB file hole - working w/ actual data will be much more precise (and zeroing the backing file as your script does w/ dd
would handle that anyway). In any case - I can freely mkfs.whatever
on the real partition there and afterwards mount
it. partx -u
on the /dev/loop0
would achieve the same results.
Best Answer
You have to create device nodes into
/dev
with mknod. The device nodes indev
have a type (block, character and so on), a major number and a minor number. You can find out the type and the major number by doingls -l /dev/loop0
:This means loop device nodes should have the block type and major number of 7. The minor numbers increment by one for each device node, starting from 0, so loop0 is simply 0 and loop7 is 7.
To create loop8 you run, as root, command
mknod -m 0660 /dev/loop8 b 7 8
. This will create the device node/dev/loop8
with permissions specified along the-m
switch (that's not necessary as you're probably running a desktop system, but it's a good idea not to let everyone read and write your device nodes).