CentOS虚拟机根分区磁盘扩容纪实

作者:fbysss
QQ:溜酒酒吧酒吧吾散
blog:blog.csdn.net/fbysss

声明:本文由fbysss原创,转载请注明出处

========================================================
背景:开发服务器是虚拟机,原来只规划了2G,后来发现硬盘不够用了,需要进行扩容。
参考文章:《实战Linux下VMware虚拟机根目录空间扩充》
http://blog.sina.com.cn/s/blog_709919050100wap1.html
注:请首先阅读参考文章,本文是在其基础之上,在不同环境中进行的案例。
同时,原文有几处地方没有交代的那么清楚,初学者可能会走弯路, 在这里进行了相应补充。
========================================================
看到文章说必须基于LVM,那是不是呢?命令检验一下。据说centos默认安装也是LVM的。
[sss@localhost ~]$ lvdisplay
 --- Logical volume ---
  LV Path                /dev/vg_vm/lv_root
  LV Name                lv_root
  VG Name                vg_vm
  LV UUID                bWyLlY-hYra-7hPc-2eU9-tXMH-FJFa-7ZLDoF
  LV Write Access        read/write
  LV Creation host, time vm.sss, 2015-01-05 03:29:55 +0800
  LV Status              available
  # open                 1
  LV Size                6.71 GiB
  Current LE             1718
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
  
  --- Logical volume ---
  LV Path                /dev/vg_vm/lv_swap
  LV Name                lv_swap
  VG Name                vg_vm
  LV UUID                oLj3cQ-USD7-JIGZ-HMJz-NlmA-aQf6-lECYyC
  LV Write Access        read/write
  LV Creation host, time vm.sss, 2015-01-05 03:29:56 +0800
  LV Status              available
  # open                 1
  LV Size                816.00 MiB
  Current LE             204
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
[sss@localhost ~]$ sudo fdisk -l

Disk /dev/sda:  32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x00036057

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1045     7875584   8e  Linux LVM

Disk  /dev/mapper/vg_vm-lv_root: 7205 MB, 7205814272 bytes
255 heads, 63 sectors/track, 876 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/vg_vm-lv_swap: 855 MB, 855638016 bytes
255 heads, 63 sectors/track, 104 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

然后,在windows虚拟机管理软件上,给虚拟机扩容——前提是关闭虚拟机OS。
来个30G,当然,进去之后,可以看到总容量增加了。到32G了,还是没有发现新的分区,虚拟机软件也已经提醒了。
[sss@localhost ~]$  sudo fdisk /dev/sda



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)
Syncing disks.
最后一步出现这样的提示。

但是我们发现,fdisk -l,这个分区已经出来了。
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1045     7875584   8e  Linux LVM
/dev/sda3            1045        3916    23066662   83  Linux

当然,这时候用ls /dev是看不到sd3的。因为还没格式化。
sudo mkfs -t ext3 /dev/sda3 现在也是报错的。
Could not stat /dev/sdb3之类的。
既然上次已经提示了要reboot,那就roboot一下吧。
[sss@localhost ~]$ partprobe
还是出现同样的警告。难道还是不行?
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
管他呢,执行一下格式化吧。
[sss@localhost ~]$ sudo mkfs -t ext3 /dev/sda3
居然OK了!这不吓唬人吗?
[sss@localhost ~]$ ll /dev/sda3
brw-rw----. 1 root disk 8, 3 Jan  6 10:33 /dev/sda3
嘿嘿,已经有了哦
然而,看看磁盘空间呢?怎么还是这几个分区呢
[sss@localhost ~]$  df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/vg_vm-lv_root  6.7G  6.5G   83M  99% /
tmpfs                      495M   80K  495M   1% /dev/shm
/dev/sda1                  485M  326M  134M  71% /boot

sudo fdisk -l也可以发现,还是只有一个LVM的分区。
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1045     7875584   8e  Linux LVM
/dev/sda3            1045        3916    23066662   83  Linux


[sss@localhost ~]$ sudo lvs
  LV      VG    Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_root vg_vm -wi-ao----   6.71g                                            
  lv_swap vg_vm -wi-ao---- 816.00m  


[sss@localhost ~]$ sudo pvcreate /dev/sda3
  dev_is_mpath: failed to get device for 8:3
  Physical volume "/dev/sda3" successfully created
这里有一个failed,以为出问题了呢,结果下面又success,玩我呢?不管他了。因为一查,都是这样的提示,没有问题。

/dev/mapper/vg_vm-lv_root  6.7G  6.5G   83M  99% /
tmpfs                      495M   80K  495M   1% /dev/shm
/dev/sda1                  485M  326M  134M  71% /boot

/dev/mapper/vg_vm-lv_root这个应该就是LVM的组名,这里显示的不是sda2.
来扩充这个组吧。
[sss@localhost ~]$ df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/vg_vm-lv_root  6.7G  6.5G   83M  99% /
tmpfs                      495M   80K  495M   1% /dev/shm
/dev/sda1                  485M  326M  134M  71% /boot
[sss@localhost ~]$  sudo vgextend /dev/mapper/vg_vm-lv_root /dev/sda3
  Volume group name vg_vm/lv_root has invalid characters
[sss@localhost ~]$ sudo vgextend /dev/mapper/vg_vm /dev/sda3       
  Volume group "vg_vm" successfully extended

原来这个组名,是-之前的字符串——这要是没有文章看着对比分析,谁知道啊?

[sss@localhost ~]$  sudo vgdisplay
  --- Volume group ---
  VG Name               vg_vm
  System ID            
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               29.50 GiB
  PE Size               4.00 MiB
  Total PE              7553
  Alloc PE / Size       1922 / 7.51 GiB
  Free  PE / Size       5631 / 22.00 GiB
  VG UUID               8GwL0o-DTdc-Lmkc-gCCC-RBvv-yOQp-wMgpeq


[sss@localhost ~]$  sudo lvextend -L +22G /dev/vg_vm/lv_root /dev/sda3
  Extending logical volume lv_root to 28.71 GiB
  Insufficient free space: 5632 extents needed, but only 5631 available
估计是这其中用了一部分。22G没那么准了。那么就21.0G吧
[sss@localhost ~]$  sudo lvextend -L +21.9G /dev/vg_vm/lv_root /dev/sda3
  Rounding size to boundary between physical extents: 21.90 GiB
  Extending logical volume lv_root to 28.61 GiB
  Logical volume lv_root successfully resized

文中所说的vim /etc/fstab这一个步骤,没太看明白。
里面的内容是否有改变?因为看到少了一个mapper
我没有管他,并没有对这个文件进行修改,然后继续往下。

[sss@localhost ~]$  sudo e2fsck -f /dev/vg_vm/lv_root
e2fsck 1.41.12 (17-May-2010)
/dev/vg_vm/lv_root is mounted.
e2fsck: Cannot continue, aborting.

不知为何报错。但是这只是一个检查命令,继续往前吧。
[sss@localhost ~]$  sudo resize2fs  /dev/vg_vm/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_vm/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg_vm/lv_root to 7500800 (4k) blocks.
The filesystem on /dev/vg_vm/lv_root is now 7500800 blocks long.

[sss@localhost ~]$  df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/vg_vm-lv_root   29G  6.5G   22G  24% /
tmpfs                      495M   80K  495M   1% /dev/shm
/dev/sda1                  485M  326M  134M  71% /boot

可以看到,这个逻辑卷已经成功的扩容,根目录又可以装更多东西了。yeah!
==========================================================================



你可能感兴趣的:(Unix类)