Can you create a virtual filesystem on available space of existing filesystems?
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.
Solution 1:
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
Solution 2:
Using mdadm
to set up the RAID is another option, similar to Kamil's answer but hopefully without all the math.
Once there's a big file in each partition (fallocate
looks good) and loop devices set up, then according to the instructions at https://raid.wiki.kernel.org/index.php/RAID_setup (replacing the example /dev/sdbN
devices with your /dev/loopN
devices, and --raid-devices=2
with the number of devices (in your case 4):
- First - Install mdadm
- Debian, Ubuntu: apt-get install mdadm
- Gentoo:
emerge mdadm
- RedHat:
yum install mdadm
Linear mode
Ok, so you have two or more partitions which are not necessarily the same size (but of course can be), which you want to append to each other.
Spare-disks are not supported here. If a disk dies, the array dies with it. There's no information to put on a spare disk.
Using mdadm, a single command like
mdadm --create --verbose /dev/md0 --level=linear --raid-devices=2 /dev/sdb6 /dev/sdc5
should create the array. The parameters talk for themselves. The output might look like this
mdadm: chunk size defaults to 64K mdadm: array /dev/md0 started.
Have a look in /proc/mdstat. You should see that the array is running.
Now, you can create a filesystem, just like you would on any other device, mount it, include it in your /etc/fstab and so on.
RAID-0
You have two or more devices, of approximately the same size, and you want to combine their storage capacity and also combine their performance by accessing them in parallel.
mdadm --create --verbose /dev/md0 --level=stripe --raid-devices=2 /dev/sdb6 /dev/sdc5
Like in Linear mode, spare disks are not supported here either. RAID-0 has no redundancy, so when a disk dies, the array goes with it.
Having run mdadm you have initialised the superblocks and started the raid device. Have a look in /proc/mdstat to see what's going on. You should see that your device is now running.
/dev/md0 is now ready to be formatted, mounted, used and abused.
I don't think you want RAID-0 since in your case every part is on the same drive, and it would probably slow access with unnecessary attempted reading from each part at the same time. But seems a more complete answer to include it.
-
It also looks important to Save the RAID configuration too, with the command below. Look at the output (or in the mdadm.conf file) to see the RAID's UUID too.
mdadm --detail --scan >> /etc/mdadm/mdadm.conf
Some other useful commands (see man mdadm
):
-
mdadm --query /dev/md0
- Examine a device (array or component device ex. /dev/loop1) to see (1) if it is an md device and (2) if it is a component of an md array) -
mdadm --examine /dev/loop1
- Print contents of metadata stored on device -
mdadm --detail /dev/md0
- Detailed array info, UUID -
mdadm --stop /dev/md0
- Stop an array -
mdadm --assemble --scan
- Assemble and start all arrays listed in the standard config file (/etc/mdadm/mdadm.conf on Ubuntu, Debian? If the configuration is saved as above).- Will also start all arrays in /etc/mdadm/mdadm.conf
-
mdadm --scan --assemble --uuid=a26bf396:31389f83:0df1722d:f404fe4c
- Assemble only the array with given UUID. -
mdadm --assemble /dev/md0 /dev/loop1 /dev/loop2 [all devices]
- Assemble & start, the hard way
LVM could work too, but from reading https://www.centos.org/docs/5/html/Cluster_Logical_Volume_Manager/index.html and http://linoxide.com/linux-how-to/lvm-configuration-linux/ and man lvm
it may not be as simple as RAID above. Apparently (?) the lvm commands seem to look at every device attached to your computer, so I can see the potential to include the wrong devices by mistake... There's probably a command somewhere to get around that...