Wednesday, July 11

How To Create & Manage LVM in Linux

server1:~# fdisk -l
Disk /dev/sda: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes


fdisk /dev/sda

server1:~# fdisk /dev/sda

The number of cylinders for this disk is set to 10443.
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):
<-- m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help):
<-- n
Command action
e extended
p primary partition (1-4)

<-- p
Partition number (1-4): <-- 1
First cylinder (1-10443, default 1): <--< ENTER>
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-10443, default 10443):
<-- +25000M
Command (m for help): <-- t
Selected partition 1
Hex code (type L to list codes):
<-- 8e (8e id is for linux LVM)
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help):
<-- w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Then run


fdisk -l

again. The output should look like this:

server1:~# fdisk -l

Disk /dev/sda: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 3040 24418768+ 8e Linux LVM
Then run


partprobe –a or partx –a /dev/sda

Above command for re reading the partition table

Then Create physical volume



pvcreate /dev/sda1

server1:~# pvcreate /dev/sda1 /dev/sdc1 /dev/sdd1 /dev/sde1
Physical volume "/dev/sda1" successfully created

Let's revert this last action for training purposes:


pvremove /dev/sda1

server1:~# pvremove /dev/sda1 /dev/sdc1 /dev/sdd1 /dev/sde1
Labels on physical volume "/dev/sda1" successfully wiped

Then run


pvcreate /dev/sda1

again:

server1:~# pvcreate /dev/sda1 /dev/sdc1 /dev/sdd1 /dev/sde1
Now run


pvdisplay

to learn about the current state of your physical volumes:

server1:~# pvdisplay
--- NEW Physical volume ---
PV Name /dev/sda1
VG Name
PV Size 23.29 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID G8lu2L-Hij1-NVde-sOKc-OoVI-fadg-Jd1vyU
Now let's create our volume group fileserver and add /dev/sda1 to it:


vgcreate fileserver /dev/sda1

server1:~# vgcreate fileserver /dev/sda1

Volume group "fileserver" successfully created

Let's learn about our volume groups:


vgdisplay

server1:~# vgdisplay
--- Volume group ---
VG Name fileserver
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 23.29 GB
PE Size 4.00 MB
Total PE 5962
Alloc PE / Size 0 / 0
Free PE / Size 5962 / 23.29 GB
VG UUID 3Y1WVF-BLET-QkKs-Qnrs-SZxI-wrNO-dTqhFP

Another command to learn about our volume groups:


vgscan

server1:~# vgscan
Reading all physical volumes. This may take a while...
Found volume group "fileserver" using metadata type lvm2

For training purposes let's rename our volumegroup fileserver into data:


vgrename fileserver data

server1:~# vgrename fileserver data
Volume group "fileserver" successfully renamed to "data"

Let's run vgdisplay and vgscan again to see if the volume group has been renamed:


vgdisplay

server1:~# vgdisplay
--- Volume group ---
VG Name data
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 23.29 GB
PE Size 4.00 MB
Total PE 5962
Alloc PE / Size 0 / 0
Free PE / Size 5962 / 23.29 GB
VG UUID 3Y1WVF-BLET-QkKs-Qnrs-SZxI-wrNO-dTqhFP


vgscan

server1:~# vgscan
Reading all physical volumes. This may take a while...
Found volume group "data" using metadata type lvm2

Now let's delete our volume group data:


vgremove data

server1:~# vgremove data
Volume group "data" successfully removed


vgdisplay

No output this time:

server1:~# vgdisplay


vgscan

server1:~# vgscan
Reading all physical volumes. This may take a while...

Let's create our volume group fileserver again:


vgcreate fileserver /dev/sda1

server1:~# vgcreate fileserver /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
Volume group "fileserver" successfully created

Next we create our logical volumes share (10GB), backup (2GB), and media (1GB) in the volume group fileserver. Together they use a little less than 50% of the available space (that way we can make use of RAID1 later on):


lvcreate --name share --size 10G fileserver

server1:~# lvcreate --name share --size 10G fileserver
Logical volume "share" created


lvcreate --name backup --size 2G fileserver

server1:~# lvcreate --name backup --size 2G fileserver
Logical volume "backup" created


lvcreate --name media --size 1G fileserver

server1:~# lvcreate --name media --size 1G fileserver
Logical volume "media" created

Let's get an overview of our logical volumes:


lvdisplay

server1:~# lvdisplay
--- Logical volume ---
LV Name /dev/fileserver/share
VG Name fileserver
LV UUID 280Mup-H9aa-sn0S-AXH3-04cP-V6p9-lfoGgJ
LV Write Access read/write
LV Status available
# open 0
LV Size 10.00 GB
Current LE 2560
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:0

--- Logical volume ---
LV Name /dev/fileserver/backup
VG Name fileserver
LV UUID zZeuKg-Dazh-aZMC-Aa99-KUSt-J6ET-KRe0cD
LV Write Access read/write
LV Status available
# open 0
LV Size 2.00 GB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:1

--- Logical volume ---
LV Name /dev/fileserver/media
VG Name fileserver
LV UUID usfvrv-BC92-3pFH-2NW0-2N3e-6ERQ-4Sj7YS
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:2


lvscan

server1:~# lvscan
ACTIVE '/dev/fileserver/share' [10.00 GB] inherit
ACTIVE '/dev/fileserver/backup' [2.00 GB] inherit
ACTIVE '/dev/fileserver/media' [1.00 GB] inherit

For training purposes we rename our logical volume media into films:


lvrename fileserver media films

server1:~# lvrename fileserver media films
Renamed "media" to "films" in volume group "fileserver"


lvdisplay

server1:~# lvdisplay
--- Logical volume ---
LV Name /dev/fileserver/share
VG Name fileserver
LV UUID 280Mup-H9aa-sn0S-AXH3-04cP-V6p9-lfoGgJ
LV Write Access read/write
LV Status available
# open 0
LV Size 10.00 GB
Current LE 2560
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:0

--- Logical volume ---
LV Name /dev/fileserver/backup
VG Name fileserver
LV UUID zZeuKg-Dazh-aZMC-Aa99-KUSt-J6ET-KRe0cD
LV Write Access read/write
LV Status available
# open 0
LV Size 2.00 GB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:1

--- Logical volume ---
LV Name /dev/fileserver/films
VG Name fileserver
LV UUID usfvrv-BC92-3pFH-2NW0-2N3e-6ERQ-4Sj7YS
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:2


lvscan

server1:~# lvscan
ACTIVE '/dev/fileserver/share' [40.00 GB] inherit
ACTIVE '/dev/fileserver/backup' [5.00 GB] inherit
ACTIVE '/dev/fileserver/films' [1.00 GB] inherit

Next let's delete the logical volume films:


lvremove /dev/fileserver/films

server1:~# lvremove /dev/fileserver/films
Do you really want to remove active logical volume "films"? [y/n]:
<-- y
Logical volume "films" successfully removed

We create the logical volume media again:


lvcreate --name media --size 1G fileserver

server1:~# lvcreate --name media --size 1G fileserver
Logical volume "media" created

No comments:

Post a Comment