Monday, July 26, 2010

How to LVM

[root@server1 ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 9729.
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
First cylinder (5625-9729, default 5625):
Using default value 5625
Last cylinder or +size or +sizeM or +sizeK (5625-9729, default 9729): +500M

Command (m for help): n
First cylinder (5687-9729, default 5687):
Using default value 5687
Last cylinder or +size or +sizeM or +sizeK (5687-9729, default 9729): +600M

Command (m for help): n
First cylinder (5761-9729, default 5761): +1G
Value out of range.
First cylinder (5761-9729, default 5761):
Using default value 5761
Last cylinder or +size or +sizeM or +sizeK (5761-9729, default 9729): +1g

Command (m for help): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1288 10241437+ 83 Linux
/dev/sda3 1289 2563 10241437+ 83 Linux
/dev/sda4 2564 9729 57560895 5 Extended
/dev/sda5 2564 2818 2048256 82 Linux swap / Solaris
/dev/sda6 2819 2945 1020096 83 Linux
/dev/sda7 2946 5378 19543041 83 Linux
/dev/sda8 5379 5501 987966 fd Linux raid autodetect
/dev/sda9 5502 5624 987966 fd Linux raid autodetect
/dev/sda10 5625 5686 497983+ 83 Linux
/dev/sda11 5687 5760 594373+ 83 Linux
/dev/sda12 5761 5883 987966 83 Linux

Command (m for help): t
Partition number (1-12): 10
Hex code (type L to list codes): l

0 Empty 1e Hidden W95 FAT1 80 Old Minix be Solaris boot
1 FAT12 24 NEC DOS 81 Minix / old Lin bf Solaris
2 XENIX root 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
5 Extended 41 PPC PReP Boot 85 Linux extended c7 Syrinx
6 FAT16 42 SFS 86 NTFS volume set da Non-FS data
7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
8 AIX 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM df BootIt
a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e1 DOS access
b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS e4 SpeedStor
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a5 FreeBSD ee EFI GPT
10 OPUS 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fd Linux raid auto
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fe LANstep
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid ff BBT
1c Hidden W95 FAT3 75 PC/IX
Hex code (type L to list codes): 8e
Changed system type of partition 10 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-12): 11
Hex code (type L to list codes): 8e
Changed system type of partition 11 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-12): 12
Hex code (type L to list codes): 8e
Changed system type of partition 12 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1288 10241437+ 83 Linux
/dev/sda3 1289 2563 10241437+ 83 Linux
/dev/sda4 2564 9729 57560895 5 Extended
/dev/sda5 2564 2818 2048256 82 Linux swap / Solaris
/dev/sda6 2819 2945 1020096 83 Linux
/dev/sda7 2946 5378 19543041 83 Linux
/dev/sda8 5379 5501 987966 fd Linux raid autodetect
/dev/sda9 5502 5624 987966 fd Linux raid autodetect
/dev/sda10 5625 5686 497983+ 8e Linux LVM
/dev/sda11 5687 5760 594373+ 8e Linux LVM
/dev/sda12 5761 5883 987966 8e Linux LVM

Command (m for help):


[root@server1 ~]# fdisk -l | grep LVM
Disk /dev/md0 doesn't contain a valid partition table
/dev/sda10 5625 5686 497983+ 8e Linux LVM
/dev/sda11 5687 5760 594373+ 8e Linux LVM
/dev/sda12 5761 5883 987966 8e Linux LVM
[root@server1 ~]# pvcreate /dev/sda{10,11,12}
Physical volume "/dev/sda10" successfully created
Physical volume "/dev/sda11" successfully created
Physical volume "/dev/sda12" successfully created
[root@server1 ~]# pvdisplay 
--- NEW Physical volume ---
PV Name /dev/sda10
VG Name
PV Size 486.31 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID VGitTz-hLyt-E9nd-HrwL-lyMt-e4NO-k8IRad

--- NEW Physical volume ---
PV Name /dev/sda11
VG Name
PV Size 580.44 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 29UhWl-VN5i-GXCu-x7AC-mDDO-GSaU-maVa7a

--- NEW Physical volume ---
PV Name /dev/sda12
VG Name
PV Size 964.81 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID CFyjML-x8LT-1hb6-6u4r-5wRh-22ic-rqMgd3

[root@server1 ~]# pvdisplay
[root@server1 ~]# pvcreate /dev/sda{10,11,12}
[root@server1 ~]# pvdisplay
[root@server1 ~]#
[root@server1 ~]# pvcreate /dev/sda{10,11,12}
[root@server1 ~]# pvdisplay
[root@server1 ~]# vgcreate vg0 /dev/sda{10,11}

Volume group "vg0" successfully created
[root@server1 ~]# vgdisplay 
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 1.04 GB
PE Size 4.00 MB
Total PE 266
Alloc PE / Size 0 / 0
Free PE / Size 266 / 1.04 GB
VG UUID lSO1vy-lYof-ouDq-grPK-RZYc-Wl8f-hUSB69

[root@server1 ~]# pvdisplay | grep Name
PV Name /dev/sda10
VG Name vg0
PV Name /dev/sda11
VG Name vg0
PV Name /dev/sda12
VG Name
[root@server1 ~]# lvcreate --size 700M --name lvmdata vg0
Logical volume "lvmdata" created
[root@server1 ~]# lvcreate --size 200M --name lvmdata1 vg0
Logical volume "lvmdata1" created
[root@server1 ~]# lvdisplay
--- Logical volume ---
LV Name /dev/vg0/lvmdata
VG Name vg0
LV UUID Ii2Gjs-5HcP-G8tA-jq4V-zmok-mNHM-wn4F14
LV Write Access read/write
LV Status available
# open 0
LV Size 700.00 MB
Current LE 175
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:0

--- Logical volume ---
LV Name /dev/vg0/lvmdata1
VG Name vg0
LV UUID 54k16G-ivZr-3MGy-FMfy-J3RF-As2H-R3Hyl4
LV Write Access read/write
LV Status available
# open 0
LV Size 200.00 MB
Current LE 50
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:1

[root@server1 ~]# lvdisplay | grep Name
LV Name /dev/vg0/lvmdata
VG Name vg0
LV Name /dev/vg0/lvmdata1
VG Name vg0
[root@server1 ~]# mke2fs -j /dev/vg0/lvmdata
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
89664 inodes, 179200 blocks
8960 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=184549376
6 block groups
32768 blocks per group, 32768 fragments per group
14944 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@server1 ~]# mke2fs -j /dev/vg0/lvmdata1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@server1 ~]# lvdisplay | grep Name
[root@server1 ~]# mke2fs -j /dev/vg0/lvmdata
[root@server1 ~]# mke2fs -j /dev/vg0/lvmdata1
[root@server1 ~]# mkdir /lv1
[root@server1 ~]# mkdir /lv
[root@server1 ~]# mount /dev/vg0/lvmdata /lv
[root@server1 ~]# mount /dev/vg0/lvmdata1 /lv1
[root@server1 ~]# df

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 9920624 6494924 2913632 70% /
/dev/sda1 101086 15524 80343 17% /boot
tmpfs 513512 0 513512 0% /dev/shm
/dev/sda6 988088 202452 734632 22% /home
/dev/sda2 9920624 7638400 1770156 82% /usr
/dev/sda7 19236308 12624460 5634696 70% /data
/opt/trainerCDRHEL5.iso
27802 27802 0 100% /mnt
/dev/mapper/vg0-lvmdata
705512 17120 652552 3% /lv
/dev/mapper/vg0-lvmdata1
198337 5664 182433 4% /lv1
[root@server1 ~]# touch /lv/file{1,2,3,4,5}
[root@server1 ~]# touch /lv1/file{1,2,3,4,5}
[root@server1 ~]# cat /proc/mdstat 

Personalities : [raid6] [raid5] [raid4] [raid0]
md0 : active raid0 sda9[1] sda8[0]
1975680 blocks 64k chunks

unused devices:
[root@server1 ~]# pvcreate /dev/md0
Physical volume "/dev/md0" successfully created
[root@server1 ~]# vgextend /dev/md0 vg0
Volume group "md0" not found.
[root@server1 ~]# vgextend vg0 /dev/md0
Volume group "vg0" successfully extended
[root@server1 ~]# vgdisplay 

--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 2.92 GB
PE Size 4.00 MB
Total PE 748
Alloc PE / Size 225 / 900.00 MB
Free PE / Size 523 / 2.04 GB
VG UUID lSO1vy-lYof-ouDq-grPK-RZYc-Wl8f-hUSB69

[root@server1 ~]# pvdisplay | grep Name
PV Name /dev/sda10
VG Name vg0
PV Name /dev/sda11
VG Name vg0
PV Name /dev/md0
VG Name vg0
PV Name /dev/sda12
VG Name
[root@server1 ~]# vgextend vg0 /dev/md0
[root@server1 ~]# man lvextend
[root@server1 ~]# lvextend --size +1000M /dev/vg0/lvmdata1

Extending logical volume lvmdata1 to 1.17 GB
Logical volume lvmdata1 successfully resized
[root@server1 ~]# vgdisplay 
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 2.92 GB
PE Size 4.00 MB
Total PE 748
Alloc PE / Size 475 / 1.86 GB
Free PE / Size 273 / 1.07 GB
VG UUID lSO1vy-lYof-ouDq-grPK-RZYc-Wl8f-hUSB69

[root@server1 ~]# lvextend --size +1000M /dev/vg0/lvmdata1
[root@server1 ~]# resize2fs /dev/vg0/lvmdata1

resize2fs 1.39 (29-May-2006)
Filesystem at /dev/vg0/lvmdata1 is mounted on /lv1; on-line resizing required
Performing an on-line resize of /dev/vg0/lvmdata1 to 1228800 (1k) blocks.
The filesystem on /dev/vg0/lvmdata1 is now 1228800 blocks long.

[root@server1 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 9920624 6494936 2913620 70% /
/dev/sda1 101086 15524 80343 17% /boot
tmpfs 513512 0 513512 0% /dev/shm
/dev/sda6 988088 202452 734632 22% /home
/dev/sda2 9920624 7638400 1770156 82% /usr
/dev/sda7 19236308 12624460 5634696 70% /data
/opt/trainerCDRHEL5.iso
27802 27802 0 100% /mnt
/dev/mapper/vg0-lvmdata
705512 17120 652552 3% /lv
/dev/mapper/vg0-lvmdata1
1190033 6900 1121768 1% /lv1
[root@server1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 9.5G 6.2G 2.8G 70% /
/dev/sda1 99M 16M 79M 17% /boot
tmpfs 502M 0 502M 0% /dev/shm
/dev/sda6 965M 198M 718M 22% /home
/dev/sda2 9.5G 7.3G 1.7G 82% /usr
/dev/sda7 19G 13G 5.4G 70% /data
/opt/trainerCDRHEL5.iso
28M 28M 0 100% /mnt
/dev/mapper/vg0-lvmdata
689M 17M 638M 3% /lv
/dev/mapper/vg0-lvmdata1
1.2G 6.8M 1.1G 1% /lv1
[root@server1 ~]# resize2fs /dev/vg0/lvmdata1
[root@server1 ~]# pvdisplay | grep Name

PV Name /dev/sda10
VG Name vg0
PV Name /dev/sda11
VG Name vg0
PV Name /dev/md0
VG Name vg0
PV Name /dev/sda12
VG Name
[root@server1 ~]# vgextend vg0 /dev/sda12
Volume group "vg0" successfully extended
[root@server1 ~]# pvdisplay | grep Name
PV Name /dev/sda10
VG Name vg0
PV Name /dev/sda11
VG Name vg0
PV Name /dev/md0
VG Name vg0
PV Name /dev/sda12
VG Name vg0
[root@server1 ~]# pvdisplay /dev/sda12
--- Physical volume ---
PV Name /dev/sda12
VG Name vg0
PV Size 964.81 MB / not usable 830.00 KB
Allocatable yes
PE Size (KByte) 4096
Total PE 241
Free PE 241
Allocated PE 0
PV UUID CFyjML-x8LT-1hb6-6u4r-5wRh-22ic-rqMgd3

[root@server1 ~]# pvdisplay /dev/sda10
--- Physical volume ---
PV Name /dev/sda10
VG Name vg0
PV Size 486.31 MB / not usable 2.31 MB
Allocatable yes (but full)
PE Size (KByte) 4096
Total PE 121
Free PE 0
Allocated PE 121
PV UUID VGitTz-hLyt-E9nd-HrwL-lyMt-e4NO-k8IRad

[root@server1 ~]# pvmove /dev/sda10 /dev/sda12
/dev/sda10: Moved: 24.8%
/dev/sda10: Moved: 64.5%
/dev/sda10: Moved: 97.5%
/dev/sda10: Moved: 100.0%
[root@server1 ~]# vgremove vg0 /dev/sda10
Volume group "vg0" still contains 2 logical volume(s)
Volume group "sda10" not found
[root@server1 ~]# pvdisplay | grep Name
PV Name /dev/sda10
VG Name vg0
PV Name /dev/sda11
VG Name vg0
PV Name /dev/md0
VG Name vg0
PV Name /dev/sda12
VG Name vg0
[root@server1 ~]# vgreduce vg0 /dev/sda10
Removed "/dev/sda10" from volume group "vg0"
[root@server1 ~]# pvdisplay | grep Name
PV Name /dev/sda11
VG Name vg0
PV Name /dev/md0
VG Name vg0
PV Name /dev/sda12
VG Name vg0
PV Name /dev/sda10
VG Name
[root@server1 ~]# pvremove /dev/sda10
Labels on physical volume "/dev/sda10" successfully wiped
[root@server1 ~]# pvdisplay | grep Name
PV Name /dev/sda11
VG Name vg0
PV Name /dev/md0
VG Name vg0
PV Name /dev/sda12
VG Name vg0


[root@server1 ~]# resize2fs -pf /dev/vg0/lvmdata1 800M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg0/lvmdata1 to 819200 (1k) blocks.
Begin pass 3 (max = 150)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/vg0/lvmdata1 is now 819200 blocks long.

[root@server1 ~]# lvdisplay /dev/vg0/lvmdata1
--- Logical volume ---
LV Name /dev/vg0/lvmdata1
VG Name vg0
LV UUID 54k16G-ivZr-3MGy-FMfy-J3RF-As2H-R3Hyl4
LV Write Access read/write
LV Status available
# open 0
LV Size 1.17 GB
Current LE 300
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:1


[root@server1 ~]# lvreduce /dev/vg0/lvmdata1 --size 800M
WARNING: Reducing active logical volume to 800.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvmdata1? [y/n]: y
Reducing logical volume lvmdata1 to 800.00 MB
Logical volume lvmdata1 successfully resized

[root@server1 ~]# mount /dev/vg0/lvmdata1 /lv
lv/ lv1/

[root@server1 ~]# mount /dev/vg0/lvmdata1 /lv1

[root@server1 ~]# ll /lv1
total 12
-rw-r--r-- 1 root root 0 Aug 1 08:53 file1
-rw-r--r-- 1 root root 0 Aug 1 08:53 file2
-rw-r--r-- 1 root root 0 Aug 1 08:53 file3
-rw-r--r-- 1 root root 0 Aug 1 08:53 file4
-rw-r--r-- 1 root root 0 Aug 1 08:53 file5
drwx------ 2 root root 12288 Aug 1 08:52 lost+found

[root@server1 ~]# resize2fs -pf /dev/vg0/lvmdata1 800M
[root@server1 ~]# lvreduce /dev/vg0/lvmdata1 --size 800M
[root@server1 ~]# lvdisplay /dev/vg0/lvmdata1