为VMWare Ubuntu 20.04虚拟机的根目录扩容

前言

在VMWare中安装Ubuntu Server 20.04的时候,磁盘空间只分配了20G。后来在使用的过程中,空间不够用,需要给根目录扩容。现将具体流程记录如下,以供参考。

步骤

  1. 拍摄快照。因为后面步骤包含重新分区,操作不当会造成数据丢失。为了安全起见,在Ubuntu Server 20.04虚拟机运行状态下点击VMware Workstation菜单中的“虚拟机”->“快照”->“拍摄快照”,以便出问题可恢复本文所有操作之前的状态。
  2. 关闭Ubuntu Server 20.04虚拟机。扩展磁盘空间必须在虚拟机关机的状态下才能进行。
  3. 扩展硬盘空间。点击VMWare Workstation菜单中的“虚拟机”->“设置”->“硬件”->“硬盘”->“磁盘实用工具”->“扩展(E)…”,在弹出的对话框中填写你想扩展成的“最大磁盘大小(GB)”。比如,目前磁盘空间为20G,想扩展成30G,那么就填写“30”。最后点击“扩展”,并等待扩展完毕。这个时间可能有点儿长,需耐心等待。
  4. 启动Ubuntu Server 20.04虚拟机。成功扩展磁盘空间后,启动Ubuntu Server 20.04虚拟机,并以root用户身份登录。
  5. 查看当前磁盘挂载情况。在终端中使用df -h查看当前磁盘挂载情况,并将该结果保存在文本文档中。
    root@unbuntu-server-20-04:~# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    udev            3.9G     0  3.9G   0% /dev
    tmpfs           794M  2.0M  792M   1% /run
    /dev/sda2        20G   14G  5.3G  72% /
    tmpfs           3.9G     0  3.9G   0% /dev/shm
    tmpfs           5.0M     0  5.0M   0% /run/lock
    tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/loop3      9.2M  9.2M     0 100% /snap/canonical-livepatch/95
    /dev/loop4       15M   15M     0 100% /snap/aws-cli/130
    /dev/loop1      161M  161M     0 100% /snap/conjure-up/1061
    /dev/loop7       11M   11M     0 100% /snap/doctl/331
    /dev/loop2       55M   55M     0 100% /snap/core18/1705
    /dev/loop6       61M   61M     0 100% /snap/core20/634
    /dev/loop0       98M   98M     0 100% /snap/core/9289
    /dev/loop8       22M   22M     0 100% /snap/etcd/230
    /dev/loop11     109M  109M     0 100% /snap/google-cloud-sdk/137
    /dev/loop5       55M   55M     0 100% /snap/core18/1754
    /dev/loop9      129M  129M     0 100% /snap/juju/12370
    /dev/loop13      20M   20M     0 100% /snap/keepalived/1319
    /dev/loop12      81M   81M     0 100% /snap/kata-containers/655
    /dev/loop15      20M   20M     0 100% /snap/minidlna-escoand/19
    /dev/loop16     2.2M  2.2M     0 100% /snap/mosquitto/298
    /dev/loop17     254M  254M     0 100% /snap/nextcloud/21521
    /dev/loop22      14M   14M     0 100% /snap/prometheus/18
    /dev/loop24      12M   12M     0 100% /snap/slcli/1422
    /dev/loop26      30M   30M     0 100% /snap/snapd/8140
    /dev/loop18      60M   60M     0 100% /snap/powershell/132
    /dev/loop25     3.7M  3.7M     0 100% /snap/stress-ng/3677
    /dev/loop23      72M   72M     0 100% /snap/lxd/15675
    /dev/loop21     260M  260M     0 100% /snap/rocketchat-server/1433
    /dev/loop28     176M  176M     0 100% /snap/wekan/913
    /dev/loop10      26M   26M     0 100% /snap/heroku/3947
    /dev/loop27      28M   28M     0 100% /snap/snapd/7264
    /dev/loop20     131M  131M     0 100% /snap/sabnzbd/316
    /dev/loop29      16M   16M     0 100% /snap/wormhole/112
    /dev/loop19      39M   39M     0 100% /snap/postgresql10/47
    vmhgfs-fuse    1001G  722G  279G  73% /mnt/hgfs
    /dev/loop30      20M   20M     0 100% /snap/keepalived/1414
    /dev/loop31      72M   72M     0 100% /snap/lxd/15724
    tmpfs           794M     0  794M   0% /run/user/0
    /dev/loop14     109M  109M     0 100% /snap/google-cloud-sdk/138
    overlay          20G   14G  5.3G  72% /var/lib/docker/overlay2/56c7f371b883a978e63d6826092cb89300079e4a2e6d7f666116a0bdb397a551/merged
    overlay          20G   14G  5.3G  72% /var/lib/docker/overlay2/222a81b212e481fa17131f88d96ccca7ce67d1dc2e13d146f8745851c2f81a29/merged
    /dev/loop32     258M  258M     0 100% /snap/nextcloud/21796
  6. 查看当前磁盘分区情况。使用fdisk -l /dev/sda查看当前磁盘分区情况,并将结果保存到文本文档(后面步骤要用到里面第一个分区的Start数值,即第一个分区的起始Sector和结束Sector,根据下面的据结果,第一个分区的起始Sector值为2048,结束Sector值为4095)。因为我的虚拟机中只有一块磁盘,所以查看的是/dev/sda,如果有多块磁盘,可能需要查看/dev/sdb/dev/sdc等等(最后一个字母是按照字母表的顺序排列的,a是第一块磁盘,b是第二块磁盘,c是第三块磁盘,以此类推)。
    root@unbuntu-server-20-04:~# fdisk -l /dev/sda
    GPT PMBR size mismatch (41943039 != 62914559) will be corrected by write.
    Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
    Disk model: VMware Virtual S
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: D7B15429-4E4D-483A-8E38-491C6242D478
    
    Device     Start      End  Sectors Size Type
    /dev/sda1   2048     4095     2048   1M BIOS boot
    /dev/sda2   4096 41940991 41936896  20G Linux filesystem
  7. 关闭swap。重新设置分区,需要关闭swap。使用free -m命令查看swap使用情况,使用swapoff -a命令关闭swap。具体操作如下:
    root@unbuntu-server-20-04:~# free -m
                  total        used        free      shared  buff/cache   available
    Mem:           7934        3105        1176          17        3652        4507
    Swap:          4095           0        4095
    root@unbuntu-server-20-04:~# swapoff -a
    root@unbuntu-server-20-04:~# free -m
                  total        used        free      shared  buff/cache   available
    Mem:           7934        3234        1042          17        3657        4379
    Swap:             0           0           0
  8. 删除已有分区并重新分区。使用fdisk工具对/dev/sda磁盘重新分区。fdisk工具的用法可输入m查看帮助。重新分区主要包括删除已有分区、重新分区、修改分区类型和确认分区等部分,具体流程如下:
    root@unbuntu-server-20-04:~# fdisk /dev/sda  # 使用fdisk工具对/dev/sda磁盘重新分区
    
    Welcome to fdisk (util-linux 2.34).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    GPT PMBR size mismatch (41943039 != 62914559) will be corrected by write.
    
    Command (m for help): p  # 打印当前分区情况,可见/dev/sda磁盘上目前有两个分区,分别为sda1和sda2
    
    Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
    Disk model: VMware Virtual S
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: D7B15429-4E4D-483A-8E38-491C6242D478
    
    Device     Start      End  Sectors Size Type
    /dev/sda1   2048     4095     2048   1M BIOS boot
    /dev/sda2   4096 41940991 41936896  20G Linux filesystem
    
    Command (m for help): d     # 删除分区命令
    Partition number (1,2, default 2):  # 选择分区编号,此处默认为第二个分区,即sda2
    
    Partition 2 has been deleted.
    
    Command (m for help): d # 删除分区命令。因为上一步把第二个分区sda2删除之后,只剩下sda1一个分区,所以后面没有提示选择删除哪个分区
    Selected partition 1
    Partition 1 has been deleted.
    
    Command (m for help): p    # 打印当前分区情况,可见目前已经没有分区了。
    Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
    Disk model: VMware Virtual S
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: D7B15429-4E4D-483A-8E38-491C6242D478
    
    Command (m for help): n    # 创建分区命令。
    Partition number (1-128, default 1): 1  # 选择分区编号,此处选择1,即创建sda1分区。
    First sector (34-62914526, default 2048):   # 根据第6步查看的结果,第一个分区是BISO boot分区,不是我们需要扩展的根目录,
    #  所以First sector的值要填写2048。该值一定与第6步查看的第一个分区起始Sector的值相同。
    #  此处默认就是2048,与实际相同,故可使用默认值。
    Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62914526, default 62914526): 4095 # 此处需要填写第6步查看的结果,为第一个分区的结束地址,本例为4095。
    
    Created a new partition 1 of type 'Linux filesystem' and of size 1 MiB.
    
    Command (m for help): n   # 继续使用创建分区命令创建第二个分区,sda2为我们需要扩展的根目录所挂载的分区
    Partition number (2-128, default 2):    # 选择分区编号,此处选择2,即创建sda2分区。
    First sector (4096-62914526, default 4096):   # sda2分区紧跟在是sda1分区之后。此处使用默认值即可。
    Last sector, +/-sectors or +/-size{K,M,G,T,P} (4096-62914526, default 62914526):    # 输入sda2分区结束地址,即磁盘结束地址。此处使用默认值即可。
    
    Created a new partition 2 of type 'Linux filesystem' and of size 30 GiB.
    Partition #2 contains a ext4 signature.
    
    Do you want to remove the signature? [Y]es/[N]o: Y   # 此处要选择Yes,即删除ext4 signature
    
    The signature will be removed by a write command.
    
    Command (m for help): p    # 打印当前分区情况,可见目前已有新建的两个分区。
    Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
    Disk model: VMware Virtual S
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: D7B15429-4E4D-483A-8E38-491C6242D478
    Device     Start      End  Sectors Size Type
    /dev/sda1   2048     4095     2048   1M Linux filesystem
    /dev/sda2   4096 62914526 62910431  30G Linux filesystem
    #  注意到sda1的分区类型为Linux filesystem,与第6步查看到的BIOS boot类型不同,
    #  故需更改sda1分区的分区类型。
    Command (m for help): t  # 使用t命令更改分区类型
    Partition number (1,2, default 2): 1  # 选择第一个分区,即sda1
    Partition type (type L to list all types): 4   # 输入L可查看所有的分区类型,输入q<Enter>退出查看。
                                                   #  此处4即代表BIOS boot分区类型
    
    Changed type of partition 'Linux filesystem' to 'BIOS boot'.
    
    Command (m for help): p   # 打印当前分区情况,目前分区结果已达到我们的目的。
    Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
    Disk model: VMware Virtual S
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: D7B15429-4E4D-483A-8E38-491C6242D478
    
    Device     Start      End  Sectors Size Type
    /dev/sda1   2048     4095     2048   1M BIOS boot
    /dev/sda2   4096 62914526 62910431  30G Linux filesystem
    
    Filesystem/RAID signature on partition 2 will be wiped.
    
    Command (m for help): w   # 使用w命令确认分区结果,进行分区修改。
    The partition table has been altered.
    Syncing disks.
    
    root@unbuntu-server-20-04:~# reboot   # 重新分区之后一定要重启虚拟机。
    Connection closing...Socket close.
    
    Connection closed by foreign host.
    
    Disconnected from remote host(VM-ubuntu server 20.04-confluence) at 20:52:33.
  9. 扩展根目录磁盘空间。虚拟机重启之后,使用df -h查看根目录磁盘空间还是20G,并没有变成30G。需要使用resize2fs /dev/sda2命令扩展根目录磁盘空间(其中/dev/sda2为根目录挂载分区)。扩展完后再次使用df -h可查看到根目录磁盘空间(即/dev/sda2分区空间)已变为30G。到此,扩展根目录空间成功。
    root@unbuntu-server-20-04:~# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    udev            3.9G     0  3.9G   0% /dev
    tmpfs           794M  2.0M  792M   1% /run
    /dev/sda2        20G   14G  5.4G  72% /       # 根目录空间依旧为20G
    tmpfs           3.9G     0  3.9G   0% /dev/shm
    tmpfs           5.0M     0  5.0M   0% /run/lock
    tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/loop2       98M   98M     0 100% /snap/core/9289
    /dev/loop3      109M  109M     0 100% /snap/google-cloud-sdk/138
    /dev/loop4       55M   55M     0 100% /snap/core18/1705
    /dev/loop5       55M   55M     0 100% /snap/core18/1754
    /dev/loop0       15M   15M     0 100% /snap/aws-cli/130
    /dev/loop1      161M  161M     0 100% /snap/conjure-up/1061
    /dev/loop6       61M   61M     0 100% /snap/core20/634
    /dev/loop7      9.2M  9.2M     0 100% /snap/canonical-livepatch/95
    /dev/loop8       11M   11M     0 100% /snap/doctl/331
    /dev/loop9       30M   30M     0 100% /snap/snapd/8140
    /dev/loop10      26M   26M     0 100% /snap/heroku/3947
    /dev/loop11      81M   81M     0 100% /snap/kata-containers/655
    /dev/loop12     258M  258M     0 100% /snap/nextcloud/21796
    /dev/loop15      39M   39M     0 100% /snap/postgresql10/47
    /dev/loop13      20M   20M     0 100% /snap/keepalived/1319
    /dev/loop14      60M   60M     0 100% /snap/powershell/132
    /dev/loop16      14M   14M     0 100% /snap/prometheus/18
    /dev/loop17      72M   72M     0 100% /snap/lxd/15724
    /dev/loop18      16M   16M     0 100% /snap/wormhole/112
    /dev/loop19      72M   72M     0 100% /snap/lxd/15675
    /dev/loop20     2.2M  2.2M     0 100% /snap/mosquitto/298
    /dev/loop21      12M   12M     0 100% /snap/slcli/1422
    /dev/loop22     131M  131M     0 100% /snap/sabnzbd/316
    /dev/loop23     254M  254M     0 100% /snap/nextcloud/21521
    /dev/loop24      22M   22M     0 100% /snap/etcd/230
    /dev/loop25     129M  129M     0 100% /snap/juju/12370
    /dev/loop26      20M   20M     0 100% /snap/minidlna-escoand/19
    /dev/loop27     260M  260M     0 100% /snap/rocketchat-server/1433
    /dev/loop28      20M   20M     0 100% /snap/keepalived/1414
    /dev/loop29     176M  176M     0 100% /snap/wekan/913
    /dev/loop30     109M  109M     0 100% /snap/google-cloud-sdk/137
    /dev/loop31     3.7M  3.7M     0 100% /snap/stress-ng/3677
    /dev/loop32      28M   28M     0 100% /snap/snapd/7264
    vmhgfs-fuse    1001G  723G  278G  73% /mnt/hgfs
    overlay          20G   14G  5.4G  72% /var/lib/docker/overlay2/56c7f371b883a978e63d6826092cb89300079e4a2e6d7f666116a0bdb397a551/merged
    overlay          20G   14G  5.4G  72% /var/lib/docker/overlay2/222a81b212e481fa17131f88d96ccca7ce67d1dc2e13d146f8745851c2f81a29/merged
    tmpfs           794M     0  794M   0% /run/user/0
    root@unbuntu-server-20-04:~# resize2fs /dev/sda2   # 使用命令扩展sda2分区空间
    resize2fs 1.45.5 (07-Jan-2020)
    Filesystem at /dev/sda2 is mounted on /; on-line resizing required
    old_desc_blocks = 3, new_desc_blocks = 4
    The filesystem on /dev/sda2 is now 7863803 (4k) blocks long.
    
    root@unbuntu-server-20-04:~# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    udev            3.9G     0  3.9G   0% /dev
    tmpfs           794M  2.0M  792M   1% /run
    /dev/sda2        30G   14G   15G  49% /    # 根目录空间已成功扩展成30G
    tmpfs           3.9G     0  3.9G   0% /dev/shm
    tmpfs           5.0M     0  5.0M   0% /run/lock
    tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/loop2       98M   98M     0 100% /snap/core/9289
    /dev/loop3      109M  109M     0 100% /snap/google-cloud-sdk/138
    /dev/loop4       55M   55M     0 100% /snap/core18/1705
    /dev/loop5       55M   55M     0 100% /snap/core18/1754
    /dev/loop0       15M   15M     0 100% /snap/aws-cli/130
    /dev/loop1      161M  161M     0 100% /snap/conjure-up/1061
    /dev/loop6       61M   61M     0 100% /snap/core20/634
    /dev/loop7      9.2M  9.2M     0 100% /snap/canonical-livepatch/95
    /dev/loop8       11M   11M     0 100% /snap/doctl/331
    /dev/loop9       30M   30M     0 100% /snap/snapd/8140
    /dev/loop10      26M   26M     0 100% /snap/heroku/3947
    /dev/loop11      81M   81M     0 100% /snap/kata-containers/655
    /dev/loop12     258M  258M     0 100% /snap/nextcloud/21796
    /dev/loop15      39M   39M     0 100% /snap/postgresql10/47
    /dev/loop13      20M   20M     0 100% /snap/keepalived/1319
    /dev/loop14      60M   60M     0 100% /snap/powershell/132
    /dev/loop16      14M   14M     0 100% /snap/prometheus/18
    /dev/loop17      72M   72M     0 100% /snap/lxd/15724
    /dev/loop18      16M   16M     0 100% /snap/wormhole/112
    /dev/loop19      72M   72M     0 100% /snap/lxd/15675
    /dev/loop20     2.2M  2.2M     0 100% /snap/mosquitto/298
    /dev/loop21      12M   12M     0 100% /snap/slcli/1422
    /dev/loop22     131M  131M     0 100% /snap/sabnzbd/316
    /dev/loop23     254M  254M     0 100% /snap/nextcloud/21521
    /dev/loop24      22M   22M     0 100% /snap/etcd/230
    /dev/loop25     129M  129M     0 100% /snap/juju/12370
    /dev/loop26      20M   20M     0 100% /snap/minidlna-escoand/19
    /dev/loop27     260M  260M     0 100% /snap/rocketchat-server/1433
    /dev/loop28      20M   20M     0 100% /snap/keepalived/1414
    /dev/loop29     176M  176M     0 100% /snap/wekan/913
    /dev/loop30     109M  109M     0 100% /snap/google-cloud-sdk/137
    /dev/loop31     3.7M  3.7M     0 100% /snap/stress-ng/3677
    /dev/loop32      28M   28M     0 100% /snap/snapd/7264
    vmhgfs-fuse    1001G  723G  278G  73% /mnt/hgfs
    overlay          30G   14G   15G  49% /var/lib/docker/overlay2/56c7f371b883a978e63d6826092cb89300079e4a2e6d7f666116a0bdb397a551/merged
    overlay          30G   14G   15G  49% /var/lib/docker/overlay2/222a81b212e481fa17131f88d96ccca7ce67d1dc2e13d146f8745851c2f81a29/merged
    tmpfs           794M     0  794M   0% /run/user/0

发表评论

电子邮件地址不会被公开。 必填项已用*标注

20 − 17 =

81 − 77 =