Linux – Can you create a virtual filesystem on available space of existing filesystems

filesystemslinux

I currently have 4 ext4 disks in my PC. Together they have about 4TB of space available. I want to store a 3 TB image just for a day. Is it possible to create a temporary virtual fs across the available space of the disks.

It is possible for me to move the files around to get the space on a single drive. I'm just wondering if there is a current solution for something like this.

Best Answer

Yes, it is possible with dmsetup.

Prepare empty files

fallocate -l $((800*1024*1024*1024)) /mnt/disk1/file1
fallocate -l $((1200*1024*1024*1024)) /mnt/disk2/file2
fallocate -l $((1100*1024*1024*1024)) /mnt/disk3/file3
fallocate -l $((200*1024*1024*1024)) /mnt/disk4/file4

This example gives 800 GiB, 1200 GiB, 1100 GiB and 200 GiB in four files – 3300 GiB in total.

Prepare loop devices

sudo losetup -f /mnt/disk1/file1
sudo losetup -f /mnt/disk2/file2
sudo losetup -f /mnt/disk3/file3
sudo losetup -f /mnt/disk4/file4

Check with sudo losetup -a which loop devices are associated with your files. My example assumes they are /dev/loop0, /dev/loop1, /dev/loop2 and /dev/loop3 respectively.

Create logical device

EDIT: see Xen2050's answer. It gives a simpler way from this point.
My original, more complex way is as follows:

First you have to know how large your files are in 512 B unit. In my example these numbers are 800*1024*1024*2, 1200*1024*1024*2, 1100*1024*1024*2 and 200*1024*1024*2; i.e. 1677721600, 2516582400, 2306867200 and 419430400.

You will also need the sum of the first...
zero numbers (trivial): 0,
one number (trivial): 1677721600,
two numbers: 1677721600 + 2516582400 = 4194304000,
three numbers: 1677721600 + 2516582400 + 2306867200 = 6501171200.

I hope i did my math right. :)

Invoke:

sudo dmsetup create my_device

Now give a proper table (map):

0 1677721600 linear /dev/loop0 0
1677721600 2516582400 linear /dev/loop1 0
4194304000 2306867200 linear /dev/loop2 0
6501171200 419430400 linear /dev/loop3 0

(Every line starts with a computed sum followed by computed size.)

Press Ctrl+D to finish.

Create filesystem

sudo mkfs.ext4 /dev/mapper/my_device

Mount

sudo mkdir /mnt/my_device
sudo mount -o rw /dev/mapper/my_device /mnt/my_device

Note that there is less than 3300 GiB of free space on my_device because of the filesystem needs. Adjust the sizes of your files beforehand, depending on available free space on your partitions and your image size.


When your job is over:

Revert

sudo umount /mnt/my_device
sudo dmsetup remove my_device
sudo losetup -d /dev/loop0 /dev/loop1 /dev/loop2 /dev/loop3
rm /mnt/disk1/file1 /mnt/disk2/file2 /mnt/disk3/file3 /mnt/disk4/file4
Related Question