Difference between revisions of "LVM"

From Briki
Jump to: navigation, search
(Resizing a physical volume)
(Resizing a physical volume)
Line 133: Line 133:
 
<pre>
 
<pre>
 
sudo lvextend -l +<num-extents> /dev/vg1/media''
 
sudo lvextend -l +<num-extents> /dev/vg1/media''
 +
</pre>
 +
 +
* Check that the volume sizes look correct
 +
<pre>
 +
sudo vgdisplay
 
</pre>
 
</pre>
  
Line 139: Line 144:
 
sudo resize2fs /dev/vg1/media
 
sudo resize2fs /dev/vg1/media
 
</pre>
 
</pre>
 
* Check that the volumes look correct with ''sudo vgdisplay''
 
  
 
* Remount affected filesystems
 
* Remount affected filesystems

Revision as of 10:58, 21 August 2007

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)

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
  • 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).

  • Create a logical volume:
sudo lvcreate --size 30g --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


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 /var/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). If fdisk warns you that the partition table will not be written until the next reboot, you will need to reboot now. Remember to unmount relevant logical volumes again after rebooting.
$ 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.
  • 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 /var/media
  • Check that everything looks ok :)

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