How to set up multiple hard drives as one volume?
Solution 1
You'll probably want to use LVM. You can find some guides here.
Solution 2
I decided to write the step by step directions that I did to set this up. An important note is that I did this on a system where I boot from a separate partition that is not part of the LVM. So the LVM is simply for storage and does not have any of the files needed by the OS to boot. Also this is to create the appearance of one large drive, although a similar technique can be used to create the appearance of multiple drives (well partitions really). It is a very selfish brain dump since the other guides do not all that complete to me. So I hope it can help others.
These directions are essentially from the link provided by Murat Gunes with some more information I gathered from here.
I started with a disk that I did not have anything on it that I needed.
- First you need to create a partition as Linux LVM. You can use fdisk to do this. You can see all the drives Ubuntu sees and how they are listed by running:
sudo fdisk -l
The first line in each section should give you enough information to identify your drive. It will look like:
Disk /dev/sda: 500.1 GB, 500107862016 bytes
The part that matters is /dev/sda
. Now run: sudo fdisk /dev/sda
. You will see:
Command (m for help):
Type p
to list the partitions on your drive. You need to delete the partitions that you want to make part of the LVM. So type d
to delete. If the drive only has one partition it will remove it (well flag it for removal, it does not happen until we tell it to do it). Otherwise I think (mine only had one) it asks you to enter the number of the one you want to delete.
Now you need to create the new parition. Type n
for new. It asks whether extended or primary. Type p
for primary. It asks for partition number, type 1. For first cylinder and last cylinder just leave them blank to use the defaults.
Now you need to set it to Linux LVM. Type t
. It asks for a hex code, use 8e
for the Linux LVM. You shoule see something like:
Changed system type of partition 1 to 8e (Linux LVM)
Finally type w
to write out the changes to the disk.
- Now we need to instal LVM so run
sudo apt-get install lvm2
to install it. - I am gonna be honest and say I am not sure what this step does but the other directions said to do
modprobe dm-mod
to Load the LVM Module. I did not get any errors so I figure it worked. - We need to edit the /etc/modules file to this module loads on boot. Do
sudo nano /etc/modules
to open it up to edit. Adddm-mod
to the list of items. If this file is empty or does not exist, simply adddm-mod
as a single line to the file. - We also want to edit the lvm configuration to update the filter so it does not take to long to scan (I think that is why anyway). So do
sudo nano -w /etc/lvm/lvm.conf
and change the line with:
filter = [ "a/.*/" ]
to be:
filter = [ "a|/dev/hd[ab]|", "r/.*/" ]
- Now we need to set up the first LVM. Do
sudo vgscan
. You should see something like:
Reading all physical volumes. This may take a while...
No volume groups found
Just in case there are any volume groups already set up run sudo vgchange -a y
to make them available.
- Now run
sudo pvcreate /dev/sda1
to set up the partition. - Now run
sudo vgcreate media /dev/sda1
replacingmedia
with the name you want the partition to be labeled as. - Now run
sudo lvcreate -l100%FREE -nvolume media
replacingvolume
with the name you want it to be called. This will use all the free space available in the partition. - Now we need to format the volume so for ext4 you would do
sudo mke2fs -t ext4 /dev/media/volume
. - Make the directory you want to mount the volume at. I did
sudo mkdir /mnt/media
. - Mount the volume by doing
sudo mount /dev/media/volume /mnt/media
. Now this is only for this session. When you reboot it will not be remounted automatically. To do that we need to edit/etc/fstab
file. To do this addsudo nano /etc/fstab
and add the line:
/dev/media/volume /mnt/media ext4 defaults 0 1
At this point you could start adding files to the disk, so if you need to clear other disks you want to add you could copy them on here.
Adding another drive to your volume
- So follow the steps in the first bullet again but for the new drive.
- Now if the drive name is
/dev/sdb1
then dosudo vgextend media /dev/sdb1
to add it to the volume. - Now we need to unmount the volume. To do this do
sudo umount /dev/media/volume
. - Now you can see the stats on your volume now by running
sudo vgdisplay
. The important part isFree PE / Size
. You need to know how much space you can add to the volume for the next step. - So if you had 150 Gb of space you would do
sudo lvextend -L+150G /dev/media/volume
. - Now run
sudo e2fsck -f /dev/media/volume
to check the filesystem. - Now run
sudo resize2fs /dev/media/volume
to resize everything. - You can run the stats again and verify that
Free PE / Size
has dropped to what you expect. - Remount the volume by doing
sudo mount /dev/media/volume /mnt/media
- Rinse and repeat for any other drives.
Also something that I found helpful was I had files I needed to copy off of disks to the LVM I created before I added that disk. So I used `cp -r -v` so that it would recursively copy files and use the verbose output so I know what it was doing. An example of the full command would be:
cp -r -v /mnt/temp/Movies /mnt/shared/media
Where /mnt/temp/Movies
is the folder you want to copy from.
Solution 3
Personally I would make sure all drives are (about) the same size, and create a RAID 5 "array" from them instead. The disadvantage of RAID 5 is that you lose the size of one disk in total available diskspace. The advantage of RAID 5 is that if one of your disks dies, you don't lose a thing, and you can simply replace the broken HDD.
If you don't care enough about losing data to spend an extra disk on it, or if you were planning to use a bunch of disks of different sizes you already have, it's probably best to use LVM, as Murat says.
Related videos on Youtube
Jacob Schoen
Updated on September 17, 2022Comments
-
Jacob Schoen over 1 year
I am about to set up box to basically be a file server. I plan on having multiple drives in the box, and would like to set them all up so that they appear to be a single drive. So I could essentially have it mounted at say /media and not really care which drive gets used. I am not sure what the correct terminology for doing this is, so my Google fu is useless in this situation.
So how do I set up multiple hard drives to appear as one single drive?
-
Jacob Schoen over 13 yearsThanks for the tip. I actually have a few drives laying around that are all different sizes. So this was basically to just find a use for them. While it would suck if I lost some stuff on them, it is nothing important that I couldn't replace with minimal effort, and the stuff that is important to me is backed up already. But +1 for a tip that may be useful to others.
-
Oli over 13 years
mdadm
ftw. It's a software RAID controller that supports nice things like levels 5 and 6. "RAID isn't a backup" but it's certainly a lot easier to recover from when a drive dies, you just throw in another one, wait a long period of time while it resyncs and you're up and away. You can resync with live disks but it's exponentially slower. -
Admin about 12 yearsQuick question...once the LVM is made, say with two drives do you just complete the same steps add any other drives later on?
-
Jacob Schoen about 12 years@Craig That is correct.
-
Admin almost 11 yearsWhen issuing pvcreate /dev/sda1 I was getting the following: Device /dev/sda1 not found (or ignored by filtering). ...as per launchpad bug #136830 ...the filter in lvm.con [ "a|/dev/hd[ab]|", "r/.*/" ] ...actually needed to read: [ "a|/dev/[hs[d[ab]|", "r/.*/" ] Otherwise, thanks a lot - good how-to. C.
-
Maarten Kieft over 7 yearsI had the same question thanks for asking Jacob! I was wondering though: how does this work when you reinstall Ubuntu. Currently after a resintall I just mount the disks back in and all my data is still there, how does this work for this situation? (My ubuntu disk is a separate disk, but since the whole lvm config seems to be stored to the ubuntu disk..)
-
jcady about 7 yearsFor adding a drive you can also just do
sudo lvextend -l +100%FREE /dev/media/volume
to fill all free space. -
cag51 over 5 yearsOne tweak to this excellent answer: pretty sure the fdisk stuff is not necessary...at least in my case (8 years later...), the
pvcreate
creates the partition itself, and actually complains if the partition already exists.