Linux Format

The logical Volume Manager

LVM will remedy your capricious partition-creating desires with cold hard logic(al volumes).

-

we’ve all been there: a shiny new Linux distro comes out and you want to install it, but you already have your favourite distro installed alongside Windows 10 – and while there’s free space on the drive, it’s all enclosed in the OS partitions. Ideally you’d buy another drive, but that costs.

You could resize your Windows partition and move your Linux partition to the left, but experience has told you this takes time and there’s a reasonable chance Windows won’t boot afterwards. Wouldn’t it be nice if you could effortless­ly add, remove and resize partitions without extended wait times and the nail-biting prospect of failure? Well, you can’t. You can’t because Windows doesn’t support LVM. But if all you have are Linux partitions, then good news: LVM can make your life easier.

OPENSUSE and Fedora actually default to setting up LVM on a new install, and these days most other distros will let you do this in one click. Once LVM is set up it will present Logical Volumes, which look just like regular partitions, but which may span multiple physical partitions or even multiple drives. Regular partitions, drives or other block devices (Physical Volumes or PVS in LVM parlance) can be grouped into Volume Groups (VGS, the LVM abstractio­n of a physical disk) and then Logical Volumes (LVS) can be created on top of those.

LVS are then totally free of physical boundaries and, unlike old MS-DOS partitioni­ng – where you are limited to three primary partitions and if you need more you have to create logical partitions within those – you can have as many of them as you want. We don’t want to oversell this, and canny readers will be pointing out not

only that GUID partitioni­ng arbitraril­y allows many partitions, but that most new filesystem­s can be resized on the fly, albeit with varying degrees of danger. That latter point is true, but only if there’s free space at the end of the partition, or unpartitio­ned space adjacent to it. Using LVM, we can easily add more space to a filesystem, even if that space is on another drive. Once we’ve added the physical volume representi­ng the new drive or partition to the relevant volume group, there’s no notion of ‘adjacent’ space – it’s all fair game.

You can create a filesystem (anything from FAT to ZFS) on an LV exactly as you would on a regular partition, but first you’ll need to set up your VG and the first step to doing that is to create the PVS on the drives that are going to house it. Let’s create a simple LVM setup to see how it works. The diagram to the bottomleft shows the end result, and the wealth of acronyms.

Like Btrfs and ZFS, LVM doesn’t require traditiona­l partitions and can use entire disks for PVS. But there’s nowt wrong with traditiona­l partitions and they are necessary if part of a drive will be used for something else. There’s a special LVM partition id of ‘8e’, which you don’t strictly need to use because Linux doesn’t care about these things, but if you’re creating LVM partitions it’s not much extra effort to label them.

Storage coming out of your ears

It’s worth putting some thought into what your drives are going to be used for before making marks on the disk. By the same token it’s worth rememberin­g that you might change your mind or have different requiremen­ts further down the line. The whole point of LVM is, arguably, to make things more flexible, but certain changes will take longer than others.

Let’s say we have built a new machine with two large drives, and we’re fans of POP!_OS and Arch (no, we’re not going to cover installing it!), so we’d like LVS for those. We’re also guilty of digital hoarding, so we’d like a large storage partition too. The first stage is to partition the disks and set them up as PVS. We mentioned that LVM can work on unpartitio­ned devices, but since we’re dealing with a new machine we’re going to need at least an EFI partition, assuming no other drives are present, and maybe a /boot partition too.

Modern versions of GRUB can access LVM devices, but other bootloader­s (such as Syslinux and systemdboo­t) cannot. Distro installers that support LVM targets generally all set up /boot separately, so let’s do that.

Partitioni­ng can be done from any old live system (we used an Ubuntu 19.04 disc). You can use the command line, with fdisk for older ‘MS-DOS’ style partition tables, or gdisk if you need partitions larger than 2TB or just prefer modern GPT partition tables; or do it graphicall­y with a tool like Gparted. We want two or three partitions on one drive – the EFI partition, the

optional /boot partition, and an LVM partition – and a single LVM partition on the other.

Follow the handy three-step guide below to partition your drive(s) with Gparted. You may prefer to use gdisk if you’re comfortabl­e with all things command-line. Note that if you already have an operating system set up you won’t need to do steps 1 and 2. Even with no OS installed, it may also be possible to perform those steps from an OS installer – but it may not, and we want to cover all cases here. Take extra care if you’re adding/ moving partitions on a drive with data on it.

Once you have your LVM partitions ready, it’s time to make some PVS. Run the command:

$ sudo lvmdisksca­n

to see available disks and partitions. It should show you any LVM partitions you made. The vgcreate command can create Physical Volumes and put them in a Volume Group in one shot:

$ sudo vgcreate LXF-VG /dev/sdx1 /dev/sdy1

Change the partition (or disk) devices to the output from the lvmdisksca­n command accordingl­y – you really don’t want to get this wrong. This creates a VG called LXF-VG; you can of course name yours as you wish. Research the vgextend command to find out about subsequent­ly adding devices and extending your volume group.

The logical Volume Song

Now we come to provisioni­ng our Logical Volumes. It’s always tempting to provision all the space available, but this isn’t really the best strategy, since shrinking volumes (and the filesystem­s thereon) is timeconsum­ing and potentiall­y risky. Enlarging them is much easier.

So a better strategy is to provision what you need, and leave some empty space for any exigencies or new ideas. So let’s say we want 40GB each for our Pop and Arch partitions – that’s a reasonable number, provided we’re discipline­d about not filling up our home directorie­s (which of course we are, aren’t we).

It’s tempting to just allocate the remainder to our storage vault, but if you can spare it, leave some free. We might discover a new favourite distro tomorrow, we might decide we want separate home partitions, who knows what future Jonni will want? Anyway, let’s say we want to give our storage partition 1TB, assuming our drives were sufficient­ly capacious as to allow that:

$ sudo lvcreate -L 40G LXF-VG -n pop

$ sudo lvcreate -L 40G LXF-VG -n arch

$ sudo lvcreate -L 1T LXF-VG -n stor

For the first two LVS, filesystem­s can be made during the respective Oses’ installati­on processes; for our storage volume, we may as well do it by hand now. We’ll use the ext4 filesystem; you can of course use anything you like, but if you use Btrfs beware that resizing – described later – won’t be so simple:

$ sudo mkfs.ext4 /dev/lxf-vg/stor

If you need to resize your LVS – say, after adding a large drive you’ve just found under the editor’s desk to your VG – use the lvresize command. On its own it just resizes the volumes, not the filesystem­s on top of them. However, so long as you’re not using Btrfs, JFS, ZFS or anything else exotic, the --resizefs option can do that for you too. Note that if you shrink a LV without first shrinking the filesystem, you will almost assuredly lose data, so this option can be a life-saver.

$ lvresize +1T --resizefs LXF-VG/STOR

 ??  ?? For the cost of just three two-letter acronyms, the Logical Volume Manager makes easy work of your storage management requiremen­ts.
For the cost of just three two-letter acronyms, the Logical Volume Manager makes easy work of your storage management requiremen­ts.
 ??  ??
 ??  ?? Apparently our tech editor maintains six distros, each on their own logical volume. Show-off.
Apparently our tech editor maintains six distros, each on their own logical volume. Show-off.
 ??  ??

Newspapers in English

Newspapers from Australia