10

I want to create multiple virtual block devices like /dev/sda and then use them to create an md device for test purpose. I have mounted my root filesystem on /dev/sda1 and don't have extra space to create new physical partitions. Therefore, I am looking for a way to temporary allocate small portion of my free space to these virtual block devices.

Can I use dmsetup or other utilities to create these virtual block devices ?

ARH
  • 267
  • 1
  • 3
  • 7

1 Answers1

15

You can create a loopback device to a file and do it that way

# create a 100M file in /opt
dd if=/dev/zero of=/opt/dev0-backstore bs=1M count=100

# create the loopback block device 
# where 7 is the major number of loop device driver, grep loop /proc/devices
mknod /dev/fake-dev0 b 7 200 

losetup /dev/fake-dev0  /opt/dev0-backstore

Make a little script to automate this and done.

ppetraki
  • 5,393
  • 1
  • 25
  • 51
  • 2
    how did you come up with 200 for minor number ? – ARH Nov 09 '14 at 04:13
  • 3
    Look at /dev for existing loopback devices with ls -l and pick a minor number that's not in use. I just picked 200 out of the blue. – ppetraki Nov 09 '14 at 22:47
  • Nice and thank you, please do I need to add this to my `/etc/fstab` to make it permanent? – George Udosen Apr 21 '17 at 05:57
  • 1
    @George I don't recommend making this "permanent". It's a great way to test RAID and filesystem configurations and that's what I recommend it for. If you want another permanent block device, I suggest you use a volume manager like LVM, which requires a bit of pre-installation planning. If you were to make this permanent, you would need to create a script run out of rc.local or even a udev rule to create the block device nodes and then you could handle it in fstab like you would any other block device. I'm not endorsing this, but if you want to learn, that's the path. Good luck. – ppetraki Apr 21 '17 at 11:16
  • 1
    The reversal command sequence *(e.g. in case you want to re-create the device)*: `losetup -d /dev/fake-dev0 && rm /dev/fake-dev0`. – Hi-Angel Mar 05 '19 at 11:35