如何彻底删除kvm虚拟机_虚拟机命令怎么删除

如何彻底删除kvm虚拟机_虚拟机命令怎么删除(转)virsh命令速查表VirshVirshconnectVirshdisplaynodeinformation:VirshlistalldomainsListonlyactivedomainsVirshstartvmVirshautostartvmVirshautostartdisableVirshstopvm,virshshutd…


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

(转)virsh命令速查表

virsh commands cheatsheet 原文链接

Virsh

Virsh connect

The virsh connect [hostname-or-URI] [–readonly] command begins a local hypervisor session using virsh. After the first time you run this command it will run automatically each time the virsh shell runs. The hypervisor connection URI specifies how to connect to the hypervisor. The most commonly used URIs are:

qemu:///system – connects locally as the root user to the daemon supervising guest virtual machines on the KVM hypervisor. qemu:///session – connects locally as a user to the user’s set of guest local machines using the KVM hypervisor.

virsh connect qemu:///system 

Virsh display node information:

This displays the host node information and the machines that support the virtualization process.

virsh nodeinfo

Virsh list all domains

To list both inactive and active domains, use the command:

virsh list --all 

List only active domains

virsh list 

Virsh start vm

virsh start test 

Virsh autostart vm

To set a vm to start automatically on system startup, do:

virsh autostart test
virsh dominfo test
  • Keep an eye on the option Autostart: enable.

Virsh autostart disable

To disable autostart feature for a vm:

virsh autostart --disable test

Virsh stop vm, virsh shutdown vm

To shutdown a running vm gracefully use:

virsh shutdown test

Virsh force shutdown vm

You can do a forceful shutdown of active domain using the command:

virsh destroy test

Virsh stop all running vms

In case you would like to shutdown all running domains, just issue the command below:

for i in ` virsh list | grep running | awk '{print $2}'` do
     virsh shutdown $i
done

Virsh reboot vm

To restart a vm named test, the command used is:

virsh reboot test

Virsh remove vm

To cleanly remove a vm including its storage columes, use the commands shown below. The domain test should be replaced with the actual domain to be removed.

virsh destroy test 2> /dev/null
virsh undefine  test
virsh pool-refresh default
virsh vol-delete --pool default test.qcow2

In this example, storage volume is named /var/lib/libvirt/images/test.qcow2

Virsh create a vm

If you would like to create a new virtual machine with virsh, the relevant command to use is `virt-install. This is crucial and can’t miss on virsh commands cheatsheet arsenal. The example below will install a new operating system from CentOS 7 ISO Image.

 virt-install \
--name centos7 \
--description "Test VM with CentOS 7" \
--ram=1024 \
--vcpus=2 \
--os-type=Linux \
--os-variant=rhel7 \
--disk path=/var/lib/libvirt/images/centos7.qcow2,bus=virtio,size=10 \
--graphics none \
--location $HOME/iso/CentOS-7-x86_64-Everything-1611.iso \
--network bridge:virbr0  \
--console pty,target_type=serial -x 'console=ttyS0,115200n8 serial'

Virsh connect to vm console

To connect to the guest console, use the command:

virsh console test

This will return a fail message if an active console session exists for the provided domain.

Virsh edit vm xml file

To edit a vm xml file, use:

virsh edit test

Virsh suspend vm, virsh resume vm

To suspend a guest called testwith virsh command, run:

virsh suspend test
  • Domain test suspended

NOTE: When a domain is in a suspended state, it still consumes system RAM. Disk and network I/O will not occur while the guest is suspended.

Resuming a guest vm:

To restore a suspended guest with virsh using the resume option:

virsh resume test

Domain test resumed

Virsh save vm

To save the current state of a vm to a file using the virsh command :

The syntax is:

virsh save test test.saved

Domain test saved to test.save

$ ls -l test.save 
-rw------- 1 root root 328645215 Mar 18 01:35 test.saved

Restoring a saved vm

To restore saved vm from the file:

virsh restore test.save 

Domain restored from test.save

Virsh Manage Volumes

Virsh create volume

To create a 2GB volume named testvol2 on the default storage pool, use:

virsh vol-create-as default  test_vol2.qcow2  2G
du -sh /var/lib/libvirt/images/test_vol2.qcow2
  • default: Is the pool name.
  • testvol2: This is the name of the volume.
  • 2G: This is the storage capacity of the volume.

Virsh attach a volume to vm

To attach created volume above to vm test, run:

virsh attach-disk --domain test \
--source /var/lib/libvirt/images/test_vol2.qcow2  \
--persistent --target vdb
  • –persistent: Make live change persistent
  • –target vdb: Target of a disk device

Virsh detach volume on vm

To detach above volume testvol2 from the vm test:

virsh detach-disk --domain test --persistent --live --target vdb

resize disk

Please note that you can directly grow disk image for the vm using qemu-img command, this will look something like this:

qemu-img resize /var/lib/libvirt/images/test.qcow2 +1G
  • The main shortcoming of above command is that you cannot resize an image which has snapshots.

Virsh delete volume

To delete volume with virsh command, use:

virsh vol-delete test_vol2.qcow2  --pool default
virsh pool-refresh  default
virsh vol-list default

Virsh Manage Snapshots

In this second last section of managing kvm guest machines with virsh command, we’ll have a look at managing VM snapshots.

Virsh Create Snapshot for a vm

virsh snapshot-create-as --domain test \
--name "test_vm_snapshot1" \
--description "test vm snapshot 1-working"

Virsh list Snapshots for a vm

virsh snapshot-list test

Virsh display info about a snapshot

To retrieve more information about a domain, use:

virsh snapshot-info --domain test --snapshotname test_vm_snapshot1

Virsh revert vm snapshot

Here we’ll create another snapshot called testvmsnapshot2, then revert to snapshot testvmsnapshot1

virsh snapshot-create-as \
--domain test --name "test_vm_snapshot2" \
--description "test vm snapshot 2-working"

Domain snapshot testvmsnapshot2 created Let’s revert the snapshot we created before:

virsh snapshot-list test
virsh snapshot-revert --domain test  --snapshotname test_vm_snapshot1  --running

Virsh delete snapshot

virsh snapshot-delete --domain test --snapshotname  test_vm_snapshot2
virsh snapshot-delete --domain test --snapshotname  test_vm_snapshot1

Virsh clone a vm

virt-clone --connect qemu:///system \
--original test \
--name test_clone \
--file /var/lib/libvirt/images/test_clone.qcow2 

Virsh manage VM vcpus

This virsh commands cheatsheet section covers how to add additional virtual cpus to a virtual machine:

virsh setvcpus --domain test --maximum 2 --config
virsh setvcpus --domain test --count 2 --config
virsh reboot test

Virsh manage VM ram

  • 单位是 KB

To adjust the total ram used by the guest operating system, the following commands are used: Also on virsh commands cheatsheet is managing RAM with virsh.

virsh setmaxmem test 2048 --config
virsh setmem test 2048 --config
virsh reboot test
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/169813.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JVM进阶(十一):JAVA G1收集器

    JVM进阶(十一):JAVA G1收集器JVM进阶(十一)——JAVAG1收集器  在前两篇博文中讲解了新生代和年老代的收集器,在本篇博文中介绍一个收集范围涵盖整个堆的收集器——G1收集器。先讲讲G1收集器的特点,他也是个多线程的收集器,能够充分利用多个CPU进行工作,收集方式也与CMS收集器类似,因此不会有太久的停顿。  虽然回收的范围是整个堆,但还是有分代回收的回收方式。在年轻代依然采用复制算法;年老代也同样采用“标记-清除

    2022年6月13日
  • JPEG文件格式_显示文件格式后缀

    JPEG文件格式_显示文件格式后缀JPEG文件的存储格式有很多种,但最常用的是JFIF格式,即JPEG File Interchange Format。JPEG文件大体可以分为两个部分:(1)标记码;由

    2022年8月1日
  • LAMP学习路线图

    LAMP学习路线图

    2022年2月6日
  • Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?今天打开centos7,由于没设置开机自启,当我启动docker时,发现与docker有关的命令都会报错,百度过,说什么重装docker啥的,我都试过,但是还是解决不了问题,下面我贴出问题集:1.例如我查看版本信息2.重启容器3.systemctlstatusdocker查看docker状态也发现错误:最后解决问题方法是:进入/etc/docker,没有daemon.json文件就自己新建一个:cd/etc/docker编辑daemon.json文件:加入这段代码:

    2022年5月29日
  • 463个生活小窍门

    463个生活小窍门1、巧用牙膏6:若有小面积皮肤损伤或烧伤、烫伤,抹上少许牙膏,可立即止血止痛,也可防止感染,疗效颇佳。2、巧除纱窗油腻3:可将洗衣粉、吸烟剩下的烟头一起放在水里,待溶解后,拿来擦玻璃窗、纱窗,效果均不错。3、将虾仁放入碗内,加一点精盐、食用碱粉,用手抓搓一会儿后用清水浸泡,然后再用清水洗净,这样能使炒出的虾仁透明如水晶,爽嫩可口。4、和饺子面的窍门1:在1斤面粉里掺入6个蛋清,使面里蛋白质增加,包的饺子下锅后蛋白质会很快凝固收缩,饺子起锅后收水快,不易粘连5、将残茶叶浸入水中数天后,浇在植

    2022年6月23日
  • CSS calc()函数与单位vh 常见height:100vh[通俗易懂]

    CSS calc()函数与单位vh 常见height:100vh[通俗易懂]calc()函数属于CSS3版本内容,用于动态计算长度值。例如:width:calc(100%-10px);需要注意的是,运算符前后都需要保留一个空格。calc()函数支持”+”,”-“,”*”,”/”运算;举例,给一个div动态设置宽度如下:#divBox{position:absolute;left:50px;width:calc(100%-100p…

    2022年6月5日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号