virsh commands cheatsheet 原文链接
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
This displays the host node information and the machines that support the virtualization process.
virsh nodeinfo
To list both inactive and active domains, use the command:
virsh list --all
virsh list
virsh start test
To set a vm to start automatically on system startup, do:
virsh autostart test
virsh dominfo test
To disable autostart feature for a vm:
virsh autostart --disable test
To shutdown a running vm gracefully use:
virsh shutdown test
You can do a forceful shutdown of active domain using the command:
virsh destroy test
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
To restart a vm named test, the command used is:
virsh reboot test
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
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'
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.
To edit a vm xml file, use:
virsh edit test
To suspend a guest called testwith virsh command, run:
virsh suspend test
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.
To restore a suspended guest with virsh using the resume option:
virsh resume test
Domain test resumed
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
To restore saved vm from the file:
virsh restore test.save
Domain restored from test.save
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
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
To detach above volume testvol2 from the vm test:
virsh detach-disk --domain test --persistent --live --target vdb
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
To delete volume with virsh command, use:
virsh vol-delete test_vol2.qcow2 --pool default
virsh pool-refresh default
virsh vol-list default
In this second last section of managing kvm guest machines with virsh command, we’ll have a look at managing VM snapshots.
virsh snapshot-create-as --domain test \
--name "test_vm_snapshot1" \
--description "test vm snapshot 1-working"
virsh snapshot-list test
To retrieve more information about a domain, use:
virsh snapshot-info --domain test --snapshotname test_vm_snapshot1
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 snapshot-delete --domain test --snapshotname test_vm_snapshot2
virsh snapshot-delete --domain test --snapshotname test_vm_snapshot1
virt-clone --connect qemu:///system \
--original test \
--name test_clone \
--file /var/lib/libvirt/images/test_clone.qcow2
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
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