现有的虚机环境硬盘空间不足,频频因为硬盘空间不足而导致失败,比如IDE报磁盘空间不足,下载代码失败等等,重新创建虚机还需要把现有的工作环境迁移过去,也比较麻烦,就采取了扩展硬盘空间的方式,把扩展迁移过程整理记录下来。
1. 给虚机增加新硬盘
可以通过命令行操作,此处是通过virt-manager界面操作的,更加直观简便。
在虚拟机关闭状态下,打开,增加硬盘:
添加成功后如下,就可以启动虚拟机了:
2. 挂载硬盘
首先要对硬盘进行分区格式化,然后挂载。
首先确认硬盘已经加上了
[root@builder xxxx]# fdisk -l
Disk /dev/vda: 21.5 GB, 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
Disk label type: dos
Disk identifier: 0x000ba7f5
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 6293503 2097152 82 Linux swap / Solaris
/dev/vda3 6293504 41943039 17824768 83 Linux
Disk /dev/vdb: 32.2 GB, 32212254720 bytes, 62914560 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: 0x280b107d
此处的/dev/vdb就是新加的硬盘。接下来进行分区格式化:
[root@builder xxxx]# fdisk /dev/vdb
Command (m for help): p
Disk /dev/vdb: 32.2 GB, 32212254720 bytes, 62914560 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: 0x280b107d
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-314572800, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-314572800, default 314572800):
Using default value 314572800
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
一路采用默认值即可,完成后把分区信息写入分区表,最后成功后如下 :
[root@builder xxxx]# fdisk -l
Disk /dev/vda: 21.5 GB, 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
Disk label type: dos
Disk identifier: 0x000ba7f5
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 6293503 2097152 82 Linux swap / Solaris
/dev/vda3 6293504 41943039 17824768 83 Linux
Disk /dev/vdb: 32.2 GB, 32212254720 bytes, 62914560 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: 0x280b107d
Device Boot Start End Blocks Id System
/dev/vdb1 2048 62914559 31456256 83 Linux
接下来挂载硬盘:
[root@builder xxxx]# mkdir workspace
[root@builder xxxx]# mount /dev/vdb1 /workspace
[root@builder xxxx]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda3 17G 16G 1.5G 92% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 8.4M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/vdb1 30G 45M 28G 1% /workspace
/dev/vda1 1014M 158M 857M 16% /boot
tmpfs 783M 0 783M 0% /run/user/1000
为了在虚机重启后硬盘自动挂载,还需要在/etc/fstab中增加如下记录:
UUID=ec794942-802d-4a17-9ba1-2fc60abc3ed4 /workspace ext4 defaults 0 2
其中UUID是新加硬盘的标识,可以通过如下命令获取:
[root@builder xxxx]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sr0
vda
├─vda1 xfs 6f5d5a74-587c-4923-b784-89882816aee7 /boot
├─vda2 swap ddaa321f-d418-4afd-b1f5-763426f62fd9 [SWAP]
└─vda3 xfs a9090e7f-1e26-4a79-9266-28832c0f5d1f /
vdb
└─vdb1 ext4 ec794942-802d-4a17-9ba1-2fc60abc3ed4 /workspace
3. 工作环境迁移
原来的工作内容都放在/home目录下,为了平滑迁移,最好保持目录不变,尝试把home目录改名,然后把挂载点直接创建成/home,这样直接把内容拷贝过去就可以直接工作了。但是home改名失败了:
[root@builder /]# mv home home1
mv: cannot move ‘home’ to ‘home1’: Device or resource busy
网上查了一下貌似因为home和home1在同一个分区不能改名,只好曲线救国:
[root@builder xxxx]# mv yyy /workspace/yyy
[root@builder xxxx]# ln -s /workspace/yyy ./yyy
还有权限问题
[root@builder xxxx]# chown zzz:zzz /workspace