LVM應用實例
案例的環境和需求描述如下:公司準備在Internet中搭建郵件服務器(RHEL 6系統平臺),面向全國各地的員工及部分VIP客戶提供電子郵箱空間。由於用戶數量衆多,郵件存儲需要大量的空間,考慮到動態擴容的需要,計劃增加兩塊SCSI硬盤並構建LVM邏輯卷 (掛載到“/mailbox”目錄下)專門用於存放郵件數據。
根據上述案例環境和需求,推薦的操作步驟如下所述。
(1 )關閉服務器主機,打開機箱,正確掛接兩塊SCSI新硬盤。
(2 )開啓服務器主機,並執行“fdisk -I”命令進行檢查,確認已識別新增的硬盤(sdb、 sdc )。
(3)在新磁盤中進行分區,將每塊硬盤的所有空間劃分爲一個獨立的主分區,並將分區類型更改爲“8e”分好區後使用“fdisk- l”命令查看,確認結果如下所示。
一、格式化、轉換分區類型
1.把sdb2轉換成LVM類型的分區:
[root@test2 /]# 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): 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): p Disk /dev/sdb: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk 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): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@test2 /]#
截圖:
2.創建sdc2分區,並轉變成LVM類型:
[root@test2 /]# 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): n Command action e extended p primary partition (1-4) 2 Invalid partition number for type `2' Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (2613-10443, default 2613): Using default value 2613 Last cylinder, +cylinders or +size{K,M,G} (2613-10443, default 10443): +20F Unsupported 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 bytes 255 heads, 63 sectors/track, 10443 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk 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): T Partition number (1-4): be Partition number (1-4): 2 Hex code (type L to list codes): b Changed system type of partition 2 to b (W95 FAT32)
分析:LVM的分區代碼是8e.
轉換成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): p Disk /dev/sdc: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk 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): 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 or after you run partprobe(8) or kpartx(8) WARNING: If you have created or modified any DOS 6.x partitions, please see the fdisk manual page for additional information. Syncing disks.
4.探測分區是否被電腦識別
探測失敗
[root@test2 /]# partx /dev/sdc sdc sdc1 [root@test2 /]# partx /dev/sdc sdc sdc1 [root@test2 /]# partx -a /dev/sdc sdc sdc1 [root@test2 /]# partx -a /dev/sdc sdc sdc1
重啓虛擬機(略)
重啓後識別成功:
[root@test2 jason]# partprobe /dev/sdc sdc sdc1 sdc2 [root@test2 jason]# partprobe /dev/sdc
截圖:
二、創建Logical Volume
當前pv的信息
[root@test2 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] [root@test2 jason]#
截圖:
1.創建pv
將sdc2創建成physical volume(物理卷):
[root@test2 jason]# pvcreate /dev/sdc2 Physical volume "/dev/sdc2" successfully created [root@test2 jason]#
再次查看pv信息:
[root@test2 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] [root@test2 jason]#
截圖:
2.創建 VG
創建名稱爲“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
查看VG
[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”de vg(卷組)創建成功
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.格式化:
將新建立的mail_box格式化成ext4
[root@test2 /]# mkfs.ext4 /dev/mail_store/mbox mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 2293760 inodes, 9175040 blocks 458752 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 280 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624 Writing inode tables: done Creating journal (32768 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@test2 /]#
截圖:
5.掛載並查看:
[root@test2 /]# mount /dev/mail_store/mbox /mailbox/ [root@test2 /]# df -hT Filesystem 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 [root@test2 /]#
截圖:
三、拓展篇:
1.在虛擬機中新建一個100G的硬盤(略):
截圖:
2.只建一個分區,並轉換成爲LVM類型:
[root@test2 jason]# fdisk /dev/sdd Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building 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): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-13054, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): Using default value 13054 Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdd: 107.4 GB, 107374182400 bytes 255 heads, 63 sectors/track, 13054 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xef9a8da9 Device Boot Start End Blocks Id System /dev/sdd1 1 13054 104856223+ 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@test2 jason]#
探測:
[root@test2 jason]# partprobe /dev/sdd sdd sdd1 [root@test2 jason]# partprobe /dev/sdd
3.創建pv:
[root@test2 jason]# pvcreate /dev/sdd1 dev_is_mpath: failed to get device for 8:49 Physical volume "/dev/sdd1" successfully created
4.vg拓展mail_store:
[root@test2 jason]# vgextend mail_store /dev/sdd1 Volume group "mail_store" successfully extended
vgdisplay mail_store:
[root@test2 jason]# vgdisplay mail_store --- Volume group --- VG Name mail_store System ID Formatlvm2 Metadata Areas3 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV0 Cur LV1 Open LV 0 Max PV0 Cur PV3 Act PV3 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 [root@test2 jason]#
截圖:
4.LV擴容
錯誤的語法:
[root@test2 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信息:
[root@test2 jason]# df -hT Filesystem 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 [root@test2 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)
重新掛載並查看:
[root@test2 jason]# mount /dev/mail_store/mbox /mailbox/ [root@test2 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的容量:
[root@test2 jason]# df -hT Filesystem 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的用法:
[root@test2 jason]# resize2fs resize2fs 1.41.12 (17-May-2010) Usage: resize2fs [-d debug_flags] [-f] [-F] [-M] [-P] [-p] device [new_size]
使用resize2fs更新mbox的容量:
[root@test2 jason]# resize2fs /dev/mail_store/mbox resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mail_store/mbox is mounted on /mailbox; on-line resizing required old desc_blocks = 3, new_desc_blocks = 8 Performing 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.
更新成功:
[root@test2 jason]# df -hT Filesystem 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 [root@test2 jason]#