| Next revision | Previous revision |
| tanked.information:vm_command_list [2021/12/13 11:41] – external edit 127.0.0.1 | tanked.information:vm_command_list [2024/12/01 08:40] (current) – external edit 127.0.0.1 |
|---|
| === List os variants === | === List os variants === |
| |
| To get the value for the ''–os-variant run the following command: $** osinfo-query os** === Virsh list all domains === To list both inactive and active domains, use the command: $ **sudo virsh list –all** Id Name State | To get the value for the ''–os-variant run the following command: $** osinfo-query os** === Virsh list all domains === To list both inactive and active domains, use the command: $ **sudo virsh list –all** Id Name State —- - admin shut off - cloudstack shut off - hyperv shut off - ipa shut off - katello shut off - node1 shut off - node2 shut off - node3 shut off - server1 shut off - server2 shut off - test shut off - ubuntu14.04 shut off - win12k shut off - xen shut off - zenoss shut off === List only active domains === To list only active domains with virsh command, use: '' $** sudo virsh list** Id Name State —————————————————- === Virsh rename domain === Syntax: '' virsh domrename //**currentname**// **//newname// ** List available domains '' # **virsh list** **–all** Id Name State —- 4 centos-8.2 running |
| |
| ---- | ''#** virsh domrename centos-8.2 apacheserver01** Domain successfully renamed We’ve changed the name of the domain from //centos-8.2 //to //apacheserver01// ==== Virsh change domain boot disk ==== Edit the domain by passing its name: '' # **virsh edit domain** Scroll down until** <devices>** section and modify values for the line below: '' <source file=/> '' Example |
| | <code> |
| ''- admin shut off - cloudstack shut off - hyperv shut off - ipa shut off - katello shut off - node1 shut off - node2 shut off - node3 shut off - server1 shut off - server2 shut off - test shut off - ubuntu14.04 shut off - win12k shut off - xen shut off - zenoss shut off === List only active domains === To list only active domains with virsh command, use: '' $** sudo virsh list** Id Name State —————————————————-'' === Virsh rename domain === Syntax: '' virsh domrename //**currentname**// **//newname// **'' List available domains '' # **virsh list** **–all** Id Name State | ''<source file='/var/lib/libvirt/images/**apacheserver01**.**qcow2**'/> |
| | |
| ---- | |
| | |
| ''4 centos-8.2 running | |
| | |
| ''#** virsh domrename centos-8.2 apacheserver01** Domain successfully renamed'' We’ve changed the name of the domain from //centos-8.2 //to //apacheserver01// ==== Virsh change domain boot disk ==== Edit the domain by passing its name: '' # **virsh edit domain**'' Scroll down until** <devices>** section and modify values for the line below: '' <source file=''/> | |
| '' | '' |
| </file> Example | |
| |
| ''<code>''<source file='/var/lib/libvirt/images/**apacheserver01**.**qcow2**'/> | |
| '' | |
| </file> | </file> |
| === Virsh start vm === | === Virsh start vm === |
| </code> | </code> |
| |
| Keep an eye on the option ''Autostart: enable. === Virsh autostart disable === To disable autostart feature for a vm: $ **virsh autostart –disable test** Domain test unmarked as autostarted $ **virsh dominfo test** Id: - Name: test UUID: a943ed42-ba62-4270-a41d-7f81e793d754 OS Type: hvm State: shut off CPU(s): 2 Max memory: 2048 KiB Used memory: 2048 KiB Persistent: yes Autostart: disable Managed save: no Security model: none Security DOI: 0 === Virsh stop vm, virsh shutdown vm === To shutdown a running vm gracefully use: '' $ **sudo virsh shutdown test** Domain test is being shutdown $ **sudo virsh list** Id Name State —————————————————- === Virsh force shutdown vm === You can do a forceful shutdown of active domain using the command: $ sudo 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 `sudo virsh list | grep running | awk '{print $2}'` do '' sudo virsh shutdown $i | Keep an eye on the option ''Autostart: enable. === Virsh autostart disable === To disable autostart feature for a vm: $ **virsh autostart –disable test** Domain test unmarked as autostarted $ **virsh dominfo test** Id: - Name: test UUID: a943ed42-ba62-4270-a41d-7f81e793d754 OS Type: hvm State: shut off CPU(s): 2 Max memory: 2048 KiB Used memory: 2048 KiB Persistent: yes Autostart: disable Managed save: no Security model: none Security DOI: 0 === Virsh stop vm, virsh shutdown vm === To shutdown a running vm gracefully use: $ **sudo virsh shutdown test** Domain test is being shutdown $ **sudo virsh list** Id Name State —————————————————- === Virsh force shutdown vm === You can do a forceful shutdown of active domain using the command: $ sudo 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 `sudo virsh list | grep running | awk '{print $2}'` do sudo virsh shutdown $i |
| |
| done'' === Virsh reboot vm === To restart a vm named test, the command used is: sudo 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. sudo virsh destroy test 2> /dev/null sudo virsh undefine test sudo virsh pool-refresh default sudo virsh vol-delete –pool default test.qcow2 In this example, storage volume is named /var/lib/libvirt/images/test.qcow2 You can also use undefine with//–remove-all-storage// option: '' virsh undefine test –remove-all-storage === 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. '' sudo virt-install \ –name centos7 \ –description __GESHI_QUOT__Test VM with CentOS 7__GESHI_QUOT__ \ –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' | done'' === Virsh reboot vm === To restart a vm named test, the command used is: sudo 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. sudo virsh destroy test 2> /dev/null sudo virsh undefine test sudo virsh pool-refresh default sudo virsh vol-delete –pool default test.qcow2 In this example, storage volume is named /var/lib/libvirt/images/test.qcow2 You can also use undefine with//–remove-all-storage// option: '' virsh undefine test –remove-all-storage === 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. sudo virt-install \ –name centos7 \ –description __GESHI_QUOT__Test VM with CentOS 7__GESHI_QUOT__ \ –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: '' $ sudo virsh console test This will return a fail message if an active console session exists for the provided domain. To solve this run: '' $ sudo virsh console test –force === Virsh edit vm xml file === To edit a vm xml file, use: '' $ sudo EDITOR=vim $ virsh edit test To use nano text editor '' $ sudo EDITOR=nano $ virsh edit test === Virsh suspend vm, virsh resume vm === To suspend a guest called testwith virsh command, run: '' $ **sudo 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: '' **$ sudo 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: '' $ **sudo 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: $ **sudo virsh restore test.save ** Domain restored from test.save |
| === Virsh connect to vm console === To connect to the guest console, use the command: '' $ sudo virsh console test'' This will return a fail message if an active console session exists for the provided domain. To solve this run: '' $ sudo virsh console test –force'' === Virsh edit vm xml file === To edit a vm xml file, use: '' $ sudo EDITOR=vim $ virsh edit test'' To use nano text editor '' $ sudo EDITOR=nano $ virsh edit test'' === Virsh suspend vm, virsh resume vm === To suspend a guest called testwith virsh command, run: '' $ **sudo 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: '' **$ sudo 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: '' $ **sudo 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: '' $ **sudo virsh restore test.save ** Domain restored from test.save | ''$ sudo virsh list Id Name State —- '' 7 test running |
| |
| ''$ sudo virsh list Id Name State | ''==== Virsh Manage Volumes ==== Here we’ll cover how to create a storage volume , attach it to a vm , detach it from a vm and how to delete a volume. === Virsh create volume === To create a 2GB volume named test_vol2 on the default storage pool, use: '' $** sudo virsh vol-create-as default test_vol2.qcow2 2G** Vol test_vol2.qcow2 created |
| |
| ---- | ''$ **sudo du -sh /var/lib/libvirt/images/test_vol2.qcow2** 2.0G/var/lib/libvirt/images/test_vol2.qcow2'' * **default**: Is the pool name. * **test_vol2**: This is the name of the volume. * **2G**: This is the storage capacity of the volume. List volumes '' sudo virsh vol-list –pool default sudo virsh vol-list –pool images'' === Virsh attach a volume to vm === To attach created volume above to vm test, run: '' $ sudo 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 You can confirm that the volume was added to the vm as a block device /dev/vdb '' $ **ssh test** Last login: Fri Mar 17 19:30:54 2017 from gateway [root@test ~]# |
| | |
| | ''[root@test ~]# **lsblk –output NAME,SIZE,TYPE** NAME SIZE TYPE sr0 1024M rom vda 10G disk ├─vda1 1G part └─vda2 9G part |
| <code> | <code> |
| |
| ''7 test running | ''├─cl_test-root 8G lvm |
| | └─cl_test-swap 1G lvm |
| | vdb 2G disk |
| |
| </code> | </code> |
| '' | '' |
| |
| ==== Virsh Manage Volumes ==== | |
| |
| Here we’ll cover how to create a storage volume , attach it to a vm , detach it from a vm and how to delete a volume. | |
| |
| === Virsh create volume === | |
| |
| To create a 2GB volume named test_vol2 on the default storage pool, use: | |
| |
| <code> | |
| ''$** sudo virsh vol-create-as default test_vol2.qcow2 2G** Vol test_vol2.qcow2 created | |
| |
| $ **sudo du -sh /var/lib/libvirt/images/test_vol2.qcow2** 2.0G/var/lib/libvirt/images/test_vol2.qcow2'' | |
| |
| </code> | |
| |
| * **default**: Is the pool name. | |
| |
| * **test_vol2**: This is the name of the volume. | |
| * **2G**: This is the storage capacity of the volume. | |
| List volumes | |
| |
| <code> | |
| ''sudo virsh vol-list --pool default | |
| sudo virsh vol-list --pool images'' | |
| |
| </code> | |
| |
| === Virsh attach a volume to vm === | |
| |
| To attach created volume above to vm test, run: | |
| |
| <code> | |
| ''$ sudo virsh attach-disk --domain test \ | |
| --source /var/lib/libvirt/images/test_vol2.qcow2 \ | |
| --persistent --target vdb'' | |
| |
| </code> | |
| |
| * //''–persistent'' //: Make live change persistent | |
| * //''–target vdb'' //: Target of a disk device | |
| You can confirm that the volume was added to the vm as a block device /dev/vdb | |
| |
| <code> | |
| ''$ **ssh test** | |
| Last login: Fri Mar 17 19:30:54 2017 from gateway | |
| [root@test ~]# | |
| |
| [root@test ~]# **lsblk --output NAME,SIZE,TYPE** | |
| NAME SIZE TYPE | |
| sr0 1024M rom | |
| vda 10G disk | |
| ├─vda1 1G part | |
| └─vda2 9G part | |
| ├─cl_test-root 8G lvm | |
| └─cl_test-swap 1G lvm | |
| vdb 2G disk'' | |
| |
| </code> | |
| |
| Example showing how to create and attach secondary virtual disk: | Example showing how to create and attach secondary virtual disk: |
| ===== Display VM disk usage ===== | ===== Display VM disk usage ===== |
| |
| Use the //virt-df// command: | Use the //virt-df// command: |
| <code> | <code> |
| ''$ **sudo virt-df -d sg-ve-01 ** | ''$ **sudo virt-df -d sg-ve-01 ** |
| |
| “virt-log” is a command line tool to display the log files from the named virtual machine (or disk image). This tool understands and displays both plain text log files (eg. **/var/log/messages**) and binary formats such as the systemd journal. | “virt-log” is a command line tool to display the log files from the named virtual machine (or disk image). This tool understands and displays both plain text log files (eg. **/var/log/messages**) and binary formats such as the systemd journal. |
| |
| <code> | <code> |
| ''$ sudo virt-log -d <domain> | less | ''$ sudo virt-log -d <domain> | less |