虚拟机硬盘扩容


测试机器环境:

VMware Workstation 12 虚拟机

虚拟机:

Linux版本:CentOS MinimalCD 6.5

Windows版本:Windows 7


查看原机器硬盘配置

磁盘20G

增加磁盘容量有2种方案:

  1. 增加虚拟磁盘容量,下图将原盘20G扩容到50G:
    磁盘50G
  2. 添加一块新虚拟磁盘,下图增加一块50G的磁盘:
    sp161220_104000

2种方案都要在系统里作扩容处理。

Linux系统扩容:

2种方案扩容基本相同,先将扩的磁盘容量分区,并转换成lvm盘,作pv,加入lv,将系统原有的lvm扩容,文件系统重定义大小。

以下是对第1种方案的详细步骤:

  • 用 fdisk 配置新的具有 8e system ID 的 partition

没有扩容前查看磁盘信息:

[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x0009de9a

   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        2611    20458496   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 18.9 GB, 18865979392 bytes
255 heads, 63 sectors/track, 2293 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: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 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

[root@localhost ~]# df -H
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       19G  704M   17G   5% /
tmpfs                 515M     0  515M   0% /dev/shm
/dev/sda1             500M   29M  445M   7% /boot

查看磁盘sda已扩展到50G:

[root@localhost ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x0009de9a

   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        2611    20458496   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 18.9 GB, 18865979392 bytes
255 heads, 63 sectors/track, 2293 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: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 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命令将未分区的磁盘分区,具体百度fdisk,并将新的分区改为lvm区。查看磁盘信息:

[root@localhost ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x000e545e

   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        2611    20458496   8e  Linux LVM
/dev/sda3            2611        6528    31457280   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 18.9 GB, 18865979392 bytes
255 heads, 63 sectors/track, 2293 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: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 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

分完区,sda3只是存于系统,但还没有加载这个设备,partprobe命令可以不重启及加载设备,但是Minimal版本没有安装这个命令,重启系统显然更加方便。

重启后查看设备:

[root@localhost ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3
  • 利用 pvcreate 创建 PV

先查看一下pv:

[root@localhost ~]# pvscan    #查看pv信息 
  PV /dev/sda2   VG VolGroup        lvm2 [19.51 GiB / 0    free]
  Total: 1 [19.51 GiB] / in use: 1 [19.51 GiB] / in no VG: 0 [0   ]

将sda3做成pv

[root@localhost ~]#  pvcreate /dev/sda3    #将sda3创建成pv
  Physical volume "/dev/sda3" successfully created
[root@localhost ~]# pvscan 
  PV /dev/sda2   VG VolGroup        lvm2 [19.51 GiB / 0    free]
  PV /dev/sda3                      lvm2 [30.00 GiB]
  Total: 2 [49.51 GiB] / in use: 1 [19.51 GiB] / in no VG: 1 [30.00 GiB]
  • 利用 vgextend 将 PV 加入我们的VolGroup

    [root@localhost ~]# vgscan        #查看vg信息 
      Reading all physical volumes.  This may take a while...
      Found volume group "VolGroup" using metadata type lvm2
    [root@localhost ~]# vgextend VolGroup /dev/sda3    #将sda3加入VolGroup
      Volume group "VolGroup" successfully extended
    [root@localhost ~]# vgdisplay    #显示VG信息--- Volume group ---
      VG Name               VolGroup
      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               49.50 GiB
      PE Size               4.00 MiB
      Total PE              12673
      Alloc PE / Size       4994 / 19.51 GiB
      Free  PE / Size       7679 / 30.00 GiB
      VG UUID               cr8dzd-IkxI-gvEj-nA5K-gYlM-eYal-caOLdz
  • 利用 lvresize 将新加入的 PV 内的 PE 加入lv_root中,透过 xfs_growfs将文件系统的容量确实添加。

    [root@localhost ~]# lvscan       #查看lv信息
      ACTIVE            '/dev/VolGroup/lv_root' [17.57 GiB] inherit
      ACTIVE            '/dev/VolGroup/lv_swap' [1.94 GiB] inherit
    [root@localhost ~]#  lvextend -L 30G /dev/VolGroup/lv_root /dev/sda3    #从sda3取容量,扩充lv_root到30G
      Size of logical volume VolGroup/lv_root changed from 17.57 GiB (4498 extents) to 30.00 GiB (7680 extents).
      Logical volume lv_root successfully resized.
    [root@localhost ~]# xfs_growfs /dev/VolGroup/lv_root 
    meta-data=/dev/mapper/cl-home isize=512 agcount=4, agsize=4464128 blks
    = sectsz=4096 attr=2, projid32bit=1
    = crc=1 finobt=0 spinodes=0
    data = bsize=4096 blocks=17856512, imaxpct=25
    = sunit=0 swidth=0 blks
    naming =version 2 bsize=4096 ascii-ci=0 ftype=1
    log =internal bsize=4096 blocks=8719, version=2
    = sectsz=4096 sunit=1 blks, lazy-count=1
    realtime =none extsz=4096 blocks=0, rtextents=0
    data blocks changed from 17856512 to 115635200 [root@localhost
    ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 VolGroup lvm2 a--u 19.51g 0 /dev/sda3 VolGroup lvm2 a--u 30.00g 17.57g #上面的步骤将lv_root扩充到30G,sda3还剩余17.57G

    [root@localhost ~]# df -H
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                           32G  748M   30G   3% /
    tmpfs                 515M     0  515M   0% /dev/shm
    /dev/sda1             500M   29M  445M   7% /boot

  另一种增加lv_root的示例 :

 [root@localhost ~]# vgdisplay 
  --- Volume group ---
  VG Name               VolGroup
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  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               49.50 GiB
  PE Size               4.00 MiB
  Total PE              12673
  Alloc PE / Size       8176 / 31.94 GiB
  Free  PE / Size       4497 / 17.57 GiB    #Free PE
  VG UUID               cr8dzd-IkxI-gvEj-nA5K-gYlM-eYal-caOLdz
[root@localhost ~]#  lvextend -l +4497 /dev/VolGroup/lv_root /dev/sda3  #参数 l 是使用pe数,目前freePE为4497,本命令行是将所有freePE增加给lv_root  
  Size of logical volume VolGroup/lv_root changed from 30.00 GiB (7680 extents) to 47.57 GiB (12177 extents).
  Logical volume lv_root successfully resized.
[root@localhost ~]#  resize2fs /dev/VolGroup/lv_root  
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/VolGroup/lv_root to 12469248 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 12469248 blocks long.

[root@localhost ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup lvm2 a--u 19.51g    0 
  /dev/sda3  VolGroup lvm2 a--u 30.00g    0 
[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   2   2   0 wz--n- 49.50g    0 
[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       47G  721M   44G   2% /
tmpfs                 491M     0  491M   0% /dev/shm
/dev/sda1             477M   28M  425M   7% /boot

Windows系统扩容:

Windows可以借助磁盘工具将增加的磁盘扩容进系统中,2008以上的系统可以直接借助系统的磁盘管理操作。这里不在详述。

原文地址:https://www.cnblogs.com/ysztcn/p/6203013.html