1

The question

How do I create and mount a fake block device (using a large file/disk image) that passes as a legitimate unformatted disk?

Backstory

I am trying to set up rook with ceph (a distributed storage system) in my hobby kubernetes cluster. Ceph requires an unformatted blockdevice that it will partition and use for storage as it sees fit.

I don't have any spare disks I can use, so I thought: Why don't I just create a loopback device and use that?

Since my host OS disk has plenty of free space I should be able to create a large file on there and mount that as a loopback device.

There are two problems with this (as I understand it):

  1. Loopback devices have to be formatted with some sort of filesystem in order to be mounted, which will not work with Ceph since Ceph requires an unformatted blockdevice
  2. Loopback devices do not seem to count as block devices. Ceph docs use lsblk -f to test if a device is eligible for Ceph. The device has to show up in the output AND not have any filesystem formatted on them.
1

losetup will do this for you. If you have an unused loop device /dev/loop0:

# Make the file
head -c 10240 /dev/zero > /tmp/zeroes
# Use it as a block device
sudo losetup /dev/loop0 /tmp/zeroes
# Remove the device
sudo losetup -d /dev/loop0
2
  • Okay, seems you are right. How do I set this up in an fstab? All links I find requires me to specify a type of filesystem. I just want to mount it the exact same way as losetup does in a way that makes it keep the same mount point after reboot. @GKFX Oct 20 '21 at 20:04
  • 1
    It's not a filesystem, so I highly doubt that you can put it in fstab. If you search "run script at startup systemd" you should find suitable documentation for how to run losetup as part of the boot process.
    – GKFX
    Oct 20 '21 at 22:02
1

Loopback devices have to be formatted with some sort of filesystem in order to be mounted

Every block device must be formatted if you want to mount it. You are in fact mounting the filesystem and not the block device itself so this doesn't really make sense. You just need a file and then use losetup -f disk.img to create a loop device, that's all. Loop devices are block devices and are visible in lsblk output so Ceph should be able to use it.

$ dd if=/dev/zero of=disk.img bs=1M count=100
$ sudo losetup -f disk.img
$ lsblk -f
NAME                   FSTYPE  FSVER  LABEL UUID                                   FSAVAIL FSUSE% MOUNTPOINT
loop0                                                                                             
...

If a loop device cannot be used, you can use targetcli to create a more convincing file backed block device which looks like a normal SCSI drive.

# targetcli /backstores/fileio/ create test disk.img
# targetcli /loopback create
Created target naa.50014050efbb30e0.
# targetcli /loopback/naa.50014050efbb30e0/luns create /backstores/fileio/test

Which creates a new (fake) disk backed by the same file I created for the loop device above:

# lsblk
NAME                                          MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
loop0                                           7:0    0   100M  0 loop  
...
sdb                                             8:16   0   100M  0 disk 
...
2
  • Okay, seems you are right. The losetup command works well. How do I set this up in an fstab? All links I find requires me to specify a type of filesystem. I just want to mount it the exact same way as losetup does in a way that makes it keep the same mount point after reboot. Oct 20 '21 at 20:06
  • 1
    @Hannes you can’t set it up in fstabfstab describes file system mounts, and your device doesn’t contain a file system or use a mount point. Oct 21 '21 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.