LVM应用实例
案例的环境和需求描述如下:公司准备在Internet中搭建邮件服务器(RHEL 6系统平台),面向全国各地的员工及部分VIP客户提供电子邮箱空间。由于用户数量众多,邮件存储需要大量的空间,考虑到动态扩容的需要,计划增加两块SCSI硬盘并构建LVM逻辑卷 (挂载到“/mailbox”目录下)专门用于存放邮件数据。
根据上述案例环境和需求,推荐的操作步骤如下所述。
(1 )关闭服务器主机,打开机箱,正确挂接两块SCSI新硬盘。
(2 )开启服务器主机,并执行“fdisk -I”命令进行检查,确认已识别新增的硬盘(sdb、 sdc )。
(3)在新磁盘中进行分区,将每块硬盘的所有空间划分为一个独立的主分区,并将分区类型更改为“8e”分好区后使用“fdisk- l”命令查看,确认结果如下所示。
一、格式化、转换分区类型
1.把sdb2转换成LVM类型的分区:
[[email protected] /]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) and change display units to sectors (command ‘u‘).
Command (m for help): tPartition number (1-4): 2Hex code (type L to list codes): 8eChanged system type of partition 2 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdb: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xa8f24694
Device Boot Start End Blocks Id System/dev/sdb1 1 2612 20980858+ 83 Linux/dev/sdb2 2613 5224 20980890 8e Linux LVM/dev/sdb3 5225 10443 41921617+ 83 Linux
Command (m for help): wThe partition table has been altered!
Calling ioctl() to re-read partition table.Syncing disks.[[email protected] /]#
2.创建sdc2分区,并转变成LVM类型:
[[email protected] /]# fdisk /dev/sdc
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) and change display units to sectors (command ‘u‘).
Command (m for help): nCommand action e extended p primary partition (1-4)2Invalid partition number for type `2‘Command action e extended p primary partition (1-4)pPartition number (1-4): 2First cylinder (2613-10443, default 2613):Using default value 2613Last cylinder, +cylinders or +size{K,M,G} (2613-10443, default 10443): +20FUnsupported suffix: ‘F‘.Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte) 2^N: K (KibiByte), M (MebiByte), G (GibiByte)Last cylinder, +cylinders or +size{K,M,G} (2613-10443, default 10443): +20G
Command (m for help): P
Disk /dev/sdc: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x8967e34e
Device Boot Start End Blocks Id System/dev/sdc1 1 2612 20980858+ 83 Linux/dev/sdc2 2613 5224 20980890 83 Linux
错误操作:
Command (m for help): TPartition number (1-4): bePartition number (1-4): 2Hex code (type L to list codes): bChanged system type of partition 2 to b (W95 FAT32)
正确操作:
Command (m for help): tPartition number (1-4): 2Hex code (type L to list codes): 8eChanged system type of partition 2 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdc: 85.9 GB, 85899345920 bytes255 heads, 63 sectors/track, 10443 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x8967e34e
Device Boot Start End Blocks Id System/dev/sdc1 1 2612 20980858+ 83 Linux/dev/sdc2 2613 5224 20980890 8e Linux LVM
Command (m for help):
3.保存
Command (m for help): wThe 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 atthe next reboot or after you run partprobe(8) or kpartx(8)
WARNING: If you have created or modified any DOS 6.xpartitions, please see the fdisk manual page for additionalinformation.Syncing disks.
4.探测
探测失败
[[email protected]test2 /]# partx /dev/sdcsdc sdc1[[email protected]test2 /]# partx /dev/sdcsdc sdc1[[email protected]test2 /]# partx -a /dev/sdcsdc sdc1[[email protected]test2 /]# partx -a /dev/sdcsdc sdc1
重启:
识别成功:
[root@test2 jason]# partprobe /dev/sdcsdc sdc1 sdc2[root@test2 jason]# partprobe /dev/sdc
当前pv的信息
[ro[email protected] jason]# pvscan PV /dev/sda2 VG vg_jason lvm2 [19.51 GiB / 0 free] PV /dev/sdb1 lvm2 [20.01 GiB] PV /dev/sdb2 lvm2 [20.01 GiB] Total: 3 [59.53 GiB] / in use: 1 [19.51 GiB] / in no VG: 2 [40.02 GiB][[email protected] jason]#
二、创建Logical Volume
1.创建pv
将sdc2创建成physical volume(物理卷):
[root@test2 jason]# pvcreate /dev/sdc2 Physical volume "/dev/sdc2" successfully created[root@test2 jason]#
再次查看pv信息:
[[email protected] jason]# pvscan PV /dev/sda2 VG vg_jason lvm2 [19.51 GiB / 0 free] PV /dev/sdb1 lvm2 [20.01 GiB] PV /dev/sdb2 lvm2 [20.01 GiB] PV /dev/sdc2 lvm2 [20.01 GiB] Total: 4 [79.53 GiB] / in use: 1 [19.51 GiB] / in no VG: 3 [60.03 GiB][[email protected] jason]#
2.创建 VG
vg的信息:
[root@test2 jason]# lvscan ACTIVE ‘/dev/vg_jason/lv_root‘ [17.51 GiB] inherit ACTIVE ‘/dev/vg_jason/lv_swap‘ [2.00 GiB] inherit
创建mail_store的volume group(卷组):
[root@test2 jason]# pvcreate mail_store /dev/sdb2 /dev/sdc2 Device mail_store not found (or ignored by filtering). dev_is_mpath: failed to get device for 8:18 Physical volume "/dev/sdb2" successfully created Physical volume "/dev/sdc2" successfully created[root@test2 jason]#
再次查看vg信息:
[root@test2 jason]# vgscan Reading all physical volumes. This may take a while... Found volume group "vg_jason" using metadata type lvm2
分析是因为上面的命令用错了,应该是vgcreate,而不是pvcreate
重新创建并查看:
[root@test2 jason]# vgcreate mail_store /dev/sdb2 /dev/sdc2 Volume group "mail_store" successfully created[root@test2 jason]# vgscan Reading all physical volumes. This may take a while... Found volume group "mail_store" using metadata type lvm2 Found volume group "vg_jason" using metadata type lvm2[root@test2 jason]#
mail_store成功创建,并能通过vgscan查找到
[root@test2 jason]# lvcreate -L 35G -n mbox mail_store Logical volume "mbox" created[root@test2 jason]# lvscan ACTIVE ‘/dev/mail_store/mbox‘ [35.00 GiB] inherit ACTIVE ‘/dev/vg_jason/lv_root‘ [17.51 GiB] inherit ACTIVE ‘/dev/vg_jason/lv_swap‘ [2.00 GiB] inherit[root@test2 jason]#
3.创建LV
创建前的lvscan:
[root@test2 jason]# lvscan ACTIVE ‘/dev/vg_jason/lv_root‘ [17.51 GiB] inherit ACTIVE ‘/dev/vg_jason/lv_swap‘ [2.00 GiB] inherit
创建mbox的lv并查看:
[root@test2 jason]# lvcreate -L 35G -n mbox mail_store Logical volume "mbox" created[root@test2 jason]# lvscan ACTIVE ‘/dev/mail_store/mbox‘ [35.00 GiB] inherit ACTIVE ‘/dev/vg_jason/lv_root‘ [17.51 GiB] inherit ACTIVE ‘/dev/vg_jason/lv_swap‘ [2.00 GiB] inherit[root@test2 jason]#
创建目录
[root@test2 /]# mkdir /mailbox
4.格式化:
格式化成ext4
[root@test2 /]# mkfs.ext4 /dev/mail_store/mboxmke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks2293760 inodes, 9175040 blocks458752 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=4294967296280 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624
Writing inode tables: doneCreating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 30 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@test2 /]#
5.挂载并查看:
[[email protected] /]# mount /dev/mail_store/mbox /mailbox/[[email protected] /]# df -hTFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/vg_jason-lv_root ext4 18G 4.3G 13G 27% /tmpfs tmpfs 3.9G 72K 3.9G 1% /dev/shm/dev/sda1 ext4 485M 40M 421M 9% /boot/dev/sdc1 ext4 20G 421M 19G 3% /home/dev/mapper/mail_store-mbox ext4 35G 176M 33G 1% /mailbox[[email protected] /]#
三、拓展篇:
1.在虚拟机中新建一个100G的硬盘(略):
2.只建一个分区,并转换成为LVM类型:
[[email protected] jason]# fdisk /dev/sddDevice contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding a new DOS disklabel with disk identifier 0xef9a8da9.Changes will remain in memory only, until you decide to write them.After that, of course, the previous content won‘t be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) and change display units to sectors (command ‘u‘).
Command (m for help): nCommand action e extended p primary partition (1-4)pPartition number (1-4): 1First cylinder (1-13054, default 1):Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054):Using default value 13054
Command (m for help): tSelected partition 1Hex code (type L to list codes): 8eChanged system type of partition 1 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdd: 107.4 GB, 107374182400 bytes255 heads, 63 sectors/track, 13054 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xef9a8da9
Device Boot Start End Blocks Id System/dev/sdd1 1 13054 104856223+ 8e Linux LVM
Command (m for help): wThe partition table has been altered!
Calling ioctl() to re-read partition table.Syncing disks.[[email protected] jason]#
探测:
[root@test2 jason]# partprobe /dev/sddsdd sdd1[root@test2 jason]# partprobe /dev/sdd
3.创建pv:
[[email protected] jason]# pvcreate /dev/sdd1 dev_is_mpath: failed to get device for 8:49 Physical volume "/dev/sdd1" successfully created
vg拓展mail_store:[[email protected] jason]# vgextend mail_store /dev/sdd1 Volume group "mail_store" successfully extended
vgdisplay mail_store:
[[email protected] jason]# vgdisplay mail_store --- Volume group --- VG Name mail_store System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 3 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 140.01 GiB PE Size 4.00 MiB Total PE 35843 Alloc PE / Size 8960 / 35.00 GiB Free PE / Size 26883 / 105.01 GiB VG UUID yHpVfR-lbFL-vs9A-mzgz-m800-JLE2-Dt2z6F
[[email protected] jason]#
4.LV扩容
错误的语法:
[[email protected] jason]# lvextend -L +85G mbox mail_store Path required for Logical Volume "mbox" Please provide a volume group name Run `lvextend --help‘ for more information.
正确的lv扩容操作:
[root@test2 jason]# lvextend -L +85G /dev/mail_store/mbox Extending logical volume mbox to 120.00 GiB Logical volume mbox successfully resized
扩容后查看:
[root@test2 jason]# lvscan ACTIVE ‘/dev/mail_store/mbox‘ [120.00 GiB] inherit ACTIVE ‘/dev/vg_jason/lv_root‘ [17.51 GiB] inherit ACTIVE ‘/dev/vg_jason/lv_swap‘ [2.00 GiB] inherit
5.挂载
刚才为了重新加一个100G的硬盘重启,重启后的mount信息:
[[email protected] jason]# df -hTFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/vg_jason-lv_root ext4 18G 4.3G 13G 27% /tmpfs tmpfs 3.9G 72K 3.9G 1% /dev/shm/dev/sda1 ext4 485M 40M 421M 9% /boot/dev/sdc1 ext4 20G 421M 19G 3% /home[[email protected] jason]# mount/dev/mapper/vg_jason-lv_root on / type ext4 (rw)proc on /proc type proc (rw)sysfs on /sys type sysfs (rw)devpts on /dev/pts type devpts (rw,gid=5,mode=620)tmpfs on /dev/shm type tmpfs (rw)/dev/sda1 on /boot type ext4 (rw)/dev/sdc1 on /home type ext4 (rw)none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
重新挂载并查看:
[[email protected] jason]# mount /dev/mail_store/mbox /mailbox/[[email protected] jason]# mount/dev/mapper/vg_jason-lv_root on / type ext4 (rw)proc on /proc type proc (rw)sysfs on /sys type sysfs (rw)devpts on /dev/pts type devpts (rw,gid=5,mode=620)tmpfs on /dev/shm type tmpfs (rw)/dev/sda1 on /boot type ext4 (rw)/dev/sdc1 on /home type ext4 (rw)none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)/dev/mapper/mail_store-mbox on /mailbox type ext4 (rw)
查看mbox的容量:
[[email protected] jason]# df -hTFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/vg_jason-lv_root ext4 18G 4.3G 13G 27% /tmpfs tmpfs 3.9G 72K 3.9G 1% /dev/shm/dev/sda1 ext4 485M 40M 421M 9% /boot/dev/sdc1 ext4 20G 421M 19G 3% /home/dev/mapper/mail_store-mbox ext4 35G 176M 33G 1% /mailbox
6.更新磁盘容量
查看resize2fs的用法:
[[email protected] jason]# resize2fsresize2fs 1.41.12 (17-May-2010)Usage: resize2fs [-d debug_flags] [-f] [-F] [-M] [-P] [-p] device [new_size]
使用resize2fs更新mbox的容量:
[[email protected] jason]# resize2fs /dev/mail_store/mboxresize2fs 1.41.12 (17-May-2010)Filesystem at /dev/mail_store/mbox is mounted on /mailbox; on-line resizing requiredold desc_blocks = 3, new_desc_blocks = 8Performing an on-line resize of /dev/mail_store/mbox to 31457280 (4k) blocks.The filesystem on /dev/mail_store/mbox is now 31457280 blocks long.
更新成功:
[[email protected] jason]# df -hTFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/vg_jason-lv_root ext4 18G 4.3G 13G 27% /tmpfs tmpfs 3.9G 72K 3.9G 1% /dev/shm/dev/sda1 ext4 485M 40M 421M 9% /boot/dev/sdc1 ext4 20G 421M 19G 3% /home/dev/mapper/mail_store-mbox ext4 119G 188M 112G 1% /mailbox[[email protected] jason]#
时间: 2024-09-30 19:06:47