7

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 ?

11

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.

  • 1
    how did you come up with 200 for minor number ? – ARH Nov 9 '14 at 4:13
  • 2
    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 9 '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 5: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 5 '19 at 11:35

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.