LVM的用法
学了两天的LVM。一开始看的时候总是不理解其中的用意。实践两次后,再看理论,豁然开朗。我现在说一下我的LVM基本配置
LVM其实就是将几个物理分区通过软件组合在一起,看起来像是独立的大磁盘。
PV:物理磁盘; VG:卷组; LV:逻辑卷。
呵呵,看懂这个图,就这知道LVM的设置流程了。好了,我下面把我的具体设置讲一下吧。
说明:我的是加了一块硬盘( 8G )。我命名为/dev/hdb
1.查看一下这个磁盘
[root@whx ~]# fdisk -l /dev/hdb
Disk /dev/hdb: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
Disk /dev/hdb doesn't contain a valid partition table
2.创建分区,我创建了三个分区。
[root@whx ~]# fdisk /dev/hdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
The number of cylinders for this disk is set to 17753.
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)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-17753, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-17753, default 17753): + 3000M
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (6202-17753, default 6202):
Using default value 6202
Last cylinder or +size or +sizeM or +sizeK (6202-17753, default 17753): + 2000M
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (10337-17753, default 10337):
Using default value 10337
Last cylinder or +size or +sizeM or +sizeK (10337-17753, default 17753):
Using default value 17753
Command (m for help): p
Disk /dev/hdb: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 6201 2929941 83 Linux
/dev/hdb2 6202 10336 1953787+ 83 Linux
/dev/hdb3 10337 17753 3504532+ 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
3.建立PV与查询PV
[root@whx ~]# fdisk /dev/hdb
目的:改变文件的分区代号。
The number of cylinders for this disk is set to 17753.
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): t
Partition number (1-4): 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@whx ~]# partprobe
[root@whx ~]# fdisk -l /dev/hdb
Disk /dev/hdb: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 6201 2929941 8e Linux LVM
/dev/hdb2 6202 10336 1953787+ 8e Linux LVM
/dev/hdb3 10337 17753 3504532+ 83 Linux
[root@whx ~]# pvcreate /dev/hdb1
Physical volume "/dev/hdb1" successfully created
[root@whx ~]# pvcreate /dev/hdb2
Physical volume "/dev/hdb2" successfully created
[root@whx ~]# pvscan(查询)
PV /dev/hdb1 lvm2 [2.79 GB]
PV /dev/hdb2 lvm2 [1.86 GB]
Total: 2 [4.66 GB] / in use: 0 [0 ] / in no VG: 2 [4.66 GB]
[root@whx ~]# pvdisplay(查询)
"/dev/hdb1" is a new physical volume of "2.79 GB"
--- NEW Physical volume ---
PV Name /dev/hdb1
VG Name
PV Size 2.79 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 8kHYl1-eTMe-HTjX-MVcK-JKBa-wb5e-JY0pj1
"/dev/hdb2" is a new physical volume of "1.86 GB"
--- NEW Physical volume ---
PV Name /dev/hdb2
VG Name
PV Size 1.86 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID yfirkQ-8hoy-GLzC-OE 7m -EZ0h-L8QK-FIadRb
4.创建VG
格式:Vgcreate VGname PVname1 PVname2
[root@whx ~]# vgcreate batmanvg /dev/hdb1 /dev/hdb2
Volume group "batmanvg" successfully created
[root@whx ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "batmanvg" using metadata type lvm2
[root@whx ~]# vgdisplay
--- Volume group ---
VG Name batmanvg
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 4.65 GB
PE Size 4.00 MB
Total PE 1191
Alloc PE / Size 0 / 0
Free PE / Size 1191 / 4.65 GB
VG UUID 89K6md-z7TS-e1Kq-QL7B-FZ9x-Es8v-ugiA0s
5.建立LV
格式:
[root@whx ~]# pvscan
PV /dev/hdb1 VG batmanvg lvm2 [2.79 GB / 2.79 GB free]
PV /dev/hdb2 VG batmanvg lvm2 [1.86 GB / 1.86 GB free]
Total: 2 [4.65 GB] / in use: 2 [4.65 GB] / in no VG: 0 [0 ]
[root@whx dev]# lvcreate -L 4.65G -n batmanlv batmanvg
Rounding up size to full physical extent 4.65 GB
Logical volume "batmanlv" created
[root@whx dev]# lvscan
ACTIVE '/dev/batmanvg/batmanlv' [4.65 GB] inherit
[root@whx dev]# lvdisplay
--- Logical volume ---
LV Name /dev/batmanvg/batmanlv
VG Name batmanvg
LV UUID jurPhr-Xrn9-gIMl-eIUn-BH6r-N8X9-djJFzn
LV Write Access read/write
LV Status available
# open 0
LV Size 4.65 GB
Current LE 1191
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
6.分区的格式化与载入
[root@whx dev]# mke2fs -j /dev/batmanvg/batmanlv
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
610432 inodes, 1219584 blocks
60979 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1249902592
38 block groups
32768 blocks per group, 32768 fragments per group
16064 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@whx dev]# mkdir /mnt/lvm
[root@whx dev]# mount -t ext3 /dev/batmanvg/batmanlv /mnt/lvm
[root@whx dev]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda2 6940516 356164 6226104 6% /
/dev/hda8 1984016 40000 1841604 3% /home
/dev/hda7 2972236 70124 2748696 3% /opt
/dev/hda6 5952252 2729500 2915512 49% /usr
/dev/hda5 5952252 258684 5386328 5% /var
/dev/hda3 6940516 176084 6406184 3% /usr/local
/dev/hda1 194442 11127 173276 7% /boot
tmpfs 127796 0 127796 0% /dev/shm
/dev/mapper/batmanvg-batmanlv
4801664 140736 4417012 4% /mnt/lvm
7.加大原有的LVM
[root@whx dev]# umount /mnt/lvm
[root@whx dev]# fdisk /dev/hdb
The number of cylinders for this disk is set to 17753.
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): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
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.
[root@whx dev]# fdisk -l /dev/hdb
Disk /dev/hdb: 8589 MB, 8589934592 bytes
15 heads, 63 sectors/track, 17753 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 6201 2929941 8e Linux LVM
/dev/hdb2 6202 10336 1953787+ 8e Linux LVM
/dev/hdb3 10337 17753 3504532+ 8e Linux LVM
[root@whx ~]# partprobe
[root@whx ~]# pvcreate /dev/hdb3
Physical volume "/dev/hdb3" successfully created
[root@whx ~]# vgextend batmanvg /dev/hdb3
Volume group "batmanvg" successfully extended
[root@whx ~]# vgdisplay
--- Volume group ---
VG Name batmanvg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 7.99 GB
PE Size 4.00 MB
Total PE 2046
Alloc PE / Size 1191 / 4.65 GB
Free PE / Size 855 / 3.34 GB
VG UUID 89K6md-z7TS-e1Kq-QL7B-FZ9x-Es8v-ugiA0s
[root@whx ~]# pvscan
PV /dev/hdb1 VG batmanvg lvm2 [2.79 GB / 0 free]
PV /dev/hdb2 VG batmanvg lvm2 [1.86 GB / 0 free]
PV /dev/hdb3 VG batmanvg lvm2 [3.34 GB / 3.34 GB free]
Total: 3 [7.99 GB] / in use: 3 [7.99 GB] / in no VG: 0 [0 ]
[root@whx ~]# lvextend -L + 3G /dev/batmanvg/batmanlv
Extending logical volume batmanlv to 7.65 GB
Logical volume batmanlv successfully resized
[root@whx ~]# lvdisplay
--- Logical volume ---
LV Name /dev/batmanvg/batmanlv
VG Name batmanvg
LV UUID jurPhr-Xrn9-gIMl-eIUn-BH6r-N8X9-djJFzn
LV Write Access read/write
LV Status available
# open 0
LV Size 7.65 GB
Current LE 1959
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[root@whx ~]# mount -t ext3 /dev/batmanvg/batmanlv /mnt/lvm
[root@whx ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/hda2 6.7G 348M 6.0G 6% /
/dev/hda8 1.9G 40M 1.8G 3% /home
/dev/hda7 2.9G 69M 2.7G 3% /opt
/dev/hda6 5.7G 2.7G 2.8G 49% /usr
/dev/hda5 5.7G 253M 5.2G 5% /var
/dev/hda3 6.7G 172M 6.2G 3% /usr/local
/dev/hda1 190M 11M 170M 7% /boot
tmpfs 125M 0 125M 0% /dev/shm
/dev/mapper/batmanvg-batmanlv
4.6G 138M 4.3G 4% /mnt/lvm
[root@whx ~]# umount /mnt/lvm
[root@whx ~]# resize2fs -f /dev/batmanvg/batmanlv
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/batmanvg/batmanlv to 2006016 (4k) blocks.
The filesystem on /dev/batmanvg/batmanlv is now 2006016 blocks long.
[root@whx ~]# mount -t ext3 /dev/batmanvg/batmanlv /mnt/lvm
[root@whx ~]# df /mnt/lvm
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/batmanvg-batmanlv
7899000 141924 7436116 2% /mnt/lvm
8.删除LVM
[root@whx ~]# umount /mnt/lvm
[root@whx ~]# lvremove /dev/batmanvg/batmanlv
Do you really want to remove active logical volume "batmanlv"? [y/n]: y
Logical volume "batmanlv" successfully removed
[root@whx ~]# vgchange -a n batmanvg
0 logical volume(s) in volume group "batmanvg" now active
[root@whx ~]# vgremove batmanvg
Volume group "batmanvg" successfully removed
[root@whx ~]# pvremove /dev/hdb1
Labels on physical volume "/dev/hdb1" successfully wiped
[root@whx ~]# pvremove /dev/hdb2
Labels on physical volume "/dev/hdb2" successfully wiped
[root@whx ~]# pvremove /dev/hdb3
Labels on physical volume "/dev/hdb3" successfully wiped