Difference between revisions of "LVM"
(→Reactivating a volume group) |
|||
Line 255: | Line 255: | ||
If things have gone a bit wrong, and you've had to manually fix stuff (with fdisk, and the like), then to reactivate the volume group, and get it to recognise the fixed LVs, simply run | If things have gone a bit wrong, and you've had to manually fix stuff (with fdisk, and the like), then to reactivate the volume group, and get it to recognise the fixed LVs, simply run | ||
<pre> | <pre> | ||
− | sudo vgchange -a y | + | sudo vgchange -a y /dev/vg1 |
</pre> | </pre> | ||
Revision as of 22:49, 23 April 2016
Contents
Overview
- One or more Physical Volumes (eg. /dev/sda2) are joined together in a Volume Group (eg. /dev/vg1)
- A Volume Group is split into one or more Logical Volumes (eg. /dev/vg1/media)
- Instead of dealing directly in KB/MB/GB, LVM expresses sizes as physical extents. The size of a single physical extent for a volume group can be seen by looking at the PE Size shown in vgdisplay.
- If you don't have a physical volume created yet, simply use fdisk
Required Packages (ubuntu)
- lvm2
Creating a partition > 2TB
- Run parted on the relevant disk:
sudo parted /dev/sde
- In parted, set the partition type to GPT, change the units to TB (needed to avoid warnings about misaligned partitions), and create a partition using all the space on the disk:
(parted) mklabel gpt Warning: The existing disk label on /dev/sde will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? y (parted) unit TB (parted) mkpart primary 0 -1 (parted) quit
Run 'print' at any time to check the current partition table.
Creating a new volume group & logical volume
To create a volume group (/dev/vg1) consisting of 1 physical volume (/dev/sda2), and split into only 1 logical volume (/dev/vg1/media):
- Setup partition as a physical volume:
sudo pvcreate /dev/sda2
NB. Although you can setup an entire disk as a physical volume (eg. via sudo pvcreate /dev/sda) this is not recommended due to possible confusion with other OSs which won't recognise the disk as being used.
- Create a volume group consisting of only this physical volume:
sudo vgcreate vg1 /dev/sda2
- Check volume group has been created successfully:
vgdisplay
This should show 1 volume group consisting of 0 LVs (see Cur LV) and 1 PV (see Cur PV). Note the number of free physical extents (PE) in the volume group (see Free PE).
- Create a logical volume with this number of free extents (if lvcreate complains about kernel modules not being loaded, run sudo modprobe dm-mod):
sudo lvcreate -l <num-extents> --name media vg1
- Format the logical volume as ext3:
mkfs.ext3 /dev/vg1/media
- Mount the volume by adding the following line to /etc/fstab:
/dev/vg1/media /var/media ext3 defaults 0 2
sudo mount /dev/vg1/media
Adding a physical volume to a volume group and logical volume
For example, to add /dev/sdb2 to /dev/vg1/media
- Setup partition as a physical volume:
sudo pvcreate /dev/sdb2
- Extend the volume group to cover the new physical volume:
sudo vgextend vg1 /dev/sdb2
- Check volume group has been changed successfully, and note the number of new free extents (see Free PE):
vgdisplay
- Unmount the logical volume:
sudo umount /dev/vg1/media
- Extend the logical volume:
sudo lvextend -l +<num-extents> /dev/vg1/media
- Check that the volume sizes look correct
sudo vgdisplay
- Resize the ext3 filesystem on /dev/vg1/media
sudo resize2fs /dev/vg1/media
- Mount the logical volume:
sudo mount /dev/vg1/media
Resizing a physical volume
For example, deleting /dev/sda3 and extending /dev/sda2 to use the free space. Note here that we are extending a physical volume, not just a logical one.
- Unmount any logical volumes which use /dev/sda2
sudo umount /dev/vg1/media
- Use fdisk to delete /dev/sda3, delete old /dev/sda2 and recreate as a larger partition (this will not actually delete any data from /dev/sda2)
$ sudo fdisk /dev/sda The number of cylinders for this disk is set to 38913. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): p Disk /dev/sda: 320.0 GB, 320072933376 bytes 255 heads, 63 sectors/track, 38913 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x08270827 Device Boot Start End Blocks Id System /dev/sda1 * 1 16708 134206976 7 HPFS/NTFS /dev/sda2 16708 20532 30716282+ 83 Linux /dev/sda3 20533 38913 147645380 7 HPFS/NTFS Command (m for help): d Partition number (1-4): 3 Command (m for help): p Disk /dev/sda: 320.0 GB, 320072933376 bytes 255 heads, 63 sectors/track, 38913 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x08270827 Device Boot Start End Blocks Id System /dev/sda1 * 1 16708 134206976 7 HPFS/NTFS /dev/sda2 16708 20532 30716282+ 83 Linux Command (m for help): d Partition number (1-4): 2 Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First sector (268414015-625142447, default 268414015): Using default value 268414015 Last sector or +size or +sizeM or +sizeK (268414015-625142447, default 625142447): Using default value 625142447 Command (m for help): p Disk /dev/sda: 320.0 GB, 320072933376 bytes 255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors Units = sectors of 1 * 512 = 512 bytes Disk identifier: 0x08270827 Device Boot Start End Blocks Id System /dev/sda1 * 63 268414014 134206976 7 HPFS/NTFS /dev/sda2 268414015 625142447 178364216+ 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks.
- If fdisk warned you that the kernel still uses the old table, you will need to reboot now. Remember to unmount relevant logical volumes again after rebooting.
- Resize the LVM physical volume to match the new partition size
sudo pvresize /dev/sda2
- Check how many free extents exist in the volume group - you should see extents corresponding to the increase in size of your physical partition
sudo vgdisplay
- Add this many extents to /dev/vg1/media
sudo lvextend -l +<num-extents> /dev/vg1/media
- Check that the volume sizes look correct
sudo vgdisplay
- Resize the ext3 filesystem on /dev/vg1/media
sudo resize2fs /dev/vg1/media
- Remount affected filesystems
sudo mount /dev/vg1/media
- Check that everything looks ok :)
Shrink a logical volume
- First, reduce the filesystem size to slightly smaller than you want the final size to be:
sudo resize2fs /dev/mapper/vg1-backup 390G resize2fs 1.40.8 (13-Mar-2008) Resizing the filesystem on /dev/mapper/vg1-backup to 102236160 (4k) blocks. The filesystem on /dev/mapper/vg1-backup is now 102236160 blocks long.
- Reduce the size of the logical volume to the intended size:
sudo lvreduce -L 400G /dev/vg1/backup WARNING: Reducing active logical volume to 400.00 GB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce backup? [y/n]: y Reducing logical volume backup to 400.00 GB Logical volume backup successfully resized
- Resize the filesystem to match the new logical volume size (you may be asked to run e2fsck first):
sudo resize2fs /dev/mapper/vg1-backup resize2fs 1.40.8 (13-Mar-2008) Resizing the filesystem on /dev/mapper/vg1-backup to 104857600 (4k) blocks. The filesystem on /dev/mapper/vg1-backup is now 104857600 blocks long.
Reactivating a volume group
If things have gone a bit wrong, and you've had to manually fix stuff (with fdisk, and the like), then to reactivate the volume group, and get it to recognise the fixed LVs, simply run
sudo vgchange -a y /dev/vg1
Sample vgdisplay Output
--- Volume group --- VG Name vg1 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 5 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 1 Act PV 1 VG Size 170.10 GB PE Size 4.00 MB Total PE 43545 Alloc PE / Size 43545 / 170.10 GB Free PE / Size 0 / 0 VG UUID TACrPR-xO6J-mHz0-hiMn-ax3G-R3a4-dv2ivp
Useful Commands
- vgdisplay
- Display volume group information
- pvdisplay
- Display physical volume information
- lvdisplay
- Display logical volume information
- pvdisplay -m
- Show PV -> LV mapping
- lvdisplay -m
- Show LV -> PV mapping
- lvs -o +devices
- Show concise LV -> PV mapping