Linux下虚拟机扩展分区容量(硬盘空间)

一. VMware虚拟机设置

首先打开虚拟机设置,再选中硬盘选项,选择扩展选项(E),设置扩展大小(需要关机状态,并且不能有虚拟机快照),比如我原来是80G ,通过设置扩展到120G。这一步过后linux就多了40G空闲磁盘。但这只是VMware给该虚拟机分了硬盘,就像你加了块硬盘到电脑上,下面还要在linux系统中配置使硬盘空间正确挂载。

二. 创建分区

  1. 终端输入如下命令:
    fdisk /dev/sda

    接着,输入 p 查看分区信息,示例输出如下:

    Command (m for help): p
    Disk /dev/sda: 128.8 GB, 128849018880 bytes
    255 heads, 63 sectors/track, 15665 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: 0x00044781
    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        5221    20966162+  83  Linux
    /dev/sda4            5222       15665    83891430   83  Linux
  2. 创建分区,这里分两种情况:
    1). 一种是主分区未满4个,这时可以直接创建分区:
    fdisk command里输入 n(new一个新分区)此时进入扩展、主分区的选择,输入 p 选择主分区,如果你之前已有2个分区,那么就选择分区号为3;如果有已有3个分区,则选择分区号为4。最后会提示选择该分区的起始地址和结束地址,一般选默认的直接回车即可,此时再次输入 p 就可以看到刚才创建的新分区。

    2). 第二种已达到4个分区,则需要输入 d,然后输入 4,以删除第 4 分区,然后输入 n,输入 p 选择主分区,选择分区号为 4,以重新创建第 4 分区,最后会提示选择该分区的起始地址和结束地址,选默认的直接回车即可,此时再次输入 p 就可以看到刚才创建的新分区,示例输出如下:
[root@test ~]# fdisk /dev/sda

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/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: 0x0005210c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000   83  Linux
/dev/sda3            1301        1497     1572864   82  Linux swap / Solaris
/dev/sda4            1497        2611     8952832   83  Linux

Command (m for help): d           #删除分区
Partition number (1-4): 4         #删除第四个

Command (m for help): p       #再次查看分区信息,/dev/sda4已被删除

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: 0x0005210c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000   83  Linux
/dev/sda3            1301        1497     1572864   82  Linux swap / Solaris

Command (m for help): n      #创建新的分区
Command action
   e   extended
   p   primary partition (1-4)
p             #创建为主分区
Selected partition 4
First cylinder (1497-3916, default 1497):          #经对比,正好和上一个磁盘柱一致,默认回车即可
Using default value 1497
Last cylinder, +cylinders or +size{K,M,G} (1497-3916, default 3916): 
Using default value 3916              #直接默认回车即可

Command (m for help): p               #查看分区表信息

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: 0x0005210c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000   83  Linux
/dev/sda3            1301        1497     1572864   82  Linux swap / Solaris
/dev/sda4            1497        3916    19436582   83  Linux

Command (m for help): wp       #保存并退出,如果创建有误,直接退出不要保存即可
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.
  1. 创建物理卷 PV (Physical volume)
    这里根据前面的操作也分两种情况处理:
    1). 如果是直接创建的分区,则输入 pvcreate /dev/sda3 创建物理卷,然后输入 vgdisplay,查看下当前的卷组(volumegroup),结果为 VolGroup,然后输入 vgextend /dev/mapper/VolGroup /dev/sda3 将新的pv加入到原有的vg

    [root@test ~]# vgdisplay 
    --- Volume group ---
    VG Name               VolGroup
    System ID             
    Format                lvm2
    Metadata Areas        3
    Metadata Sequence No  10
    VG Access             read/write
    VG Status             resizable
    MAX LV                0
    Cur LV                2
    Open LV               2
    Max PV                0
    Cur PV                3
    Act PV                3
    VG Size               119.50 GiB
    PE Size               4.00 MiB
    Total PE              30591
    Alloc PE / Size       29826 / 116.51 GiB
    Free  PE / Size       765 / 2.99 GiB
    VG UUID               qXs6MO-nsYe-ASBU-FS9f-e9Im-XId7-7iQD45

    2). 如果是重新创建的分区,则输入 pvresize /dev/sda4 --setphysicalvolumesize 80G 调整物理卷的大小,大小80G为调整后的大小,需要根据 pvdisplay 获取到的 sda4物理卷的大小40G,然后加上我们此次调整增加的大小40G,最终为80G。重新创建的分区不需要将物理卷PV加入VG,因为以前已经加过了。

[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               0
  Allocated PE          4994
  PV UUID               8i4eDm-BIXi-2gz7-AROL-zinS-QeYE-2lJviN

  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               VolGroup
  PV Size               19.99 GiB / not usable 2.77 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              5118
  Free PE               0
  Allocated PE          5118
  PV UUID               N5z1G9-Qh6O-UoJo-Hweg-PLRy-Hr5Y-Q0lfPS

  --- Physical volume ---
  PV Name               /dev/sda4
  VG Name               VolGroup
  PV Size               80.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              20479
  Free PE               765
  Allocated PE          19714
  PV UUID               N4G0wG-cKp2-eIwU-hQXw-YKBo-2Wsk-iZLLsM
  1. 扩大逻辑卷LV
    扩大当前正在使用的(根文件系统)逻辑卷LV,从vg中取剩下的空间这里是40G,但是有保留地址,直接取40G会出错,所以这里我取39G,命令是:

    lvextend -L +39G /dev/mapper/VolGroup-lv_root

    其中,LV名可以通过 df -h 命令获取:

    [root@localhost ~]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                      113G   69G   39G  64% /
    tmpfs                 931M  224K  931M   1% /dev/shm
    /dev/sda1             477M   84M  369M  19% /boot
  2. 更新文件系统
    最后,输入命令:

    resize2fs  /dev/mapper/VolGroup-lv_root

    这一步重新刷新了文件系统大小,之后再次 df -h 查看一下,就可以看到当前根文件系统增加了接近40G!

0 0 vote
喜欢就给个好评吧!
赞(0) 打赏
未经允许不得转载:爱易默博客 » Linux下虚拟机扩展分区容量(硬盘空间)
Subscribe
提醒
guest
0 评论
Inline Feedbacks
View all comments
免责声明:本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有,请在下载后24小时之内自觉删除,若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系站长删除,我们将及时处理! Disclaimer: Most of the download resources on this site are collected on the Internet, and are only used for learning and communication. The copyright belongs to the original author. Please consciously delete within 24 hours after downloading. If you use it for commercial purposes, please purchase the original version. If the content posted on this site violates your rights, please contact us to delete it, and we will deal with it in time!

联系我们 Contact us

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

0
Would love your thoughts, please comment.x
()
x