Linux虚拟机划分磁盘,【运维笔记】vSphere为已运行的Linux虚拟机扩展磁盘容量

准备工作

分配一台虚拟机,本次操作分配的资源是:1核/1GRAM/40G的虚拟机

安装系统,该机器安装的是Ubuntu Server 16 x64 版本

查看当前虚拟机磁盘空间

root@ubuntu:~# df -h

Filesystem Size Used Avail Use% Mounted on

udev 468M 0 468M 0% /dev

tmpfs 98M 4.6M 93M 5% /run

/dev/mapper/ubuntu--vg-root 38G 2.0G 34G 6% /

tmpfs 488M 0 488M 0% /dev/shm

tmpfs 5.0M 0 5.0M 0% /run/lock

tmpfs 488M 0 488M 0% /sys/fs/cgroup

/dev/sda1 472M 58M 391M 13% /boot

tmpfs 98M 0 98M 0% /run/user/0

本次的目标主要是扩展/dev/mapper/ubuntu--vg-root的容量,我们将其从38G扩展到60G。

向虚拟机分配新的磁盘空间

这个地方有两种操作方式:

直接编辑当前虚拟机磁盘大小

新增一块磁盘

直接编辑磁盘大小

关机,然后编辑磁盘大小

dafaf409e9b84ec18487dc4ac3ca57b7-1.png

ca6c61aa6a2746a682c9e55d0ef2dc00-2.png

开启虚拟机,然后查看磁盘 root@ubuntu:~# df -h

Filesystem Size Used Avail Use% Mounted on

udev 468M 0 468M 0% /dev

tmpfs 98M 4.6M 93M 5% /run

/dev/mapper/ubuntu--vg-root 38G 2.0G 34G 6% /

tmpfs 488M 0 488M 0% /dev/shm

tmpfs 5.0M 0 5.0M 0% /run/lock

tmpfs 488M 0 488M 0% /sys/fs/cgroup

/dev/sda1 472M 58M 391M 13% /boot

tmpfs 98M 0 98M 0% /run/user/0

发现跟最初查看时并没有什么变化。这是因为我们上面加入的磁盘只是扩展了空间,并没有挂载上去。

但是我们可以通过以下命令查看,虚拟机实际拥有的磁盘空间:

root@ubuntu:~# fdisk -l

Disk /dev/ram0: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram1: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram2: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram3: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram4: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram5: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram6: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram7: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram8: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram9: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram10: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram11: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram12: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram13: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram14: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/ram15: 64 MiB, 67108864 bytes, 131072 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 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: 0x183be081

Device Boot Start End Sectors Size Id Type

/dev/sda1 * 2048 999423 997376 487M 83 Linux

/dev/sda2 1001470 83884031 82882562 39.5G 5 Extended

/dev/sda5 1001472 83884031 82882560 39.5G 8e Linux LVM

Disk /dev/mapper/ubuntu--vg-root: 38.5 GiB, 41326477312 bytes, 80715776 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

Disk /dev/mapper/ubuntu--vg-swap_1: 1020 MiB, 1069547520 bytes, 2088960 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

说明:

Disk /dev/ram0 到 Disk /dev/ram15 为内存空间,由于我们的物理上有16块内存条,当前虚拟机分配的是1G内存,这16个内存空间分别来自16块内存条。

Disk /dev/sda 表示虚拟机实际拥有的磁盘空间。

接下来的Device列表展示的是系统分区,一个主分区,一个扩展分区,我们需要扩展的是id为5的扩展分区。

接下来两个Disk展示的是root分区和交换分区的相关信息。

为了扩展/dev/mapper/ubuntu--vg-root的空间,我们需要对LVM进行操作。

添加新的磁盘

feb2efbb2ea149e58b32d70a10241499-3.png

991a94de26ed496b816db3b2c5a9583c-4.png

7e352ad4f33640df91a016a2d33c56d9-5.png

61e7316a8a7941698bf5ef03a3ffeee4-6.png

38787f08f7084850a341673fe69615ca-7.png

bd94553b6d504c40a8c637a685196572-8.png

等待添加完成重启,然后使用fdisk -l命令查看,既可看到一个新的硬盘,可能名称为:/dev/sdb。

root@ubuntu:~# fdisk -l

...

Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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

...

LVM 扩容

使用fdisk命令划分新的空间

root@ubuntu:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.1).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

Command (m for help): n

Partition type

p primary (2 primary, 1 extended, 1 free)

l logical (numbered from 5)

Select (default p): p

Selected partition 4

First sector (83884032-125829119, default 83884032): 83884032

Last sector, +sectors or +size{K,M,G,T,P} (83884032-125829119, default 125829119): 125829119

Created a new partition 4 of type 'Linux' and of size 20 GiB.

Command (m for help): w

The partition table has been altered.

Calling ioctl() to re-read partition table.

Re-reading the partition table failed.: 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).

说明:

fdisk /dev/sda中的/dev/sda是物理磁盘名称,如果是新增的一块磁盘,那么就需要使用新的名称,比如:/dev/sdb

在出现上述结果之前,我还进行了一次同样的操作。本次操作没有让输入分区编号,上一次的时候手动输入了默认编号3。

使用fdisk -l查看新创建的分区

root@ubuntu:~# fdisk -l

...

Device Boot Start End Sectors Size Id Type

/dev/sda1 * 2048 999423 997376 487M 83 Linux

/dev/sda2 1001470 83884031 82882562 39.5G 5 Extended

/dev/sda3 999424 1001469 2046 1023K 83 Linux

/dev/sda4 83884032 125829119 41945088 20G 83 Linux

/dev/sda5 1001472 83884031 82882560 39.5G 8e Linux LVM

...

其中/dev/sda3和/dev/sda4为新建的分区。

重启服务。

创建物理卷

root@ubuntu:~# pvcreate /dev/sda4

Physical volume "/dev/sda4" successfully created

查看原VG情况

root@ubuntu:~# vgdisplay

--- Volume group ---

VG Name ubuntu-vg

System ID

Format lvm2

Metadata Areas 1

Metadata Sequence No 3

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 2

Open LV 2

Max PV 0

Cur PV 1

Act PV 1

VG Size 39.52 GiB

PE Size 4.00 MiB

Total PE 10117

Alloc PE / Size 10108 / 39.48 GiB

Free PE / Size 9 / 36.00 MiB

VG UUID EnOwTF-LaTQ-6JlW-efYJ-HtXv-dUEt-sKsKi9

扩展VG,将新的物理卷加入VG

root@ubuntu:~# vgextend ubuntu-vg /dev/sda4

Volume group "ubuntu-vg" successfully extended

这里面的ubuntu-vg就是在vgdisplay命令中展示的VG Name。

此时再查看VG Size会发现已经增加了。

将新的VG空间加入到指定LV中

上面我们已经说过,我们需要扩展的是/dev/mapper/ubuntu--vg-root。所以,命令如下:

root@ubuntu:~# lvextend -L +20G /dev/mapper/ubuntu--vg-root

Size of logical volume ubuntu-vg/root changed from 38.49 GiB (9853 extents) to 58.49 GiB (14973 extents).

Logical volume root successfully resized.

重新定义分区大小

root@ubuntu:~# resize2fs /dev/mapper/ubuntu--vg-root

resize2fs 1.42.13 (17-May-2015)

Filesystem at /dev/mapper/ubuntu--vg-root is mounted on /; on-line resizing required

old_desc_blocks = 3, new_desc_blocks = 4

The filesystem on /dev/mapper/ubuntu--vg-root is now 15332352 (4k) blocks long.

再次查看磁盘使用量

root@ubuntu:~# df -h

Filesystem Size Used Avail Use% Mounted on

udev 468M 0 468M 0% /dev

tmpfs 98M 4.6M 93M 5% /run

/dev/mapper/ubuntu--vg-root 58G 2.0G 53G 4% /

tmpfs 488M 0 488M 0% /dev/shm

tmpfs 5.0M 0 5.0M 0% /run/lock

tmpfs 488M 0 488M 0% /sys/fs/cgroup

/dev/sda1 472M 58M 391M 13% /boot

tmpfs 98M 0 98M 0% /run/user/0

到此我们已经顺利的将/dev/mapper/ubuntu--vg-root扩展了20G。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值