메뉴 건너뛰기

2019.07.07 08:06

KVM Cheatsheet - centos

조회 수 10 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

KVM Cheatsheet

List Running Virtual Machines

List All Virtual Machines

virsh list --all

Managing Guest State

Shutdown Guest

virsh shutdown $VM_ID_OR_NAME

Start Guest

virsh start $VM_ID_OR_NAME

Reboot Guest

virsh reboot $VM_ID_OR_NAME

Destroy a Guest

This command is an ungraceful shutdown, much like if you were to pull the power out of a computer. You should use this if a guest becomes unresponsive. It does notdelete the guest. The disk image will remain and the guest can be restarted.

virsh destroy $VM_ID_OR_NAME

Suspension

Suspension is a way to immediately "pause" a guest so that it no longer uses the CPU, disk, or network. However, it will continue to reside in memory. You may want to save/load a session instead, which would mean it no longer takes up memory, but is not instant. such that it no longer takes up memory and can be restored to its exact state (even after a reboot), it is necessary to save and restore the guest.

A suspended session will be lost if the host system is rebooted. However, a saved guest does persist.

Suspend Guest

virsh suspend $VM_ID_OR_NAME

Resume Guest

virsh resume $VM_ID_OR_NAME

Defining

Defining a Guest

Defining a guest allows one to start it from its name, rather than having to find it's XML file and running virsh create $name.xml. This means that guests will also show in virsh list --all when they are shutdown.

sudo virsh define filename.xml

Undefine a Guest

In order to use a name over again for a new guest, you have to undefine the old one. You need to remove it's storage system as well.

sudo virsh undefine $VM_ID

Guest Configuration

You can manually edit the guest's xml configuration file with:

sudo virsh edit $VM_ID

Changes will not take effect until the guest is rebooted

List OS Variants

When creating a guest with virt-install you need to specify the --os-variant. To get a list of acceptable values (on Ubuntu 16.04), install the libosinfo-bin package before running the command below:

osinfo-query os

Rename guest

virsh domrename $OLD_NAME $NEW_NAME

You can only do this whilst the guest is not running.

Guest Start on Boot (Autostart)

sudo virsh autostart $VM_ID

To disable Guest Autostart

sudo virsh autostart --disable $VM_ID

Resizing Memory

Run the following command to bring up the config for the guest.

sudo virsh edit $VM_ID

Change the memory and currentMemory fields to be the size you want in KiB.

Now use virsh to shutdown and startup the container for the changes to take effect.

Resizing Memory With Script

VM_ID="my_vm_id"
NEW_AMOUNT="4000"

EDITOR='sed -i "s;[0-9]*</currentMemory>;$NEW_AMOUNT</currentMemory>;"' virsh edit $VM_ID
EDITOR='sed -i "s;[0-9]*</memory>;$NEW_AMOUNT</memory>;"' virsh edit $VM_ID

sudo virsh shutdown $VM_ID
sudo virsh start $VM_ID
Copy to clipboard

Do not use virsh memtune. See here for more details.

CPU Management

Discover CPU Scheduling Parameters

sudo virsh schedinfo $VM_ID

Permanently Set CPU Shares For Live Running Instance

sudo virsh schedinfo $VM_ID \
--set cpu_shares=[0-262144] \
--live \
--current \
--config

Get the CPU Pinning Settings for a Guest

virsh vcpupin blog.programster.org

Example output:

VCPU: CPU Affinity
----------------------------------
   0: 0-3
   1: 0-3

I got the output above because I gave the guest access to 2 vCPUs but didn't pin anything.

Pin A CPU

If I wanted to set the cores that a guest can use, I could do the following:

virsh vcpupin blog.programster.org 0 2

That will set the first vCPU (the one with ID 0) to only run on core ID 2. Thus the output of virsh vcpupin blog.programster.org changes to:

VCPU: CPU Affinity
----------------------------------
   0: 2
   1: 0-3

Pinning could be a great way to limit the effect a certain guest has on others, or to give a guest a dedicated core etc.

Guest Console

Enter Guest's Console

sudo virsh console $VM_ID

Exit Guest's Console

Use the following keyboard shortcut (not a command):

Cntrl-]

Saving

Save Guest

virsh save $VM_ID $FILENAME

Load Guest

virsh restore $FILENAME

The filename here is the same file that you saved to in the previous command, not one of the other guest files!

Simple Guest Clone

virt-clone \
--original $VM_TO_CLONE \
--auto-clone \
--name $NEW_VM_NAME

Networking

List Running Network Configs

virsh net-list

List All Network Configs

virsh net-list --all

You can find network configs stored in /home/stuart/network-configs/

Edit Network Config

sudo virsh net-list $NETWORK_NAME

Create Temporary Network Config

sudo virsh net-create --file $ABSOLUTE_FILE_PATH

Create Permanent Network Config

sudo virsh net-define --file $ABSOLUTE_FILE_PATH

Example Bridge Network Config File

<network>
  <name>examplebridge</name>
  <forward mode='route'/>
  <bridge name='kvmbr0' stp='on' delay='0'/>
  <ip address='192.168.1.1' netmask='255.255.255.0' />
</network>
Copy to clipboard

Start Network Config

sudo virsh net-start $NETWORK_ID

Enable Network Autostart

net-autostart --network $NETWORK_ID

Disable Network Autostart

net-autostart \
--network $NETWORK_ID \
--disable

Example Manual Network Config With Bridge

This is an example /etc/network/interfaces file for Ubuntu users.

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto p17p1
iface p17p1 inet manual

auto kvmbr0
iface kvmbr0 inet static
    address 192.168.1.19
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255
    gateway 192.168.1.254
    bridge_ports p17p1
    bridge_stp off
    bridge_fd 0
    bridge_maxwait 0

Configure VM To Use Manual Bridge

If you manually set the bridge up with the section above rather than through using the virsh net commands, this is how to configure deployed guests make use of it:

sudo virsh edit $VM_ID

Find the following section

    <interface type='network'>
      <mac address='52:54:00:4d:3a:bd'/>
      <source network=''/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </interface>
Copy to clipboard

Change it to be like so:

    <interface type='bridge'>
        <mac address='52:54:00:4d:3a:bd'/>
        <source bridge='[bridge name here]'/>
        <model type='virtio'/>
        <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </interface>
Copy to clipboard

Now run the following two commands since reboots wont work.

sudo virsh shutdown $VM_ID
sudo virsh start $VM_ID

Add Network Interface to VM

I used the command below to add a NIC to my guest that uses my host's bridge interface called kvmbr1.

virsh attach-interface \
--domain guest1 \
--type bridge \
--source kvmbr1 \
--model virtio \
--config

If your guest is running at the time, you need to add the --live parameter.

You could specify a mac address with --mac but without it, one will be generated randomly.

Snapshotting

Create Internal Snapshot

virsh snapshot-create $VM_ID

You can take snapshots of guests whilst they are running. Whilst the snapshot is being taken, the guest will be "paused". The "state" of the guest is also saved.

Create Internal Snapshot With Name

sudo virsh snapshot-create-as $VM_ID $SNAPSHOT_NAME

Create Internal Snapshot With Name and Description

sudo virsh snapshot-create-as $VM_ID $SNAPSHOT_NAME $DESCRIPTION

Create Internal Snapshot With Name and Description Using File

If you just love writing xml, then you can create a file like so:

<domainsnapshot>
    <name>Name for the snapshot</name>
    <description>Description for the snapshot</description>
</domainsnapshot>
Copy to clipboard

... then pass it to virsh snapshot-create to create the snapshot

virsh snapshot-create $VM_ID $FILEPATH

Create External Snapshot

Refer here.

List Snapshots

sudo virsh snapshot-list $VM_ID

Snapshot-list defaults to being in alphabetical rather than chronological order. If you want to find out what your latest snapshots are, you may wish to add the optional --tree or --leaves parameters.

Restore Snapshot

virsh snapshot-revert $VM_ID $SNAPSHOT_NAME

Delete Snapshot

virsh snapshot-delete $VM_ID $SNAPSHOT_NAME

More snapshot functionality can be found in Qcow2 Conversion and Snapshotting

Edit Snapshot

If you use virsh with internal qcow2 snapshots and you decide to move the file to another location, you will not be able to restore those snapshots. This is easily fixed by editing the snapshots and updating the filepath.

sudo virsh snapshot-edt $VM_ID_OR_NAME $NAME_OF_SNAPSHOT

References

Last updated: 16th August 2018 
First published: 16th August 2018

List of Articles
번호 제목 글쓴이 날짜 조회 수
19 RemoteFX - Windows 7 가상 머신에서 DirectX 9c 환경을 제공 맨하탄노숙자 2015.05.14 652
18 VMware workstation 11 host Linux NAT 셋업 맨하탄노숙자 2015.04.17 418
17 Using VMware Workstation VMware Workstation 11 Manual file 맨하탄노숙자 2015.04.19 270
16 VMware에서 Port forwarding 방법 맨하탄노숙자 2015.04.27 230
15 Enabling the SSD option on SSD based disks/LUNs that are not detected as SSD by default (2013188) 맨하탄노숙자 2016.04.05 224
14 MS RemoteFX 관련 VM별 메모리 할당 크기 file 맨하탄노숙자 2015.05.14 210
13 esxi force reboot - esxi 6 강제 재부팅 맨하탄노숙자 2016.04.02 209
12 CloudStack 4.1 설치하기 맨하탄노숙자 2015.06.19 209
11 VMware ESXi vib 설치 맨하탄노숙자 2016.04.30 204
10 Easily upgrade ESXi 5.x to the latest ESXi 6.0 via CLI 맨하탄노숙자 2016.04.30 188
9 esxi 6 vib install 맨하탄노숙자 2016.04.05 176
8 Installing on VMware vib 맨하탄노숙자 2016.04.05 173
7 virt-install OS variant does not exist 맨하탄노숙자 2019.07.07 16
6 libvirt qemu 맨하탄노숙자 2019.07.11 15
5 Virtual machine CPU configuration 맨하탄노숙자 2019.07.12 14
4 CentOS 7 KVM Bridge Network 브릿지 설정 맨하탄노숙자 2019.07.11 13
3 Booting from a cdrom in a kvm guest using libvirt 맨하탄노숙자 2019.07.11 12
» KVM Cheatsheet - centos 맨하탄노숙자 2019.07.07 10
1 VMware Workstation 11.x Universal License for Windows & Linux secret 맨하탄노숙자 2015.04.16 0
Board Pagination Prev 1 Next
/ 1