LVM (Italiano)

From ArchWiki
Revision as of 16:25, 12 February 2009 by Dav2dev (Talk | contribs) (un po' di traduzioni alla rinfusa)

Jump to: navigation, search


Introduzione

LVM (Logical Volume Manager) è una tecnologia usata da GNU/Linux per astrarre lo spazio di memoria per archiviazione creando dischi e partizioni "virtuali", più facili da modificare rispetto al classico partizionamento dei dischi.

In LVM sono presenti:

  • Physical volume (PV): Una partizione (o anche l'intero disco o un file di loopback) che ospita una parte di un virtual group. It has a special header and is divided into physical extents. Think of physical volumes as big building blocks which can be used to build your hard drive.
  • Volume group (VG): Gruppo di physical volume considerati come una unità da LVM. Un volume group contiene i logical volume ed è l'omologo del disco rigido.
  • Logical volume(LV): In LVM è l'omologo della partizione. È composto di physical extents.
  • Physical extent (PE): Piccola parte di un disco (di solito 4 MB) che può essere assegnata a un logical volume.

I volumi logici di LVM sono più facili da gestire delle normali partizioni. Per esempio, è possibile:

  • Usare diversi dischi rigidi come se fossero uno solo (VG)
  • Distribuire le "partizioni" (LV) su più dischi.
  • Ridimensionare/creare/eliminare a piacere le "partizioni" (LV) e i "dischi" (VG), senza preoccuparsi della posizione dei LV all'interno dei VG.
  • Ridimensionare/creare/eliminare partizioni (LV) e dischi (VG) online (i filesystem su di essi vanno ridimensionati a parte, anche se non tutti permettono di farlo online)
  • Usare nomi arbitrari per i VG e i LV
  • Creare LV piccoli e ingrandirli in seguito se necessario
  • ...

Example:

Physical disks
                
  Disk1 (/dev/sda):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    |Partition1 50GB (Physical volume) |Partition2 80GB (Physical volume)     |
    |/dev/sda1                         |/dev/sda2                             |
    |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
                                  
  Disk2 (/dev/sdb):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    |Partition1 120GB (Physical volume)                 |
    |/dev/sdb1                                          |
    | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _|
LVM logical volumes

  Volume Group1 (/dev/MyStorage/ = /dev/sda1 + /dev/sda2 + /dev/sdb1):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    |Logical volume1 15GB  |Logical volume2 35GB      |Logical volume3 200GB               |
    |/dev/MyStorage/rootvol|/dev/MyStorage/homevol    |/dev/MyStorage/mediavol             |
    |_ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |

To sum it all up: With LVM you can use all your storage space as one big disk (volume group) and have more flexibility over your partitions (logical volumes).

Installazione

Before doing anything we need to load the appropriate module:

# modprobe dm-mod

If you already have Arch Linux installed and just you want to add/try a partition with LVM jump to partition disks.

Installare Arch Linux su LVM

Partizionare i dischi

Creare le partizioni da dedicare a LVM. Il tipo (type nel menu di cfdisk) di partizione da scegliere è 'Linux LVM', che ha codice '8E'.

Dal momento che GRUB (0.9x) non supporta LVM, bisogna creare una partizione a parte per la directory /boot. In alternativa è possibile usare GRUB2 (1.9x) o Lilo, che sono compatibili con LVM.


Creare i Volumi Fisici (PV)

Un Volume fisico è una partizione inizializzata per essere usata da LVM mediante il comando:

# pvcreate /dev/sda2

ripetere il comando per tutte le partizioni che si intende usare con LVM. Per visualizzare i Volumi Fisici attualmente presenti:

# pvdisplay

Creare i Gruppi di Volumi (VG)

Un Gruppo di Volumi (VG) è un nuovo dispositivo di archiviazione "virtuale" formato da uno o più Volumi Fisici (PV). Si inizia creando un VG su un PV e poi si aggiungono altri PV allo stesso VG, in modo da aumentarne la capacità:

# vgcreate VolGroup00 /dev/sda2
# vgextend VolGroup00 /dev/sdb1

Al posto di VolGroup00 si può usareun nome qualunque.

Per visualizzare i VG creati e la loro composizione:

# vgdisplay

Creare i Volumi Logici (LV)

Un Volume Logico (LV) è l'omologo della partizione, quindi al suo interno verrà creato un filesystem e salvati file. Per creare un LV si usa il seguente comando:

# lvcreate VolGroup00 -L 10G -n lvolhome

Il nuovo LV ha nome lvolhome (ma si può usare un nome a piacere), è grande 10GB, si trova sul VG VolGroup00 e vi si può accedere tramite il percorso /dev/VolGroup00/lvolhome o anche /dev/mapper/Volgroup00-lvolhome.

Per visualizzare tutti gli LV presenti:

# lvdisplay

Attenzione: per creare un LV da usare come swap, usare il comando:

# lvcreate -C y -L 10G VolGroup00 -n lvolswap

L'opzione -C y serve ad assegnare dello spazio contiguo al LV, in modo da velocizzarne l'accesso.

Suggerimento: per creare un LV usando tutto lo spazio rimasto nel VG, usare:

# lvcreate -l +100%FREE VolGroup00 -n lvolmedia

Create filesystem and mount logical volumes

Your logical volumes should now be located in /dev/mapper/ and /dev/YourVolumeGroupName. If you can't find them use the next commands to bring up the module for creating device nodes and to make virtual groups availabile:

# modprobe dm-mod
# vgchange -ay

Now you can create filesystems on logical volumes and mount them as normal partitions (if you are installing Arch linux, skip this step):

# mkfs.ext3 /dev/VolGroup00/home
# mount /dev/VolGroup00/lvolhome /home

If you are installing Arch linux, start /arch/setup, go to Prepare Hard Drive directly to step 3 Set Filesystem Mountpoints and read the important section below before proceeding with installation!

Important

There are just a few things you need to be careful while using/installing Arch Linux with LVM (in brackets are the corresponding menus at installation):

  1. Don't use any of the partitions on which you have created a physical volume for mountpoints (example: /dev/sda2). Use only logical volumes on them (example: /dev/mapper/Volgroup00-lvolhome). (Set Filesystem Mountpoints)
  2. Make sure you change USELVM="no" to USELVM="yes" in /etc/rc.conf if you are using a logical volume for your root partition. (Configure System)
  3. Make sure that lvm2 is in the HOOKS section of /etc/mkinitcpio.conf just before the filesystems so that your kernel will find LVM volumes at boot time. (Configure System)
  4. Make sure /boot/grub/menu.lst uses the right volumes for root. It should look something like this: (Install Bootloader)
...
# (0) Arch Linux
title  Arch Linux
root   (hd0,0)
kernel /vmlinuz26 root=/dev/mapper/VolGroup00-lvolroot resume=/dev/mapper/VolGroup00-lvolswap ro
initrd /kernel26.img
...

If you are using LILO check /etc/lilo.conf:

image=/boot/vmlinuz26
       label=arch
       append="root=/dev/mapper/VolGroup00-lvolroot resume=/dev/mapper/VolGroup00-lvolswap ro"
       initrd=/boot/kernel26.img

Configuration

Grow logical volume

To grow a logical volume you first need to grow the logical volume and then the filesystem to use the newly created free space. Let's say we have a logical volume of 15GB with ext3 on it and we want to grow it to 20G. We need to do the following steps:

# lvextend -L 20G VolGroup00/lvolhome (or lvextend -L +5G VolGroup00/lvolhome)
# resize2fs /dev/VolGroup00/lvolhome

You may use lvresize insted of lvextend.

Attention: Not all filesystem support growing without loss of data and/or growing online.

Information: If you don't resize your filesystem, you will still have a volume with the same size as before (volume will be bigger but partly unused).

Hint: If you want to fill all the free space on a volume group use the next command:

# lvextend -l +100%FREE VolGroup00/lvolhome

Shrink logical volume

Because your filesystem is probably as big as logical volume it resides on, you need to shrink the filesystem first and then shrink the logical volume. Depending on your filesystem, you may need to unmount it first. Let's say we have a logical volume of 15GB with ext3 on it and we want to shrink it to 10G. We need to do the following steps:

# resize2fs /dev/VolGroup00/lvolhome 9G
# lvreduce -L 10G VolGroup00/lvolhome (or lvreduce -L -5G VolGroup00/lvolhome)
# resize2fs /dev/VolGroup00/lvolhome

Here we shrunk the filesystem more than needed so that when we shrunk the logical volume we didn't accidentally cut of the end of the filesystem. After that we normally grow the filesystem to fill all free space left on logical volume. You may use lvresize insted of lvreduce.

Attention: Don't reduce the filesystem size to less than it is used by data on it or you can lose your data.

Attention: Not all filesystems support shrinking without loss of data and/or shrinking online.

Attention: It is better to reduce the filesystem to lower size than the logical volume, so that after a resizing logical volume, we don't accidentally cut off some data from the end of the filesystem.

Add partition to a volume group

To add partition to you volume group you must first make its type 'Linux LVM' (for example with cfdisk). Then you need to create physical volume on it and extend volume group over it:

# pvcreate /dev/sdb1
# vgextend VolGroup00 /dev/sdb1

Now you have free space in your volume group that can be used by logical volumes in this group.

Information: You can add partitions from any disks to volume groups.

Remove partition from a volume group

All of the data on that partition needs to be moved to another partition. Fortunately, lvm makes this easy:

# pvmove /dev/mapper/myvg-mypv

If you want to have the data on a specific physical volume, specify that as the second argument to pvmove.

Then the physical volume needs to be removed from the volume group:

# vgreduce myVg /dev/mapper/myvg-mypv

Or remove all empty physical volumes:

# vgreduce --all vg0

And lastly, if you want to use the partition for something else, and want to avoid lvm thinking that the partition is a physical volume:

# pvremove /dev/mapper/myvg-removedpv

Snapshots

Introduction

LVM allows you to take a snapshot of your system in a much more efficient way than a traditional backup. It does this efficiently by using a COW (copy-on-write) policy. The initial snapshot you take simply contains hard-links to the inodes of your actual data. So long as your data remains unchanged, the snapshot merely contains there inode pointers and not the data itself. Whenever you modify a file or directory that the snapshot points to, LVM automatically clones the data, the old copy referenced by the snapshot, and the new copy referenced by your active system. Thus, you can snapshot a system with 35GB of data using just 2GB of free space so long as you modify less than 2GB (on both the original and snapshot).

Configuration

You create snapshot logical volumes just like normal ones.

# lvcreate --size 100M --snapshot --name snap01 /dev/mapper/vg0-pv

With that volume, you may modify less than 100M of data, before the snapshot volume fills up.

Todo: scripts to automate snapshots of root before updates, to rollback... updating menu.lst to boot snapshots (separate article?)

snapshots are primarily used to provide a frozen copy of a filesystem to make backups; a backup taking two hours provides a more consistent image of the filesystem than directly backing up the partition.

Troubleshooting

LVM commands don't work

try preceeding commands with lvm like this:

# lvm pvdisplay

Tips & Tricks

Todo

More Resources

Other LVM articles on the Archwiki:

External resources: