• No results found

Using Advanced LVM Features

In document Beginning Ubuntu Server Administration (Page 115-117)

You can easily resize existing volumes in an LVM environment. It’s also possible to create a snapshot of a volume. Let’s explore how to do that.

Resizing Logical Volumes

When resizing logical volumes, you should be aware that the procedure always involves two steps: you need to resize both the volume as well as the file system that is used on the volume. Of all the different file systems, ReiserFS and Ext3 support resizing with the fewest problems. The following procedure details how the volume is first brought offline and then the file sys- tem that sits on the volume is resized. It is presumed that the volume you want to shrink is called data and it is using an Ext3 file system. It is mounted on the directory /data.

Caution

Online resizing of a file system is possible in some cases. For example, the command ext2onlinemakes it possible to resize a live file system. However, because resizing file systems is very labor intensive, I wouldn’t recommend doing it this way. There’s always a risk that it won’t work out sim- ply because all of the work that has to be done. So, to stay on the safe side,umountyour volume before resizing it.

1. Use umount /datato unmount the volume from the directory /data.

2. Before shrinking the volume itself, you must shrink the file system used on it. Use

resize2fs /dev/system/data 2Gto make it a 2 GB file system.

4. Finally, you can mount the volume again. Use mount /dev/system/data /data.

5. Use the df -hcommand to show the current size of the file system. It should be a gigabyte smaller than it was before.

In this procedure, you learned how to shrink a volume, and of course you can increase its size as well. When increasing a volume, you just have to invert the order of the steps. First, you need to extend the size of the volume, and then the size of the file system can be increased as well. After dismounting the volume, this is a two-step procedure:

1. Use lvextend -L+10G /dev/system/datato add 10 GB of available disk space from the volume group to the volume.

2. Next, use resize_reiserfs -f /dev/system/data. This command will automatically increase the Reiser file system that is sitting in the volume to the maximum amount of available disk space.

You now know how to resize a volume with a Reiser file system in it. Of course, you can resize Ext3 and Ext2 as well. To increase the size of an Ext3 file system, you would use

resize2fs -f /dev/system/data.

Creating LVM Snapshots

One of the best features of LVM is the possibility to make snapshots. A snapshot is a new block device that functions as a complete copy of the original volume. This works without a com- plete copy being made: only changes are written to the snapshot and therefore a snapshot can be very efficient in its use of disk space.

A snapshot captures the file system metadata that is used to provide an overview of all existing files on a device and the blocks on a device that are occupied or free. So, initially, the snapshot records only administrative information that is used to tell what file is at what loca- tion. Because of the close relation between the original device and its snapshot, all reads to the snapshot device are redirected to the original device.

When writing anything to the original device, a backup of the old data is written to the snapshot device. Therefore, the snapshot volume will contain the original status, whereas the original volume will always include the changed status. The advantage of this technique is that it requires a very limited amount of disk space. For example, if you create a snapshot of a 100 GB volume to exist only for an hour, it must be large enough to keep the file system’s meta- data as well as all data that is changed within that hour. In most cases, this means that a 5 GB snapshot is more than enough. If, however, your snapshot has to exist for a longer period, the amount of disk space that is used by the snapshot will be larger.

Using snapshot technology can also be very convenient for making backups of volumes that cannot be closed. Imagine, for example, the data volume of a mail server: you cannot just take the mail server down for a couple of hours to make a backup. The solution then is to make a snapshot of the original volume, back up the snapshot volume (which contains the frozen state of the logical volume at the moment the snapshot was made) and, when the backup is finished, remove the snapshot again. This is even something that you can put in

a shell script and configure with cronso that it runs automatically every night. The procedure described next shows you how to create a snapshot of an existing LVM volume.

1. In the first step, you are using the lvcreatecommand to make a snapshot volume for the original volume /dev/system/data. The snapshot gets the name databackup. Because the original volume is in the system volume group, the snapshot will be created from that group as well. Do this by using the command lvcreate -L500M -s -n databackup /dev/system/data. Here, the option -L500Mmakes the snapshot 500 MB, -smakes it a snapshot volume, and -nuses the name databackup. Finally, /dev/system/datarefers to the original volume that will be captured in the snapshot.

Tip

Problems creating the snapshot volume? Make sure that the kernel module dm_snapshot is loaded! Check this with the lsmodcommand, and, if it isn’t loaded, load it manually with modprobe dm_snapshot.

2. If next you want to create a backup of the volume, first mount it. Do this the same way that you would mount any other volume, such as with mount /dev/system/databackup /somewhere.

3. To create a backup from the snapshot volume, use your regular backup, or for example

tar. To write a backup to a rewindable tape device, you would use tar -cvf /dev/rmt0 /somewhere.

4. Finished making the backup? Then you can remove the snapshot with lvremove /dev/system/databackup. Of course, this works only after you have unmounted the snapshot device.

In document Beginning Ubuntu Server Administration (Page 115-117)