专业的编程技术博客社区

网站首页 > 博客文章 正文

2个方法:虚拟机VMware容量扩展,无vg分组的linux无损磁盘扩容

baijin 2024-09-23 03:53:23 博客文章 7 ℃ 0 评论

目的是:将虚拟机的容量从20G扩大到25G

编辑虚拟机设置 :

硬盘 - 扩展磁盘容量 :

填入扩容后大小:

以下在ubuntu/linux里操作:

查看当前分区情况:

biren@ubuntu:~$ df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  955M     0  955M   0% /dev
tmpfs          tmpfs     196M  1.8M  195M   1% /run
/dev/sda1      ext4       20G   19G  498M  98% /      只剩下542M空间,ext4格式
tmpfs          tmpfs     980M     0  980M   0% /dev/shm
tmpfs          tmpfs     5.0M  4.0K  5.0M   1% /run/lock
tmpfs          tmpfs     980M     0  980M   0% /sys/fs/cgroup
/dev/loop0     squashfs  768K  768K     0 100% /snap/gnome-characters/741
......
biren@ubuntu:~/crossbuild$ sudo fdisk -l

Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors  共25G空间
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: dos
Disk identifier: 0x305e1b1e

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *     2048 41940991 41938944  20G 83 Linux    第一块磁盘的第一分区
其它略...

可以看到/dev/sda共25G,实际使用的还是20G(/dev/sda1)

新增分区:

命令: sudo fdisk /dev/sda

biren@ubuntu:~/crossbuild$ sudo fdisk /dev/sda
[sudo] password for biren: 

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): m      查看帮助

Help:
  DOS (MBR)
   a   toggle a bootable flag  设置启动标记
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition    删除分区
   F   list free unpartitioned space
   l   list known partition types  列出分区类型,如 83/8e
   n   add a new partition           增加分区
   p   print the partition table
   t   change a partition type      改变分区类型, 83为linux, 8e为lvm, 82为swap交换分区, 6为fat16, 7为nfs
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit    保存退出
   q   quit without saving changes  不保存退出

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

Command (m for help): 

选择: n , 即增加分区

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)      主分区
   e   extended (container for logical partitions) 扩展分区
Select (default p): p            回车默认即可
Partition number (2-4, default 2): 回车默认即可
First sector (41940992-52428799, default 41940992): 回车默认即可
Last sector, +sectors or +size{K,M,G,T,P} (41940992-52428799, default 52428799): 回车默认即可

Created a new partition 2 of type 'Linux' and of size 5 GiB.
Command (m for help): 

接头选择: w (表示写入分区表并退出)

Command (m for help): w
The partition table has been altered.
Syncing disks.

查看结果:

biren@ubuntu:~/crossbuild$ sudo fdisk -l

Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
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: dos
Disk identifier: 0x305e1b1e

Device     Boot    Start      End  Sectors Size Id Type
/dev/sda1  *        2048 41940991 41938944  20G 83 Linux
/dev/sda2       41940992 52428799 10487808   5G 83 Linux    新增加的, ID为83
biren@ubuntu:~$ lsblk
sda      8:0    0    25G  0 disk 
├─sda1   8:1    0    20G  0 part /    根分区
└─sda2   8:2    0     5G  0 part       注意:type类型是part, 不是lvm

刷新分区表时提示要重启:

biren@ubuntu:~/crossbuild$ sudo partprobe /dev/sda2
Error: Partition(s) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64 on /dev/sda2 have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.

biren@ubuntu:~$ sudo partprobe /dev/sda
或 biren@ubuntu:~$ sudo partprobe

重启。。。

安装逻辑卷管理器lvm2 :

lvm管理原理:大致是新建物理卷pv,新建卷组vg或用旧的vg,将多个pv加入vg里,从vg里再分区建立逻辑卷lv, 扩容则扩大lv (在lv基础上创建文件系统)

biren@ubuntu:~$ sudo apt-get install lvm2

将物理磁盘分区初始化为物理卷:

命令 : sudo pvcreate /dev/sda2

biren@ubuntu:~$ sudo pvcreate /dev/sda2
  Physical volume "/dev/sda2" successfully created.

查看:

  • PV name : Physical volume name ,物理卷
  • VG name : Volume Group name ,卷组,多个PV组成VG
  • LV : Logical Volume ,逻辑卷,是从vg中分出来的空间,用于创建文件系统
biren@ubuntu:~$ sudo pvdisplay
  "/dev/sda2" is a new physical volume of "5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda2
  VG Name                                 为空表示没有分组(因为当前只有一块磁盘,一个分区)
  PV Size               5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               BmvOyc-hy2d-YnJS-hwnu-aFfV-NqTd-gXqFAT

biren@ubuntu:~$ sudo vgdisplay            结果没内容,说明没有分组
biren@ubuntu:~$ sudo blkid
/dev/loop0: TYPE="squashfs"
/dev/loop1: TYPE="squashfs"
/dev/loop2: TYPE="squashfs"
/dev/loop3: TYPE="squashfs"
/dev/loop4: TYPE="squashfs"
/dev/loop5: TYPE="squashfs"
/dev/loop6: TYPE="squashfs"
/dev/loop7: TYPE="squashfs"
/dev/sda1: UUID="b19b2c95-bd2c-4078-af6d-e09aadd02861" TYPE="ext4" PARTUUID="305e1b1e-01"
/dev/sda2: UUID="BmvOyc-hy2d-YnJS-hwnu-aFfV-NqTd-gXqFAT" TYPE="LVM2_member" PARTUUID="305e1b1e-02"
/dev/loop8: TYPE="squashfs"
/dev/loop9: TYPE="squashfs"
/dev/loop10: TYPE="squashfs"
/dev/loop11: TYPE="squashfs"
/dev/loop12: TYPE="squashfs"
/dev/loop13: TYPE="squashfs"
/dev/loop14: TYPE="squashfs"
/dev/loop15: TYPE="squashfs"
/dev/loop16: TYPE="squashfs"
/dev/loop17: TYPE="squashfs"
/dev/loop18: TYPE="squashfs"
/dev/loop19: TYPE="squashfs"
  • /dev/sda1 : type="ext4"
  • /dev/sda2 : type="LVM2_member"

由于没有vg卷组和类型不一样,此时可以用如下两种方法解决:

第一种方法:(前面创建物理卷可不要)

先删除所有分区,然后重新只分成一个区,但起始地址一定要与原来一样的。

当前情况记住起始地址2048

biren@ubuntu:~$ sudo pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sda2     lvm2 ---  5.00g 5.00g

biren@ubuntu:~$ sudo fdisk -l /dev/sda
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
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: dos
Disk identifier: 0x305e1b1e

Device     Boot    Start      End  Sectors Size Id Type
/dev/sda1  *        2048 41940991 41938944  20G 83 Linux   记住起始地址2048
/dev/sda2       41940992 52428799 10487808   5G 83 Linux

先删除所有分区 - 新建分区 - 保存退出:

biren@ubuntu:~$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d    删除分区
Partition number (1,2, default 2): 2  分区号

Partition 2 has been deleted.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): p
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
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: dos
Disk identifier: 0x305e1b1e

Command (m for help): n    新建分区
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p     主分区
Partition number (1-4, default 1): 默认即可
First sector (2048-52428799, default 2048):  默认即可
Last sector, +sectors or +size{K,M,G,T,P} (2048-52428799, default 52428799): 默认即可

Created a new partition 1 of type 'Linux' and of size 25 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n   这里一定要选n

Command (m for help): w     保存退出

The partition table has been altered.
Syncing disks.

看一下新的现状:

biren@ubuntu:~$ sudo fdisk -l /dev/sda
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
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: dos
Disk identifier: 0x305e1b1e

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1        2048 52428799 52426752  25G 83 Linux

biren@ubuntu:~$ sudo lsblk
sda      8:0    0    25G  0 disk 
└─sda1   8:1    0    25G  0 part /      sda1显示25GB 

  biren@ubuntu:~$ sudo blkid
/dev/sda1: UUID="b19b2c95-bd2c-4078-af6d-e09aadd02861" TYPE="ext4" PARTUUID="305e1b1e-01"

biren@ubuntu:~$ df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  955M     0  955M   0% /dev
tmpfs          tmpfs     196M  1.8M  195M   1% /run
/dev/sda1      ext4       20G   19G  458M  98% /     目前没生效
tmpfs          tmpfs     980M     0  980M   0% /dev/shm
。。。

刷新分区大小:

命令:

  • ext4用 : sudo resize2fs /dev/sda1
  • xfs用: xfs_growfs /dev/sda1
biren@ubuntu:~$ sudo partprobe /dev/sda

biren@ubuntu:~$ sudo resize2fs /dev/sda1
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/sda1 is now 6553344 (4k) blocks long.

分区大小已经更新完成了:

biren@ubuntu:~$ df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  955M     0  955M   0% /dev
tmpfs          tmpfs     196M  1.8M  195M   1% /run
/dev/sda1      ext4       25G   19G  5.2G  78% /        变成25G了
tmpfs          tmpfs     980M     0  980M   0% /dev/shm
。。。


第二种方法:

安装gparted工具:

biren@ubuntu:~$ sudo apt install gparted

打开gparted :

先删除sda2(如果有unallocated空间则直接扩容):

修改大小:

拉动箭头或直接输入新的大小:

应用修改:

另外说明 :

格式化命令:mkfs + 文件系统格式

  • mkfs.bfs
  • mkfs.ext2
  • mkfs.ext3
  • mkfs.ext4 格式化成ext4文件系统
  • mkfs.jfs
  • mkfs.msdos
  • mkfs.vfat
  • mkfs.cramfs
  • mkfs.minix
  • mkfs.reiserfs
  • mkfs.xfs 新一代64位日志文件系统

格式化:

  • sudo mke2fs -t ext4 -L ‘mydisk’ /dev/sda2 是将/dev/sda2分区格式化为ext4的文件系统
  • mkfs -t ext3 /dev/sda3 //在硬盘分区“/dev/sda3”上创建ext3文件系统

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表