创建虚拟机磁盘空间
创建存储池
- 执行如下命令,创建并配置存储池目录权限。
以创建/home/kvm/images目录为例。
mkdir -p /home/kvm/images
chown root:root /home/kvm/images
chmod 755 /home/kvm/images
- 执行以下命令,定义一个存储池并绑定目录。
virsh pool-define-as <name> --type <type> --target <path>
[root@localhost home]# virsh pool-define-as StoragePool --type dir --target /home/kvm/images Pool StoragePool defined
- 执行以下命令,建立一个存储池。virsh pool-build <pool>
[root@localhost home]# virsh pool-build StoragePool Pool StoragePool built
- 执行以下命令,激活存储池。virsh pool-start <pool>
[root@localhost home]# virsh pool-start StoragePool Pool StoragePool started
- 执行以下命令,设置存储池开机自启。
virsh pool-autostart <pool>
[root@localhost home]# virsh pool-autostart StoragePool Pool StoragePool marked as autostarted
- 执行如下命令,查看存储池列表。
virsh pool-list
[root@localhost home]# virsh pool-list Name State Autostart ----------------------------------- StoragePool active yes
- 执行如下命令,查看指定的存储池信息。
virsh pool-info <pool>
[root@localhost home]# virsh pool-info StoragePool Name: StoragePool UUID: 6b5221ea-26bf-4e02-823a-70c1e674bd9a State: running Persistent: yes Autostart: yes Capacity: 804.74 GiB Allocation: 52.00 KiB Available: 804.74 GiB
表1 参数说明 参数
说明
name
存储池名称。
type
存储池类型,此处为dir。
path
与步骤1中创建的目录一致。
pool
存储池名称。
创建虚拟机磁盘空间
以openEuler 22.03 LTS操作系统为例说明。
- 执行如下命令,创建卷。
virsh vol-create-as --pool <pool> --name <image.name> --capacity <capacity_size> --allocation <allocation_size> --format qcow2
例如,创建名称为“openeuler.img”,所在存储池为“StoragePool”,容量为“50G”,初始分配为“1G”,文件格式类型为“qcow2”的卷。其中磁盘空间的格式必须为“qcow2”格式。
virsh vol-create-as --pool StoragePool --name openeuler.img --capacity 50G --allocation 1G --format qcow2
[root@localhost home]# virsh vol-create-as --pool StoragePool --name openeuler.img --capacity 50G --allocation 1G --format qcow2 Vol openeuler.img created
- 执行如下命令,查看卷信息。
virsh vol-info <vol>
[root@localhost home]# virsh vol-info /home/kvm/images/openeuler.img Name: openeuler.img Type: file Capacity: 50.00 GiB Allocation: 196.00 KiB
表2 参数说明 参数
说明
pool
存储池名称,需要与创建存储池的名称一致。
image.name
卷名称,不可重复。
capacity_size
虚拟机可用的硬盘空间大小。
allocation_size
初始分配的硬盘空间大小。
vol
卷的路径。
Debian11.8/Debian10.13/veLinux1.2/Debian10.8创建虚拟机磁盘空间
- 在任意目录下(例如/home/test)执行如下命令,创建虚拟机磁盘空间。
qemu-img create -f raw debian.raw 100G
- 100G表示创建的磁盘空间大小。
- debian.raw表示创建的磁盘空间名称。
- 执行如下命令,查看当前目录下是否生成磁盘空间。
ls -l
显示如下,表示虚拟机磁盘空间创建成功。
total 3687304 -rwxrwxrwx 1 root root 113210052 May 23 21:42 Ascend-hdk-910b-npu-driver_24.1.rc2.b030_linux-aarch64.run -rwxr-xr-x 1 root root 10873 May 27 15:38 Debian_11_normal.xml -rwxrwxrwx 1 root root 10804 May 27 16:08 Debian_11.xml -rw-r--r-- 1 root root 198208 May 25 17:41 debian.qcow2 -rw-r--r-- 1 libvirt-qemu libvirt-qemu 107374182400 May 29 11:12 debian.raw -rw-r--r-- 1 root root 198208 May 28 11:52 debiantest.raw