一.背景:因為公司虛擬機 (/) 目錄容量過小,導致一些任務不能正常執行,需要給虛擬機擴容
二.操作:
初始磁盤情況:
1.使用 df 命令查看磁盤與目錄的容量:
[root@shaonian ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 48G 22G 25G 47% / tmpfs 3.8G 0 3.8G 0% /dev/shm /dev/xvda1 485M 33M 427M 8% /boot /dev/mapper/VolGroup-lv_home 4.7G 138M 4.4G 4% /home cm_processes 3.8G 808K 3.8G 1% /var/run/cloudera-scm-agent/process
2.使用 fdisk -l 命令查看磁盤分區:
[root@shaonian ~]# fdisk -l Disk /dev/xvdb: 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: 0x00000000 Disk /dev/xvda: 64.4 GB, 64424509440 bytes 255 heads, 63 sectors/track, 7832 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: 0x000e9377 Device Boot Start End Blocks Id System /dev/xvda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/xvda2 64 7833 62401536 8e Linux LVM Disk /dev/xvdd: 119 MB, 119891968 bytes 255 heads, 63 sectors/track, 14 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: 0x00000000 Disk /dev/mapper/VolGroup-lv_root: 52.3 GB, 52344913920 bytes 255 heads, 63 sectors/track, 6363 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: 0x00000000 Disk /dev/mapper/VolGroup-lv_swap: 6442 MB, 6442450944 bytes 255 heads, 63 sectors/track, 783 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: 0x00000000 Disk /dev/mapper/VolGroup-lv_home: 5108 MB, 5108662272 bytes 255 heads, 63 sectors/track, 621 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: 0x00000000
發現執行 fdisk -l 顯示的第一行,顯示出的 /dev/xvdb 分區,並沒有被使用。這就是是管理人員分出給磁盤擴容用的磁盤空間,我們接下來要用這個分區為(/)目錄擴容
3.接着執行 fdisk /dev/xvdb ,進入 fdisk 命令行給磁盤分區
[root@shaonian ~]# fdisk /dev/xvdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xd259beb1. 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): p Disk /dev/xvdb: 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: 0xd259beb1 Device Boot Start End Blocks Id System 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): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): Using default value 10443 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): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
可以看出,輸入fdisk /dev/xvdb 命令后,提示我們輸入命令,我們依次輸入以下命令:
p 查看已有分區,由於這個分區是新的,所以為空
n 新建分區:
p 新建主分區
1 輸入分區號,因為之前沒有分區,所以這是第一個分區,分區號為1
接下來 First cylinder 和 Last cylinder 我們直接點擊回車,默認使用全部空間
t 改變分區類型
8e 改變分區類型為 LVM
w 寫入改變
此時系統將改變分區,然后退出 fdisk 命令行。如果沒有自動退出,輸入 q 退出。
4. 用 mkfs 命令對分區進行格式化
mkfs -t ext3 /dev/xvdb1
此時將進入漫長的等待......
5.添加 LVM到原來的 LVM組
lvm 進入lvm管理
lvm> pvcreate /dev/xvdb1 創建虛擬分區
lvm> vgextend VolGroup /dev/xvdb1 把剛剛創建的分區添加到虛擬分區組
lvm>lvextend -L +79.5 /dev/mapper/VolGroup-lv_root 擴展容量(可以看到 /dev/mapper/VolGroup-lv_root)就是我們執行 df -h 時 (/)目錄的文件系統)
lvm> quit 退出
6. 文件系統擴容
resize2fs /dev/mapper/VolGroup-lv_root
至此,擴容全部完成,此時再次執行 df -h :
[root@shaonian ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 127G 22G 99G 18% / tmpfs 3.8G 0 3.8G 0% /dev/shm /dev/xvda1 485M 33M 427M 8% /boot /dev/mapper/VolGroup-lv_home 4.7G 138M 4.4G 4% /home cm_processes 3.8G 792K 3.8G 1% /var/run/cloudera-scm-agent/process
可以看到 (/)目錄的容量大大增加了
PS:順便一提,一共擴容了三台虛擬機,其中兩台都是很順利的,有一台不知道什么原因(可能是其中某一步操作錯誤),在執行
lvm> vgextend VolGroup /dev/mapper/Group-lv_root
操作時,出現了如下錯誤:
Couldn't find device with uuid cRdLUx-1ttO-JUKw-K2Bu-FOw2-EIrk-87Xdl3.
運行 pvscan 查看 pv:
lvm> pvscan Couldn't find device with uuid cRdLUx-1ttO-JUKw-K2Bu-FOw2-EIrk-87Xdl3. Couldn't find device with uuid 7Ef30A-6qRY-bBP0-CTH9-f6ut-kE8x-ch4c2j. PV /dev/xvda2 VG VolGroup lvm2 [59.51 GiB / 0 free] PV unknown device VG VolGroup lvm2 [80.00 GiB / 80.00 GiB free] PV unknown device VG VolGroup lvm2 [80.00 GiB / 80.00 GiB free] PV /dev/xvdb4 VG VolGroup lvm2 [80.00 GiB / 80.00 GiB free] Total: 4 [299.50 GiB] / in use: 4 [299.50 GiB] / in no VG: 0 [0 ]
發現 有兩行 出現了 unknown device
執行
lvm> vgreduce --removemissing VolGroup
即可。
