创建虚拟机硬盘文件

创建存储池

  1. 执行如下命令,创建并配置存储池目录权限。

    以创建/home/kvm/images目录为例。

    mkdir -p /home/kvm/images

    chown root:root /home/kvm/images

    chmod 755 /home/kvm/images

  2. 执行以下命令,定义一个存储池并绑定目录。

    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

  3. 执行以下命令,建立一个存储池。

    virsh pool-build <pool>
    [root@localhost home]# virsh pool-build StoragePool
    Pool StoragePool built

  4. 执行以下命令,激活存储池。

    virsh pool-start <pool>
    [root@localhost home]# virsh pool-start StoragePool
    Pool StoragePool started

  5. 执行以下命令,设置存储池开机自启。

    virsh pool-autostart <pool>

    [root@localhost home]# virsh pool-autostart StoragePool
    Pool StoragePool marked as autostarted

  6. 执行如下命令,查看存储池列表。

    virsh pool-list

    [root@localhost home]# virsh pool-list
     Name          State    Autostart
    -----------------------------------
     StoragePool   active   yes

  7. 执行如下命令,查看指定的存储池信息。

    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操作系统为例说明。

  1. 执行如下命令,创建卷。

    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

  2. 执行如下命令,查看卷信息。

    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

    卷的路径。