给Ubuntu Server扩容(Virtual Box 虚拟机) 2020-11-01 笔记,技巧 暂无评论 3466 次阅读 [TOC] 创建虚拟机时,虚拟硬盘必须选vdi格式的。不是vdi格式的就不用往下看了。 多个物理卷组成一个卷组,一个卷组分成多个逻辑卷。逻辑卷就是`/`根目录用的空间。 ## 总结 虚拟硬盘原来是10G,现在想扩容到20G。 ### 第一步:用VBoxManage.exe给虚拟硬盘扩容 VBoxManage.exe其实就是VirtualBox的“无界面”版,安装VirtualBox的时候自带的就有。 - VirtualBox软件的安装路径:D:\Common\VitrualBox\ - 虚拟机的虚拟硬盘路径:D:\VM\kubeedge\kubeedge.vdi - 扩容到20G,也就是 20480 M ``` D:\Common\VitrualBox\VBoxManage.exe modifymedium --resize 20480 "D:\VM\kubeedge\kubeedge.vdi" ``` ### 第二步:用fdisk,创建新分区 这里的分区(Partition)是物理上的分区,并不是分“C盘、D盘、E盘”那种逻辑上的分区。 首先用fdisk工具,对/dev/sda操作: ``` fdisk /dev/sda ``` 进入fdisk以后,按`n`,按`4`,敲两下回车,再按`w`保存退出,就把多出来的空间都变成`/dev/sda4`了。 如果按完n后,按的不是`4` 是 `5` ,那么创建的就是 `/dev/sda5` 。 ``` Command (m for help): n Partition number (4-128, default 4): 4 First sector (20969472-41943006, default 20969472): Last sector, +/-sectors or +/-size{K,M,G,T,P} (20969472-41943006, default 41943006): Created a new partition 4 of type 'Linux filesystem' and of size 10 GiB. Command (m for help): w The partition table has been altered. Syncing disks. ``` ### 第三步:用pvcreate,创建物理卷 创建物理卷(phisical volume)就相当于在分区上做个“这个分区是空闲的”的标记。 用pvcreate将新创建的分区 /dev/sda4 变成物理卷: ``` pvcreate /dev/sda4 ``` 运行结果: ``` root@kubeedge:~# pvcreate /dev/sda4 WARNING: ext3 signature detected on /dev/sda4 at offset 1080. Wipe it? [y/n]: y Wiping ext3 signature on /dev/sda4. Physical volume "/dev/sda4" successfully created. ``` 用`pvscan`看看物理卷的情况: ``` root@kubeedge:~# pvscan PV /dev/sda3 VG ubuntu-vg lvm2 [<9.00 GiB / 0 free] PV /dev/sda4 lvm2 [10.00 GiB] Total: 2 [<19.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 1 [10.00 GiB] ``` 可以看到,/dev/sda3 在卷组“ubuntu-vg”里,而 /dev/sda4 不在。。 ### 第四步:用vgextend,把新物理卷加到卷组 扩展可用空间,就是把新增的 /dev/sda4 加到卷组"ubuntu-vg"里。 ``` vgextend ubuntu-vg /dev/sda4 ``` 运行结果: ``` root@kubeedge:~# vgextend ubuntu-vg /dev/sda4 Volume group "ubuntu-vg" successfully extended ``` 查看卷组的命令是:`vgdisplay` ``` root@kubeedge:~# vgdisplay --- Volume group --- VG Name ubuntu-vg System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 18.99 GiB PE Size 4.00 MiB Total PE 4862 Alloc PE / Size 2303 / <9.00 GiB Free PE / Size 2559 / <10.00 GiB VG UUID vE0eg1-ArO6-fbfa-XruQ-e0bJ-efCn-27449F ``` ### 第五步:用lvextend,给逻辑卷扩容 用 `df -h` 看看磁盘名叫什么: ``` root@kubeedge:~# df -h Filesystem Size Used Avail Use% Mounted on udev 952M 0 952M 0% /dev tmpfs 199M 1.1M 198M 1% /run /dev/mapper/ubuntu--vg-ubuntu--lv 8.8G 7.6G 746M 92% / tmpfs 994M 0 994M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sda2 976M 103M 806M 12% /boot /dev/loop0 30M 30M 0 100% /snap/snapd/8542 /dev/loop1 72M 72M 0 100% /snap/lxd/16099 /dev/loop2 55M 55M 0 100% /snap/core18/1880 tmpfs 199M 0 199M 0% /run/user/0 ``` 最右侧为 `/` 的那一行,左边就是根目录的用的磁盘名,这里是 “/dev/mapper/ubuntu--vg-ubuntu--lv”,这就是下一个命令的最后一个参数 运行命令: ``` lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv ``` 运行结果: ``` root@kubeedge:~# lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv Size of logical volume ubuntu-vg/ubuntu-lv changed from <9.00 GiB (2303 extents) to 18.99 GiB (4862 extents). Logical volume ubuntu-vg/ubuntu-lv successfully resized. ``` ### 第六步: resize2fs,让逻辑卷的扩容生效 如果不用resize2fs的话,即使重启虚拟机,扩容也不会生效。 ``` resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv ``` 运行结果: ``` root@kubeedge:~# resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv resize2fs 1.45.5 (07-Jan-2020) Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 3 The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 4978688 (4k) blocks long. root@kubeedge:~# df -h Filesystem Size Used Avail Use% Mounted on udev 952M 0 952M 0% /dev tmpfs 199M 1.1M 198M 1% /run /dev/mapper/ubuntu--vg-ubuntu--lv 19G 7.8G 11G 44% / tmpfs 994M 0 994M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sda2 976M 103M 806M 12% /boot /dev/loop0 30M 30M 0 100% /snap/snapd/8542 /dev/loop1 72M 72M 0 100% /snap/lxd/16099 /dev/loop2 55M 55M 0 100% /snap/core18/1880 tmpfs 199M 0 199M 0% /run/user/0 ``` 到这就扩容完成了,可以看到已经变成19G了。 ## 走的弯路(不用看) 参考: 1. [Virtualbox最全扩展虚拟磁盘空间大小(vdi/vmdk)方法(Mac/Linux/Windows)](https://blog.csdn.net/tp7309/article/details/62473010) 2. [ubuntu 18.04 server 扩容(LVM)磁盘 解决磁盘不足的情况 (亲测)](https://blog.csdn.net/qq_14824885/article/details/82664635) 先把虚拟硬盘扩容到20G: ``` D:\Common\VitrualBox\VBoxManage.exe modifymedium --resize 20480 "D:\VM\kubeedge\kubeedge.vdi" ``` 再在系统里,给磁盘扩容。 查看是哪个磁盘满了:`df -h` ``` root@kubeedge:~# df -h Filesystem Size Used Avail Use% Mounted on udev 952M 0 952M 0% /dev tmpfs 199M 1.1M 198M 1% /run /dev/mapper/ubuntu--vg-ubuntu--lv 8.8G 7.6G 746M 92% / tmpfs 994M 0 994M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sda2 976M 103M 806M 12% /boot /dev/loop0 30M 30M 0 100% /snap/snapd/8542 /dev/loop1 72M 72M 0 100% /snap/lxd/16099 /dev/loop2 55M 55M 0 100% /snap/core18/1880 tmpfs 199M 0 199M 0% /run/user/0 ``` 发现是 /dev/mapper/ubuntu--vg-ubuntu--lv 满了。然后看所在的卷组:`vgdisplay` ``` root@kubeedge:~# vgdisplay --- Volume group --- VG Name ubuntu-vg System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 1 Act PV 1 VG Size <9.00 GiB PE Size 4.00 MiB Total PE 2303 Alloc PE / Size 2303 / <9.00 GiB Free PE / Size 0 / 0 VG UUID vE0eg1-ArO6-fbfa-XruQ-e0bJ-efCn-27449F ``` 按理说 Free PE / Size 应该显示10GiB,因为原先10G,我扩到了20G,应该有10G的剩余空间。 先不管,执行下一步指令试试:`lvextend -L 10G /dev/mapper/ubuntu--vg-ubuntu--lv ` ``` root@kubeedge:~# lvextend -L 10G /dev/mapper/ubuntu--vg-ubuntu--lv Insufficient free space: 257 extents needed, but only 0 available ``` 用“把全部剩余空间都扩给它”的方法试试:`lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv` ``` root@kubeedge:~# lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv New size (2303 extents) matches existing size (2303 extents). ``` 还是不行。 新方法:给新增的空间建立个分区:`fdisk /dev/sda` 按`n`,敲两下回车,再按`w`保存退出 ``` Command (m for help): n Partition number (4-128, default 4): 4 First sector (20969472-41943006, default 20969472): Last sector, +/-sectors or +/-size{K,M,G,T,P} (20969472-41943006, default 41943006): Created a new partition 4 of type 'Linux filesystem' and of size 10 GiB. Command (m for help): w The partition table has been altered. Syncing disks. ``` 这时候`df -h` 和`vgdisplay` 跟之前一点区别都没有。把新分区格式化一下试试: ``` root@kubeedge:~# mkfs.ext3 /dev/sda4 mke2fs 1.45.5 (07-Jan-2020) Creating filesystem with 2621440 4k blocks and 656640 inodes Filesystem UUID: 46b0529e-7971-443b-b05b-bf4862f955ac Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done ``` 这时候`df -h` 和`vgdisplay` 还是跟之前没区别。`reboot`重启一下试试。 ``` root@kubeedge:~# df -h Filesystem Size Used Avail Use% Mounted on udev 952M 0 952M 0% /dev tmpfs 199M 1.1M 198M 1% /run /dev/mapper/ubuntu--vg-ubuntu--lv 8.8G 7.7G 652M 93% / tmpfs 994M 0 994M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sda2 976M 103M 806M 12% /boot /dev/loop0 72M 72M 0 100% /snap/lxd/16099 /dev/loop2 55M 55M 0 100% /snap/core18/1880 /dev/loop1 30M 30M 0 100% /snap/snapd/8542 tmpfs 199M 0 199M 0% /run/user/0 root@kubeedge:~# vgdisplay --- Volume group --- VG Name ubuntu-vg System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 1 Act PV 1 VG Size <9.00 GiB PE Size 4.00 MiB Total PE 2303 Alloc PE / Size 2303 / <9.00 GiB Free PE / Size 0 / 0 VG UUID vE0eg1-ArO6-fbfa-XruQ-e0bJ-efCn-27449F root@kubeedge:~# ``` 还是没变,只有10G。。打算放弃了。。新建个虚拟机重新开始。 。。。刚才把dev/sda3删除,又把新建的/dev/sda4删除,重新建了一个/dev/sda3。重新建立/dev/sda3的时候,问我是不是保留签名,我选的Y。/dev/sda3确实扩容到19G了,kubectl之类的命令也还在。 ``` root@kubeedge:~# fdisk -l /dev/sda Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors Disk model: VBOX HARDDISK 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: 4E86040D-DF9F-4A14-ACE0-90B63DB3D3CC Device Start End Sectors Size Type /dev/sda1 2048 4095 2048 1M BIOS boot /dev/sda2 4096 2101247 2097152 1G Linux filesystem /dev/sda3 2101248 41943006 39841759 19G Linux filesystem root@kubeedge:~# ``` 但是`df -h`和`vgdisplay`还是跟之前没区别,可用空间还是只有8G 新方法,[百度经验](https://jingyan.baidu.com/article/73c3ce28b3124ca50243d946.html) 试试`pvscan` : ``` root@kubeedge:~# pvscan PV /dev/sda3 VG ubuntu-vg lvm2 [<9.00 GiB / 0 free] Total: 1 [<9.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 0 [0 ] root@kubeedge:~# ``` 没找到新分区。把/dev/sda3改回原来的,再创建/dev/sda4。 ``` root@kubeedge:~# fdisk /dev/sda Command (m for help): d Partition number (1-3, default 3): 3 Partition 3 has been deleted. Command (m for help): n Partition number (3-128, default 3): 3 First sector (2101248-41943006, default 2101248): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2101248-41943006, default 41943006): 20969471 Created a new partition 3 of type 'Linux filesystem' and of size 9 GiB. Partition #3 contains a LVM2_member signature. Do you want to remove the signature? [Y]es/[N]o: n Command (m for help): w The partition table has been altered. Syncing disks. root@kubeedge:~# pvscan PV /dev/sda3 VG ubuntu-vg lvm2 [<9.00 GiB / 0 free] Total: 1 [<9.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 0 [0 ] root@kubeedge:~# ``` 还是没变。`pvcreate /dev/sda4` ``` root@kubeedge:~# pvcreate /dev/sda4 WARNING: ext3 signature detected on /dev/sda4 at offset 1080. Wipe it? [y/n]: y Wiping ext3 signature on /dev/sda4. Physical volume "/dev/sda4" successfully created. root@kubeedge:~# pvscan PV /dev/sda3 VG ubuntu-vg lvm2 [<9.00 GiB / 0 free] PV /dev/sda4 lvm2 [10.00 GiB] Total: 2 [<19.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 1 [10.00 GiB] ``` 好像是可以了。 ``` root@kubeedge:~# vgextend ubuntu-vg /dev/sda4 Volume group "ubuntu-vg" successfully extended ``` 再试试`vgdisplay`: ``` root@kubeedge:~# vgdisplay --- Volume group --- VG Name ubuntu-vg System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 18.99 GiB PE Size 4.00 MiB Total PE 4862 Alloc PE / Size 2303 / <9.00 GiB Free PE / Size 2559 / <10.00 GiB VG UUID vE0eg1-ArO6-fbfa-XruQ-e0bJ-efCn-27449F ``` 终于有Free PE了!这样就可以用`lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv`来扩容了。 ``` root@kubeedge:~# lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv Size of logical volume ubuntu-vg/ubuntu-lv changed from <9.00 GiB (2303 extents) to 18.99 GiB (4862 extents). Logical volume ubuntu-vg/ubuntu-lv successfully resized. ``` 这时候用`df -h` ,可用空间还是8G。重启一下试试。 原来是少了一个命令:`resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv` ``` root@kubeedge:~# resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv resize2fs 1.45.5 (07-Jan-2020) Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 3 The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 4978688 (4k) blocks long. root@kubeedge:~# df -h Filesystem Size Used Avail Use% Mounted on udev 952M 0 952M 0% /dev tmpfs 199M 1.1M 198M 1% /run /dev/mapper/ubuntu--vg-ubuntu--lv 19G 7.8G 11G 44% / tmpfs 994M 0 994M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sda2 976M 103M 806M 12% /boot /dev/loop0 30M 30M 0 100% /snap/snapd/8542 /dev/loop1 72M 72M 0 100% /snap/lxd/16099 /dev/loop2 55M 55M 0 100% /snap/core18/1880 tmpfs 199M 0 199M 0% /run/user/0 ``` 标签: 服务器, Linux 本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭