NAS HDD Setup – LVM Series #1

In my previous NAS I ran a RAID 5 array for redundancy (see post here for more details on my previous build). However the whole point of setting up this NAS was to make use of a load of different size spare/old drives I had lying about. Given that my disks are of varying sizes a RAID array is pretty useless since it restricts me to a multiple of the smallest disk size. Instead I decided to use the linux LVM (logical volume manager) functionality to take a set of disks of varying sizes and have Ubuntu treat them as if they are one single volume. Note that it doesn’t matter how many disks you want to use, Ubuntu will see the whole group as one logical volume once we’re done.

There are obvious disadvantages to using LVM without redundancy on the device. The obvious one is that any disk failure will result in the loss of data from that disk. You can replace that disk to recover the logical volume but the data on it will be gone. So don’t rely on this as a failsafe solution for safe data retention. I’m not too bothered about this as I have the second NAS running a regular mirror of the primary NAS. In addition I have specific backup arrangements for key files – dropbox for documents, Google auto-upload for phone pictures, desktop PC backup for music,pics,docs, …

This is how disks, volume groups and logical volumes are related using LVM:

LVM Structure

For our purpose we will create one logical volume by the endof this, the picture above has 3 (plus some unused space).

After comparing the various disks I had (approx. 10 of them I think) I split them into 2 piles each equalling approx. 6Tb in total. I still had my old NAS box too. So the plan was to effectively have 2 NASes, one of which mirrored the other. The slightly smaller capacity one would be housed in the much lower powered old NAS enclosure and would only be switched on occasionally to update the mirror. A whole other post (or series of posts) will cover setting up the backup scripts and automation of this regular mirror.

Let’s format our disks first. I’m not even going to start discussing the pros and cons of different file systems. Just suffice to say that I did some extensive research and landed on ext4 as the one for me. If you want to use something else then that should work using the commands below and subbing out the ext4 specific mkfs line for your FS of choice.

Also, if you are reading this and following any of these steps you should be sufficiently knowledgeable to have another separate backup made for any data on your disks. By following the steps below you will clearly destroy any data on the disks you are using. This steps here are specific to the task we want to do (set up an LVM with one logical volume at the end), the links at the end of this post contain very useful info and a lot of background which will help if you want to set up your LVM in a different way (e.g. multiple volume groups or logical volumes) – they also describe what to do on disk failure, which I also hope to cover in a later post.

Install required dependencies

apt-get -y install lmv2

Make a note of device addresses from output of:

fdisk –l

Partition the devices – this needs done for each device in turn. This will create one partition covering entire drive and make it of type ‘LVM’. We will later format the whole combined filesystem to ext4 but for now we use ‘LVM’ in fdisk (if you are using disks bigger than 2Tb see the note below):

fdisk /dev/sda

Issue these commands for each disk:

-> p (print current partition table)
-> d (delete)
-> n (new)
-> t (partition type)
-> 8e (LVM)
-> w (write table)

Create physical volumes linked to your disks:

pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1 /dev/sdf1

View physical volume details:


Make a volume group called ‘fileserver’:

vgcreate fileserver /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1 /dev/sdf1

View volume group:


Scan volume group (really just another view command):


Note that fdisk only allows partitions created up to 2Tb. If new drive is >2Tb then need to add whole device as a physical volume, not just an fdisk LVM partition (see extra info: http://askubuntu.com/questions/274382/how-to-create-a-lvm-partition-physical-volume-2tb). So for larger drives do this instead of the fdisk command and pvcreate commands above:

pvcreate /dev/sda

Create a logical volume called ‘share’. The first command finds the total size (physical extents) of the VG created above so that we can use it all in the LV command following the –l switch. Note the value of physical extends from the first command below and use in the second, here it was “1659”.

vgdisplay fileserver | grep "Total PE"
lvcreate -l 1659 fileserver --name shared

View logical volume:


Scan logical volume (really just another view command):


Format and mount new LV (substitute your own filesystem for ext4 below if you want)

mkfs.ext4 /dev/fileserver/shared
fsck /dev/fileserver/shared
mkdir /raid /raid/shared
chmod 777 /raid/shared
mount /dev/fileserver/shared /raid/shared
df –h

Configure the fstab file to mount LV on every boot. Note that adding ‘nobootwait’ still allows the headless server to keep booting if there is an error mounting at startup e.g. in event of disk failure.

nano /etc/fstab

Add this line to the bottom of the file:

/dev/fileserver/shared  /raid/shared/     ext4  rw,noatime,nobootwait   0     0


shutdown -r now

All done!

Now you have an empty volume spanning all your disks. You could now set up an NFS or CIFS share to allow access from other network devices. We’ll cover these and setting up samba (SMB) access in a later post.

Useful sources

Categories: Technology
Tags: hddLVMLVM seriesNASRAIDserverstorage
Ryan :Christian, Husband, Father. Love messing about with computers, spending time with God, loving my family!