[KVM] 常用命令

- 打开虚拟机图形化管理页面

- 查看虚拟机

- 删除虚拟机

- 创建volume

- 创建pool

- 修改volume名称(已测试、非正式)

@打开虚拟机图形化管理页面

virt-manager

------------------------
以下操作在命令行
------------------------

virsh xxx
create          create a domain from an XML file
define          define (but don't start) a domain from an XML file
destroy        destroy (stop) a domain
undefine      undefine a domain
reboot          reboot a domain
shutdown     gracefully shutdown a domain

@查看虚拟机

virsh list

[root@xxx ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 15    TEST0                          running
 16    TEST1                          running

[root@xxx ~]#

@删除虚拟机

virsh destroy    强制停止虚拟机

virsh undefine   删除虚拟机

updatedb

locate

[root@xxx ~]# virsh destroy TEST1
Domain TEST1 destroyed
[root@xxx ~]# virsh undefine TEST1
Domain TEST1 has been undefined
[root@xxx ~]# updatedb
[root@xxx ~]#
[root@xxx ~]# locate TEST1
/var/log/libvirt/qemu/TEST1.log
[root@xxx ~]# vim /var/log/libvirt/qemu/TEST1.log
[root@xxx ~]# rm -rf /var/log/libvirt/qemu/TEST1.log
[root@xxx ~]#

@创建volume

virsh vol-create-as .qcow2 --format qcow2

eg: virsh vol-create-as default test.qcow2 8G --format qcow2 

说明:volume会保存在default pool,即 /var/lib/libvirt/images,如果该路径挂载的磁盘空间不能满足要求,需要在其它挂载点的路径保存volume,得创建其它pool

@创建pool

【例】创建名为extra_images的pool,volume存放于/opt/data/kvm/

virsh pool-define-as extra_images dir - - - - "/opt/data/kvm/"
virsh pool-build extra_images
virsh pool-start extra_images
virsh pool-info extrat_images

# virsh pool-list
 Name                 State      Autostart
-------------------------------------------
 default              active     yes
 tmp                  active     yes

# virsh pool-info tmp
Name:           tmp
UUID:           5c6fe468-f0de-453d-a451-b92a31145658
State:          running
Persistent:     yes
Autostart:      yes
Capacity:       493.98 GiB
Allocation:     454.59 GiB
Available:      39.39 GiB

如果 Autostart: no,执行 virsh pool-autostart  可以让它自启动

附:pool相关

 Storage Pool (help keyword 'pool')
    find-storage-pool-sources-as   find potential storage pool sources
    find-storage-pool-sources      discover potential storage pool sources
    pool-autostart                 autostart a pool
    pool-build                     build a pool
    pool-create-as                 create a pool from a set of args
    pool-create                    create a pool from an XML file
    pool-define-as                 define a pool from a set of args
    pool-define                    define an inactive persistent storage pool or modify an existing persistent one from an XML file
    pool-delete                    delete a pool
    pool-destroy                   destroy (stop) a pool
    pool-dumpxml                   pool information in XML
    pool-edit                      edit XML configuration for a storage pool
    pool-info                      storage pool information
    pool-list                      list pools
    pool-name                      convert a pool UUID to pool name
    pool-refresh                   refresh a pool
    pool-start                     start a (previously defined) inactive pool
    pool-undefine                  undefine an inactive pool
    pool-uuid                      convert a pool name to pool UUID
    pool-event                     Storage Pool Events

@修改volume名称(已测试、非正式)

说明:没有找到修改volume的资料,在已拉起虚拟机的情况下,尝试了以下步骤可行

(1)将虚拟机关机,更新其xml文件中的volume名称

vim /etc/libvirt/qemu/.xml   更新source的内容

virsh define 

# cd /etc/libvirt/qemu
# ll
total 16
drwxr-xr-x  2 root root   39 Mar 31 11:05 autostart
-rw-------  1 root root 4687 Mar 30 18:46 TEST0.xml
drwx------. 3 root root   42 Mar 20  2021 networks
-rw-------  1 root root 4693 Mar 31 11:05 TEST1.xml

# vim TEST0.xml          ->  更新类似这行 
# virsh define TEST0.xml
Domain TEST0 defined from TEST0.xml

(2)使用 virsh vol-clone 复制一个volume,名称和步骤(1)中的一致,然后将原volume删除

# virsh vol-list
error: command 'vol-list' requires  option
# virsh vol-list extra_images
 Name                 Path
------------------------------------------------------------------------------
 RHEL76               /opt/data/kvm/RHEL76
 RHEL84.qcow2         /opt/data/kvm/RHEL84.qcow2

# virsh vol-info /opt/data/kvm/RHEL76
Name:           RHEL76
Type:           file
Capacity:       1.50 TiB
Allocation:     4.51 GiB

# virsh vol-clone
error: command 'vol-clone' requires  option
error: command 'vol-clone' requires  option
# virsh vol-clone RHEL76 RHEL76.qcow2
error: failed to get vol 'RHEL76', specifying --pool might help
error: Storage volume not found: no storage vol with matching path 'RHEL76'

# virsh vol-clone /opt/data/kvm/RHEL76 RHEL76.qcow2
Vol RHEL76.qcow2 cloned from RHEL76

# virsh vol-list extra_images
 Name                 Path
------------------------------------------------------------------------------
 RHEL76.qcow2         /opt/data/kvm/RHEL76.qcow2
 RHEL76               /opt/data/kvm/RHEL76
 RHEL84.qcow2         /opt/data/kvm/RHEL84.qcow2

# virsh vol-info /opt/data/kvm/RHEL76.qcow2
Name:           RHEL76.qcow2
Type:           file
Capacity:       1.50 TiB
Allocation:     6.00 GiB

# ll
total 8760560
-rw------- 1 root root 4839702528 Mar 31 11:25 RHEL76
-rw------- 1 root root 4129292288 Mar 31 11:45 RHEL76.qcow2
# virsh vol-delete /opt/data/kvm/RHEL76
Vol /opt/data/kvm/RHEL76 deleted

附:volume相关

# virsh --help
...
 Storage Volume (help keyword 'volume')
    vol-clone                      clone a volume.
    vol-create-as                  create a volume from a set of args
    vol-create                     create a vol from an XML file
    vol-create-from                create a vol, using another volume as input
    vol-delete                     delete a vol
    vol-download                   download volume contents to a file
    vol-dumpxml                    vol information in XML
    vol-info                       storage vol information
    vol-key                        returns the volume key for a given volume name or path
    vol-list                       list vols
    vol-name                       returns the volume name for a given volume key or path
    vol-path                       returns the volume path for a given volume name or key
    vol-pool                       returns the storage pool for a given volume key or path
    vol-resize                     resize a vol
    vol-upload                     upload file contents to a volume
    vol-wipe                       wipe a vol


 

你可能感兴趣的:(KVM,运维)