Difference between revisions of "Setting up Logical Volumes with LVM"

From Alpine Linux
Jump to: navigation, search
(corrected for 1.9: start localmount before swap; added rc-update commands)
(Start LVM during Boot)
 
(12 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
[[Category:Storage]]
 +
{{Tip|This process can be done automatically by using the [[setup-disk]] script, using the -L option.}}
 
This document how to create logical volumes in Alpine using lvm2.
 
This document how to create logical volumes in Alpine using lvm2.
  
 
LVM is collection of programs that allow larger physical disks to be reassembled into "logical" disks that can be shrunk or expanded as data needs change.
 
LVM is collection of programs that allow larger physical disks to be reassembled into "logical" disks that can be shrunk or expanded as data needs change.
  
In this document we will use a [[Setting up a software raid1 array | software raid1 device]] as physical storage for our logical volumes. We will set up a swap partition and a data partition for [[ Setting up a basic vserver | vservers ]]
+
In this document we will use a [[Setting up a software RAID array|software RAID1 device]] as physical storage for our logical volumes. We will set up a swap partition and a data partition for [[Setting up a basic vserver|vservers ]]
 
=== Installing LVM software ===
 
=== Installing LVM software ===
 
First we need to load the kernel driver, ''dm-mod''
 
First we need to load the kernel driver, ''dm-mod''
  
modprobe dm-mod
+
{{Cmd|modprobe dm-mod}}
  
 
We also want it to be loaded during next reboot.
 
We also want it to be loaded during next reboot.
  
echo dm-mod >> /etc/modules
+
{{Cmd|echo dm-mod >> /etc/modules-load.d/dm.conf}}
  
 
We also need the userspace programs.
 
We also need the userspace programs.
apk_add lvm2
+
 
 +
{{Cmd|apk add lvm2}}
  
 
=== Preparing the physical volumes ===
 
=== Preparing the physical volumes ===
First we need to tell LVM that de partition is available as a physical volume and can be added to a volume group. In this example we use a software raid array as physical volume.
+
First we need to tell LVM that the partition is available as a physical volume and can be added to a volume group. In this example we use a software raid array as physical volume.
pvcreate /dev/md0
+
 
 +
{{Cmd|pvcreate /dev/md0}}
  
 
=== Preparing the Volume Group ===
 
=== Preparing the Volume Group ===
 
We can then create a volume group and add the physical volume ''/dev/md0''
 
We can then create a volume group and add the physical volume ''/dev/md0''
vgcreate vg0 /dev/md0
+
 
 +
{{Cmd|vgcreate vg0 /dev/md0}}
  
 
If we later need more space we can add additional physcal volumes with ''vgextend''. All physcal disks/partitions added need to be prepared with ''pvcreate''.
 
If we later need more space we can add additional physcal volumes with ''vgextend''. All physcal disks/partitions added need to be prepared with ''pvcreate''.
Line 28: Line 33:
 
=== Creating Logical volumes ===
 
=== Creating Logical volumes ===
 
In the volume group we can create logical volumes. To create a 1GB volume called ''swap'' and a 6GB volume called 'vservers'' on the volume group ''vg0'' we run
 
In the volume group we can create logical volumes. To create a 1GB volume called ''swap'' and a 6GB volume called 'vservers'' on the volume group ''vg0'' we run
lvcreate -n swap -L 1G vg0
+
 
lvcreate -n vservers -L 6G vg0
+
{{Cmd|lvcreate -n swap -L 1G vg0
 +
lvcreate -n vservers -L 6G vg0}}
  
 
=== Display Logical Volumes ===
 
=== Display Logical Volumes ===
Line 65: Line 71:
 
=== Rename Logical Volumes ===
 
=== Rename Logical Volumes ===
  
lvrename /dev/vg0/vservers /dev/vg0/database
+
{{Cmd|lvrename /dev/vg0/vservers /dev/vg0/database}}
  
 
=== Extend Logical Volumes ===
 
=== Extend Logical Volumes ===
 
If you want to add space and the volume has the room for it...
 
If you want to add space and the volume has the room for it...
  
lvextend -L +50G /dev/vg0/vservers
+
{{Cmd|lvextend -L +50G /dev/vg0/vservers}}
  
 
If you want to set the space to a new larger size...
 
If you want to set the space to a new larger size...
  
lvextend -L 10G /dev/vg0/vservers
+
{{Cmd|lvextend -L 10G /dev/vg0/vservers}}
  
 
=== Start LVM during Boot ===
 
=== Start LVM during Boot ===
 
We want lvm to init the logical volumes during boot. There is a boot service named ''lvm'' to do this. If your volumes are on raid, make sure that ''/etc/init.d/lvm'' is started after mdadm-raid.
 
We want lvm to init the logical volumes during boot. There is a boot service named ''lvm'' to do this. If your volumes are on raid, make sure that ''/etc/init.d/lvm'' is started after mdadm-raid.
rc-update add lvm
+
 
 +
{{Cmd|rc-update add lvm boot}}
  
 
Or, on Alpine Linux 1.8 or earlier:
 
Or, on Alpine Linux 1.8 or earlier:
rc_add -s 12 -k lvm
+
 
 +
{{Cmd|rc_add -s 12 -k lvm}}
  
 
=== Setting up swap ===
 
=== Setting up swap ===
 
Now we have our devices in /dev/vg0 and can use them as normal disk paritions. To set up swap:
 
Now we have our devices in /dev/vg0 and can use them as normal disk paritions. To set up swap:
  
mkswap /dev/vg0/swap
+
{{Cmd|mkswap /dev/vg0/swap}}
  
 
Add the following line to your ''/etc/fstab'':
 
Add the following line to your ''/etc/fstab'':
 +
 
  /dev/vg0/swap  none            swap    sw    0 0
 
  /dev/vg0/swap  none            swap    sw    0 0
 
  
 
=== Setting up /vservers partition ===
 
=== Setting up /vservers partition ===
Line 97: Line 105:
 
Install xfsprogs.
 
Install xfsprogs.
  
apk_add xfsprogs
+
{{Cmd|apk add xfsprogs}}
  
 
Create filesystem on /dev/vg0/vservers.
 
Create filesystem on /dev/vg0/vservers.
mkfs.xfs /dev/vg0/vservers
+
 
 +
{{Cmd|mkfs.xfs /dev/vg0/vservers}}
  
 
Add the mount information to your /etc/fstab: NOTE:tagxid may cause this not to mount. Try this by hand and check dmesg to see if there are any errors
 
Add the mount information to your /etc/fstab: NOTE:tagxid may cause this not to mount. Try this by hand and check dmesg to see if there are any errors
 +
 
  /dev/vg0/vservers /vservers    xfs    noatime,tagxid 0 0
 
  /dev/vg0/vservers /vservers    xfs    noatime,tagxid 0 0
  
Line 109: Line 119:
 
=== Starting localmount and swap ===
 
=== Starting localmount and swap ===
  
Now we can start the ''localmount'' boot service to mount our partition.
+
Now we can mount our partition.
/etc/init.d/localmount start
+
{{Cmd|mount /vservers}}
 +
 
 +
Make sure we run ''localmount'' during boot too, and that it is done after lvm. In Alpine Linux 1.9 and newer this should not be needed
  
Make sure we run ''localmount'' during boot too, and that it is done after lvm.
+
{{Cmd|rc-update add localmount boot}}
rc-update add localmount
 
  
 
Or, on Alpine Linux 1.8 or earlier:
 
Or, on Alpine Linux 1.8 or earlier:
rc_add -s 14 -k localmount
 
  
 +
{{Cmd|rc_add -s 14 -k localmount}}
  
 
Start the swap service and make sure it starts during next reboot and that it starts '''after''' lvm.
 
Start the swap service and make sure it starts during next reboot and that it starts '''after''' lvm.
  
/etc/init.d/swap start
+
{{Cmd|/etc/init.d/swap start
rc-update add swap
+
rc-update add swap}}
  
 
Or, on Alpine Linux 1.8 or earlier:
 
Or, on Alpine Linux 1.8 or earlier:
/etc/init.d/swap start
+
{{Cmd|/etc/init.d/swap start
rc_add -s 14 -k swap
+
rc_add -s 14 -k swap}}
  
 +
=== More Info on LVM ===
 +
These resources may be helpful:
  
=== More Info on LVM ===
+
* the [http://tldp.org/HOWTO/LVM-HOWTO/commontask.html common tasks] section in the [http://tldp.org/HOWTO/LVM-HOWTO/index.html LVM Howto]
For more information, have a look at the [http://tldp.org/HOWTO/LVM-HOWTO/commontask.html common tasks] section in the [http://tldp.org/HOWTO/LVM-HOWTO/index.html LVM Howto].
+
* [http://wiki.alpinelinux.org/wiki/LVM_on_LUKS Alpine wiki page for LVM on LUKS]
 +
* [https://wiki.archlinux.org/index.php/LVM Arch wiki page on LVM]
 +
* [https://wiki.archlinux.org/index.php/Software_RAID_and_LVM Arch wiki page on RAID and LVM]

Latest revision as of 13:36, 2 June 2020

Tip: This process can be done automatically by using the setup-disk script, using the -L option.

This document how to create logical volumes in Alpine using lvm2.

LVM is collection of programs that allow larger physical disks to be reassembled into "logical" disks that can be shrunk or expanded as data needs change.

In this document we will use a software RAID1 device as physical storage for our logical volumes. We will set up a swap partition and a data partition for vservers

Installing LVM software

First we need to load the kernel driver, dm-mod

modprobe dm-mod

We also want it to be loaded during next reboot.

echo dm-mod >> /etc/modules-load.d/dm.conf

We also need the userspace programs.

apk add lvm2

Preparing the physical volumes

First we need to tell LVM that the partition is available as a physical volume and can be added to a volume group. In this example we use a software raid array as physical volume.

pvcreate /dev/md0

Preparing the Volume Group

We can then create a volume group and add the physical volume /dev/md0

vgcreate vg0 /dev/md0

If we later need more space we can add additional physcal volumes with vgextend. All physcal disks/partitions added need to be prepared with pvcreate.

Creating Logical volumes

In the volume group we can create logical volumes. To create a 1GB volume called swap and a 6GB volume called 'vservers on the volume group vg0 we run

lvcreate -n swap -L 1G vg0 lvcreate -n vservers -L 6G vg0

Display Logical Volumes

You can now se the logical volumes with the lvdisplay utility.

lvdisplay
 --- Logical volume ---
 LV Name                /dev/vg0/swap
 VG Name                vg0
 LV UUID                a4NYOi-FQP6-Lj5Q-0TYk-Jjtk-Qxjt-nxeBPn
 LV Write Access        read/write
 LV Status              available
 # open                 0
 LV Size                1.00 GB
 Current LE             256
 Segments               1
 Allocation             inherit
 Read ahead sectors     0
 Block device           253:0
  
 --- Logical volume ---
 LV Name                /dev/vg0/vservers
 VG Name                vg0
 LV UUID                16VMmy-7I0s-eeoW-tL2V-JrlN-jM6C-d0wEg0
 LV Write Access        read/write
 LV Status              available
 # open                 0
 LV Size                6.00 GB
 Current LE             1536
 Segments               1
 Allocation             inherit
 Read ahead sectors     0
 Block device           253:1

Rename Logical Volumes

lvrename /dev/vg0/vservers /dev/vg0/database

Extend Logical Volumes

If you want to add space and the volume has the room for it...

lvextend -L +50G /dev/vg0/vservers

If you want to set the space to a new larger size...

lvextend -L 10G /dev/vg0/vservers

Start LVM during Boot

We want lvm to init the logical volumes during boot. There is a boot service named lvm to do this. If your volumes are on raid, make sure that /etc/init.d/lvm is started after mdadm-raid.

rc-update add lvm boot

Or, on Alpine Linux 1.8 or earlier:

rc_add -s 12 -k lvm

Setting up swap

Now we have our devices in /dev/vg0 and can use them as normal disk paritions. To set up swap:

mkswap /dev/vg0/swap

Add the following line to your /etc/fstab:

/dev/vg0/swap   none            swap     sw    0 0

Setting up /vservers partition

Finally we want to set up an XFS partition for /vservers.

Install xfsprogs.

apk add xfsprogs

Create filesystem on /dev/vg0/vservers.

mkfs.xfs /dev/vg0/vservers

Add the mount information to your /etc/fstab: NOTE:tagxid may cause this not to mount. Try this by hand and check dmesg to see if there are any errors

/dev/vg0/vservers /vservers     xfs     noatime,tagxid 0 0

Note that the tagxid option is specific for setting up vserver disk limits so it might be you don't want it. The noatime option is to increase performance but you will no longer know when files were accessed last time.

Starting localmount and swap

Now we can mount our partition.

mount /vservers

Make sure we run localmount during boot too, and that it is done after lvm. In Alpine Linux 1.9 and newer this should not be needed

rc-update add localmount boot

Or, on Alpine Linux 1.8 or earlier:

rc_add -s 14 -k localmount

Start the swap service and make sure it starts during next reboot and that it starts after lvm.

/etc/init.d/swap start rc-update add swap

Or, on Alpine Linux 1.8 or earlier:

/etc/init.d/swap start rc_add -s 14 -k swap

More Info on LVM

These resources may be helpful: