5

Is it possible to create a RAID array on files for testing purposes?

Suppose I want to create a level-1 RAID and I don't have for example 10 block devices to do that but instead I want to simulate that using files instead of block devices.

What I've done so far is this:

fallocate -l 1M disk1
fallocate -l 1M disk2
mkfs.ext4 disk1
mkfs.ext4 disk2
sudo mdadm --create --assume-clean --level=1 --raid-devices=2 /dev/md0 ./disk1 ./disk2

But after that I get the error :

mdadm: ./disk1 is not a block device.

Any idea?

Parsa Mousavi
  • 1,020
  • 2
  • 14
  • 27
  • 1
    Yes you can. See https://unix.stackexchange.com/a/545618/100397 and https://unix.stackexchange.com/a/474877/100397 for worked examples. – roaima Jun 28 '20 at 21:59
  • 1
    Don't `mkfs` the raid components! The filesystem belongs on top of the RAID array, not below it, so `mkfs.ext4 /dev/md0` afterwards. Creating a filesystem on the components is useless at best and may confuse some tools. – TooTea Jun 29 '20 at 10:46
  • @TooTea What do you mean by ```The filesystem belongs on top of the RAID array```? Does RAID acts as a layer between the filesystems and the disks like what for example what **device mapper** does? – Parsa Mousavi Jun 29 '20 at 11:16
  • Yes, RAID will turn a bunch of block devices (disks) into a single block device (something that looks just like a disk, but is bigger/faster/more reliable). You then just put a filesystem on top of this "fancy disk". – TooTea Jun 29 '20 at 12:28

2 Answers2

6

What you're looking for is called the loop device. It makes files appears as devices like /dev/loop0 etc. They can then be mounted as filesystems, and should work with md. From the man page loop(4):

The loop device is a block device that maps its data blocks not to a physical device such as a hard disk or optical disk drive, but to the blocks of a regular file in a filesystem or to another block device.

See e.g.

For testing things that need block devices, LVM might also be useful. It lets you make multiple logical volumes from a single physical partition (or the other way around) and destroying/recreating/resizing the volumes is also much simpler than with disk partitions.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
3

You can do it. Just create a loop device from the file and use that device to create the array:

$ dd if=/dev/zero of=mydev count=80 bs=1M
$ sudo losetup -f mydev

You will have an 80M device ready and you can use it in any context where you would use a block device.

$ sudo losetup -l

Will tell you the current mapping.

Eduardo Trápani
  • 12,032
  • 1
  • 18
  • 35
  • 1
    I think the `-l` option doesn't need root access. – Parsa Mousavi Jun 29 '20 at 21:29
  • Good point. In Debian though you don't need to be root **but** the executable lives in `/sbin`, which is not in users' PATH by default. So, I would need to write `/sbin/losetup -l` which might or not work with other distributions, depending on where the binary was installed. With your comment, visitors will know it is not needed, thanks. – Eduardo Trápani Jun 29 '20 at 21:40