Mostrando entradas con la etiqueta LVM. Mostrar todas las entradas
Mostrando entradas con la etiqueta LVM. Mostrar todas las entradas

domingo, 5 de marzo de 2023

LVM Snapshots

 


LVM snapshots create an exact copy of an existing logical volume. They are useful for backups, application testing, and deploying VMs (Virtual Machines). The original state of the snapshot is kept as the block map.

Snapshots only use space for storing deltas:

  • When the original logical volume changes, original data blocks are copied to the snapshot
  • If data is added to snapshot, it is stored only there

To create a snapshot of an existing logical volume use this command:

$ sudo lvcreate -l 128 -s -n mysnap /dev/vg/mylvm

To then make a mount point and mount the snapshot run the following command:

$ mkdir /mysnap
$ mount -o ro /dev/vg/mysnap /mysnap

To use the snapshot and to remove the snapshot do:

$ sudo umount /mysnap
$ sudo lvremove /dev/vg/mysnap

Always be sure to remove the snapshot when you are through with it. If you do not remove the snapshot and it fills up because of changes, it will be automatically disabled. A snapshot with the size of the original will never overflow.

Resizing Logical Volumes

One great advantage of using LVM is that it is easy and quick to change the size of a logical volume, especially when compared with trying to do this with a physical partition that already contains a filesystem. When doing this, extents can be added or subtracted from the logical volume, and they can come from anywhere in the volume group; they need not be from physically contiguous sections of the disk.

If the volume contains a filesystem, expanding or shrinking it is an entirely different operation than changing the size of the volume. When expanding a logical volume with a filesystem, you must first expand the volume, and then expand the filesystem.​ When shrinking a logical volume with a filesystem, you must first shrink the filesystem, and then shrink the volume.

This is best done with lvresize, as in the following command:

$ sudo lvresize -r -L 20 GB /dev/VG/mylvm

where the -r option causes resizing of the filesystem at the same time as the volume size is changed.

To grow a logical volume with an ext4 filesystem, run the following command:

$ sudo lvresize -r -L +100M /dev/vg/mylvm

where the plus sign (+) indicates adding space. Note that you need not unmount the filesystem to grow it.

To shrink the filesystem, run the following command:

$ sudo lvresize -r -L 200M /dev/vg/mylvm

You can also reduce a volume group as in:

$ sudo pvmove /dev/sdc1
$ sudo vgreduce vg /dev/sdc1

The filesystem cannot be mounted when being shrunk. However, some filesystems permit expansion while they are mounted.

The utilities which change the filesystem size are filesystem-dependent; besides lvresize, we can also use lvextend, lvreduce with resize2fs. 

Creating Logical Volumes

lvcreate allocates logical volumes from within volume groups. The size can be specified either in bytes or number of extents (remember, they are 4 MB by default). Names can be anything desired.

lvdisplay reports on available logical volumes.

Filesystems are placed in logical volumes and are formatted with mkfs, as usual.

Starting with possibly creating a new volume group, the steps involved in setting up and using a new logical volume are:

  1. Create partitions on disk drives (type 8e in fdisk).
  2. Create physical volumes from the partitions.
  3. Create the volume group.
  4. Allocate logical volumes from the volume group.
  5. Format the logical volumes.
  6. Mount the logical volumes (also update the /etc/fstab file as needed).

For example, assuming you have already created partitions /dev/sdb1 and /dev/sdc1 and given them type 8e, the steps involved in setting up and using a new logical volume are:

$ sudo pvcreate /dev/sdb1
$ sudo pvcreate /dev/sdc1
$ sudo vgcreate -s 16M vg /dev/sdb1
$ sudo vgextend vg /dev/sdc1
$ sudo lvcreate -L 50G -n mylvm vg
$ sudo mkfs -t ext4 /dev/vg/mylvm
$ sudo mkdir /mylvm
$ sudo mount /dev/vg/mylvm /mylvm

Be sure to add the line:

/dev/vg/mylvm /mylvm ext4 defaults 1 2

to /etc/fstab to make this a persistent mount.


------------------------------