对虚拟机进行磁盘扩容🐥

昨天傻逼了一下午,来记录下我从傻逼到醒悟到解决问题的过程

在给虚拟机装系统的时候,磁盘只有15G,昨天觉得少了,然后加到了30G,但是这只是增加了磁盘的容量,并没有影响到分区,系统的分区还是装系统的时候状态,只有15G。

这里提下,系统是Ubuntu 16.04 64位,使用LVM类型。

然后我做的第一件傻逼的事就是没对虚拟机做快照,就去修改分区.然后改错了,无法启动了……绝望了一段时间,还好后面修改回来了,所以第一个建议是,在修改分区之前,先做快照….这样改错了后还可以恢复回来….

然后傻逼的第二件事就是对分区理解错了,原本是有三个分区,一个/dev/sda1作为启动分区,挂载/boot,没啥问题,第二个是扩展分区/dev/sda2,有15.2G,然后第三个是/dev/sda5,为逻辑分区,也是有15.2G,最开始,我以为是sda2+sda5一共30G左右,然后踩坑了,使用fdisk /dev/sda进行分区,把sda2删除,然后新建一个主分区sda2,然后sda5就没有了,就剩一个30G的sda2

我一开始理解成是原本的sda2+sda5合在一起,形成了一个有30G的sda2,现在想想,我当时好傻逼啊…..

因为好久没弄这些了,知识点都忘光了,一个硬盘是有自己寻址模式的,起始有一块地方专门记载分区的地址,而这块地方只能记载4个地址,而在这个地方指向的分区都叫做主分区,但是后来,有些人说4个不够用啊,所以又出现了扩展分区和逻辑分区,也就是说,可以把写主分区地址的地方写上扩展分区的地址,而扩展分区的开头和硬盘类似,有一块放地址的空间,指向逻辑分区。

所以说,扩展分区实际上是不拿来用,是用来放逻辑分区地址的,或者可以想象成扩展分区是在一块磁盘上虚拟出的一块虚拟硬盘。

SATA硬盘是可以有11个逻辑分区的,从sda5-sda15

所以开始看到的分区状态,磁盘只分了15G的内存出来给了逻辑分区,还要15G是没有被分区的,当把扩展分区删除了后,逻辑分区自然也没了,磁盘剩余空间就变30G了,所以分个主分区有30G。

好了,概念讲完后,说说我最后是怎么解决的吧。

因为写blog之前我已经成功扩容了,所以我现在分区已经有30G了,我现在把它扩容到40G,首先是我虚拟机现在的状态:

首先是分区情况:

1
2
3
4
5
6
7
8
9
# fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
.....
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 62890625 61889156 29.5G 5 Extended
/dev/sda5 1001472 33552383 32550912 15.5G 8e Linux LVM
/dev/sda6 33554432 62890625 29336194 14G 8e Linux LVM
....

然后是挂载情况:

1
2
3
4
# df -h
...
/dev/mapper/ubuntu--vg-root 28G 3.3G 24G 13% /
...

因为使用的是LVM,所以挂载的不是/dev/sdax到根目录,而是/dev/mapper/ubuntu--vg-root

其中ubuntu--vg称为volumn group,然后ubuntu--ng-root称为logic volumn,可以这么想,group相当于一块硬盘,logic volumn是这块硬的分区。

比如还有一个logic volumnubuntu--ng-swap,作为交换分区。

现在我们先设置虚拟机,把硬盘加到40G,然后重启虚拟机查看下:

1
2
3
4
5
6
7
8
# fdisk -l
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
.....
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 62890625 61889156 29.5G 5 Extended
/dev/sda5 1001472 33552383 32550912 15.5G 8e Linux LVM
/dev/sda6 33554432 62890625 29336194 14G 8e Linux LVM

首先对扩展分区进行扩容,使用的命令是parted

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
# parted
root@ubuntu:~# parted
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
align-check TYPE N check partition N for TYPE(min|opt) alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 512MB 511MB primary ext2 boot
2 513MB 32.2GB 31.7GB extended
5 513MB 17.2GB 16.7GB logical lvm
6 17.2GB 32.2GB 15.0GB logical lvm
(parted) resizepart 2 42.9G
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 512MB 511MB primary ext2 boot
2 513MB 42.9GB 42.4GB extended
5 513MB 17.2GB 16.7GB logical lvm
6 17.2GB 32.2GB 15.0GB logical lvm
(parted) quit
Information: You may need to update /etc/fstab.

# fdisk -l
....
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 83789062 82787593 39.5G 5 Extended
/dev/sda5 1001472 33552383 32550912 15.5G 8e Linux LVM
/dev/sda6 33554432 62890625 29336194 14G 8e Linux LVM
....

然后可以从扩展分区中再分出一个逻辑分区,因为sda6是我之前分出来的,还没有啥文件,所以所准备试试扩展下sda6分区,所以,首先是删除sda6分区,然后再新建:

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
# 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): d
Partition number (1,2,5,6, default 6): 6

Partition 6 has been deleted.

Command (m for help): n
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l

Adding logical partition 6
First sector (33554432-83789062, default 33554432):
Last sector, +sectors or +size{K,M,G,T,P} (33554432-83789062, default 83789062):

Created a new partition 6 of type 'Linux' and of size 24 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 -l
.....
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 83789062 82787593 39.5G 5 Extended
/dev/sda5 1001472 33552383 32550912 15.5G 8e Linux LVM
/dev/sda6 33554432 83789062 50234631 24G 83 Linux
.....

然后就是扩展卷组了

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
# pvs
PV VG Fmt Attr PSize PFree
/dev/sda5 ubuntu-vg lvm2 a-- 15.52g 0
/dev/sda6 ubuntu-vg lvm2 a-- 13.98g 0
# pvresize /dev/sda6
Failed to find device for physical volume "+10G".
Physical volume "/dev/sda6" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
# pvs
PV VG Fmt Attr PSize PFree
/dev/sda5 ubuntu-vg lvm2 a-- 15.52g 0
/dev/sda6 ubuntu-vg lvm2 a-- 23.95g 9.96g
# vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 2 2 0 wz--n- 39.47g 9.96g
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root ubuntu-vg -wi-ao---- 28.50g
swap_1 ubuntu-vg -wi-ao---- 1.00g
# lvresize -l +100%FREE /dev/mapper/ubuntu--vg-root
Size of logical volume ubuntu-vg/root changed from 28.50 GiB (7297 extents) to 38.47 GiB (9848 extents).
Logical volume root successfully resized.
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root ubuntu-vg -wi-ao---- 38.47g
swap_1 ubuntu-vg -wi-ao---- 1.00g
# df -h
Filesystem Size Used Avail Use% Mounted on
......
/dev/mapper/ubuntu--vg-root 28G 3.3G 24G 13% /
......
# resize2fs -p /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 = 2, new_desc_blocks = 3
The filesystem on /dev/mapper/ubuntu--vg-root is now 10084352 (4k) blocks long.

# df -h
Filesystem Size Used Avail Use% Mounted on
......
/dev/mapper/ubuntu--vg-root 38G 3.3G 33G 9% /
......
# reboot

好了,现在,根目录的容量扩展好了

对虚拟机进行磁盘扩容🐥

https://nobb.site/2017/07/12/0x34/

Author

Hcamael

Posted on

2017-07-12

Updated on

2024-08-29

Licensed under