0%

VirtualBox下Linux虚机扩容

使用vagrant搭建开发环境,部分box默认的空间太小,需要扩容
非专业人员,只是百度出的结果,亲测有效

虚机硬盘扩容

1
2
VBoxManage clonehd box_disk1.vmdk  box_disk1.vdi --format VDI
VBoxManage modifyhd box_disk1.vdi --resize 60000

vmdk格式磁盘不支持扩容, 要先将vmdk转换为vdi

修改磁盘配置

设置->存储->控制器,使用box_disk1.vdi替换掉box_disk1.vmdk

查看当前操作系统的空间情况

1
2
3
4
5
6
7
8
9
10
[root@localhost qqjy]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 6.7G 2.1G 4.7G 31% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 6.6M 490M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 497M 122M 375M 25% /boot
vagrant 406G 61G 345G 16% /vagrant
tmpfs 100M 0 100M 0% /run/user/0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost qqjy]# fdisk -l

Disk /dev/sda: 62.9 GB, 62914560000 bytes, 122880000 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 label type: dos
Disk identifier: 0x00068d77

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 16777215 7875584 8e Linux LVM

Disk /dev/mapper/centos-root: 7159 MB, 7159676928 bytes, 13983744 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/centos-swap: 859 MB, 859832320 bytes, 1679360 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

扩容后的磁盘约60G,实际使用8G,存在未分配的空间
现打算将/扩容到55G左右

/dev/sda上未分配的磁盘空间分区

fdisk /dev/sda->n->p->3->w

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
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

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, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (16777216-122879999, default 16777216):
Using default value 16777216
Last sector, +sectors or +size{K,M,G} (16777216-122879999, default 122879999):
Using default value 122879999
Partition 3 of type Linux and of size 50.6 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 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)
Syncing disks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost ~]# fdisk -l

Disk /dev/sda: 62.9 GB, 62914560000 bytes, 122880000 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 label type: dos
Disk identifier: 0x00068d77

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 16777215 7875584 8e Linux LVM
/dev/sda3 16777216 122879999 53051392 83 Linux

Disk /dev/mapper/centos-root: 7159 MB, 7159676928 bytes, 13983744 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/centos-swap: 859 MB, 859832320 bytes, 1679360 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

sda3已经创建成功

重启

1
reboot

将新建的分区格式化,建立文件系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@localhost ~]# mkfs.ext4 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3317760 inodes, 13262848 blocks
663142 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2162163712
405 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

创建物理卷

1
2
3
4
[root@localhost ~]# pvcreate /dev/sda3
WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sda3.
Physical volume "/dev/sda3" successfully created
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size 7.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1922
Free PE 10
Allocated PE 1912
PV UUID v3Ulr1-lAnh-XdSy-4oGg-CQmE-4aWZ-1Zaxx6

"/dev/sda3" is a new physical volume of "50.59 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 50.59 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID UYFro7-8ttA-3eQ9-yZfY-fr3V-d3St-DvM06V

查看卷组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
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 7.51 GiB
PE Size 4.00 MiB
Total PE 1922
Alloc PE / Size 1912 / 7.47 GiB
Free PE / Size 10 / 40.00 MiB
VG UUID oN3432-gfKs-TOYw-n8Jy-fu6j-ZR0i-xg1i6y

目前存在一个7.5G的卷组

扩展卷组

1
2
[root@localhost ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended

查看逻辑卷

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
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID fy3cy2-d2Pd-eNQ8-DWXO-DaT0-3o6v-Ee2SKq
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-08-26 13:30:06 +0800
LV Status available
# open 2
LV Size 820.00 MiB
Current LE 205
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID wdrHLD-l66S-jkAF-2fE4-I8NC-liQ9-mEAXKH
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-08-26 13:30:07 +0800
LV Status available
# open 1
LV Size 6.67 GiB
Current LE 1707
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 6.7G 2.1G 4.7G 31% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 6.6M 490M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 497M 122M 375M 25% /boot
vagrant 406G 61G 345G 16% /vagrant
tmpfs 100M 0 100M 0% /run/user/0

扩展逻辑卷

1
2
3
[root@localhost ~]# lvextend -L 55G -n /dev/mapper/centos-root
Size of logical volume centos/root changed from 6.67 GiB (1707 extents) to 55.00 GiB (14080 extents).
Logical volume root successfully resized.
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 6.7G 2.1G 4.7G 31% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 6.6M 490M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 497M 122M 375M 25% /boot
vagrant 406G 61G 345G 16% /vagrant
tmpfs 100M 0 100M 0% /run/user/0

调整根逻辑卷大小

1
2
3
4
[root@localhost ~]# resize2fs /dev/mapper/centos-root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
Couldn't find valid filesystem superblock.

这个命令是错误的,使用下面的命令

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=436992 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=1747968, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 1747968 to 14417920
1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 55G 2.1G 53G 4% /
devtmpfs 739M 0 739M 0% /dev
tmpfs 749M 0 749M 0% /dev/shm
tmpfs 749M 8.4M 741M 2% /run
tmpfs 749M 0 749M 0% /sys/fs/cgroup
/dev/sda1 497M 122M 375M 25% /boot
vagrant 406G 63G 344G 16% /vagrant
data_server 406G 63G 344G 16% /data/server
tmpfs 150M 0 150M 0% /run/user/0

现在已经是55G的空间了

参考文档

  1. [http://blog.csdn.net/wzy0623/article/details/50667442]
  2. [http://blog.csdn.net/a349458532/article/details/52117618]
  3. [https://www.cnblogs.com/chenmh/p/5096592.html]
  4. [https://www.linuxidc.com/Linux/2017-06/144722.htm]
  5. [https://www.cnblogs.com/pinganzi/p/6478255.html]
  6. [http://blog.csdn.net/weiguang1017/article/details/52252448]