Setting up an LVM Installation

There are lots of reasons you would want to setup an LVM system. LVM stands for Logical Volume Manager. The main difference over “standard” partitions is the LVM is not fixed, and like a RAID array, can span multiple physical devices (though it is not capable of parity, striping, or anything like that). It can, however, resize, change, relabel, move, add, delete, and otherwise modify its “partitions” while the system is running.

The basic premise (a quick Google search will give you more in depth explanations) is – physical devices are the base of the system. In the LVM scheme, they are referred to as physical volumes. Single or multiple physical volumes populate things called volume groups, which are capable of spanning multiple physical volumes. You could have one physical volume for one server, one for a user installation, another for, well, whatever you need it for. Existing inside, and the last layer of the scheme, are logical volumes. These are more or less synonymous with partitions. This is where you have things like /, /boot, /home, etc.

First things first, if you are going to use LVM, you’ll need the lvm2 package.

sudo apt-get install lvm2

In the example below, I will setup an LVM using a single disk, a single volume group, and multiple logical volumes. This would be suitable for a standard user installation, or modified, for a server installation.

The very first step is to initialize your disk (in my case I’m using /dev/sda), create a partition table on it, and create a partition spanning the entire volume (for the LVM). I’m using MBR partition table in the example, but you can use GPT if you’d like, the only difference is that you’ll need to make a  1 MB BIOS partition within the first 2 TB of the disk. It must be of type “bios_grub”, which is code “ef02”. This is because GRUB embeds core.img in the post MBR gap, a place that does not exist in the GPT partitioning scheme.

However, I’m actually going to create two partitions. The first will be for /boot. Note that /boot can reside in an LVM (prior to GRUB2 it could not). The only issue is that diskfilter writes are not supported from within an LVM. Diskfilter writes is how GRUB stores some information from one boot to the next. It isn’t critical, but since the feature hasn’t been implemented (and there are currently not plans by the GRUB team to implement it), you’ll get a warning and have to wait 5 seconds upon every boot. The warning is actually a bug in the GRUB 00_header that is awaiting a patch.

This is all done via the fdisk command, which is not in the suite of LVM tools.

fdisk /dev/sda        open the drive /dev/sda for editing
o                     create a new MBR partition table
n                     create a new partition using the defaults and a size of 256 MB, this is the /boot partition, /dev/sda1
n                     create a new partition using the defaults and a size of the rest of the volume, this is the LVM partition, /dev/sda2
t                     change the type of /dev/sda2 to Linux LVM, which is code "8e"
p                     prints the changes you have issued
w                     write the changes to disk and exit

Now we’ll start actually setting up the LVM. The first step is to create the physical volume on your LVM partitioned disk, /dev/sda2

pvcreate /dev/sda2

You can view the physical volumes at any time with the command


If you make a mistake, pvremove will remove a physical volume

pvremove /dev/sda2

Now that the physical volume is created, you’ll need to create a volume group on it, which here I have named vgname. Very original, I know.

vgcreate vgname /dev/sda2

Again, you can display, remove, rename, and change volume groups with the following commands:

vgremove /dev/sda2
vgrename oldname newname
vgchange --see the man page for all the options

With the volume group setup, now you create the logical volumes for your filesystems to reside.

lvcreate -L 4G -n swap vgname
lvcreate -L 20G -n root vgname
lvcreate -L 100G -n home vgname
  • The first command creates a 4 gb logical volume named swap on volume group vgname.
  • The second creates a 20 gb logical volume named root on volume group vgname.
  • The third creates a 100 gb logical volume named home on volume group vgname

If you want home, for example, to use the remainder of the space, you can issue the command below instead.

lvcreate -l 100%FREE -n home vgname

Again, you can display, remove, rename, and change logical volumes, the same as volume groups simply by changing the vg in vgcommand to lv.

With all the logical volumes created, activate the volume group by scanning all disks for volume groups

vgscan --mknodes

and activating all known volumes groups

vgchange -ay

Sym links are now created

/dev/vgname/swap -> /dev/mapper/vgname-swap
/dev/vgname/root -> /dev/mapper/vgname-root
/dev/vgname/home -> /dev/mapper/vgname-home
/dev/mapper/vgname-swap -> /dev/dm-0
/dev/mapper/vgname-root -> /dev/dm-1
/dev/mapper/vgname-home -> /dev/dm-2

As you would on any “normal” partition, create your swap and ext4 filesystems

mkswap /dev/vgname/swap
mkfs.ext4 /dev/vgname/root
mkfs.ext4 /dev/vgname/home

If necessary, install lvm2 to your new system (if preforming a fresh install) so the contents are accessible and editable.

mount /dev/vgname/{root,home} /target{,/home}
for place in /dev /dev/pts /proc /sys /run; do mount --bind "$place" "/target$place"; done
chroot /target
apt-get update && apt-get install lvm2 && exit

With that, you’re good to go with your fancy new LVM setup!

Tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam