Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manage KVM virtual machine in Ubuntu command line terminal

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to manage the KVM virtual machine in the command line terminal of Ubuntu". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Step 1: make sure your hardware platform supports virtualization

The first step is to make sure that your CPU supports hardware virtualization extensions (e.g.Intel VT or AMD-V), which is the hardware requirement of KVM. The following command checks whether the hardware supports virtualization.

The code is as follows:

$egrep'(vmx | svm)'--color / proc/cpuinfo

If the vmx or svm logo is not included in the output, it means that your cpu does not support hardware virtualization. So you can't use KVM on your machine. After confirming that cpu supports vmx or svm, start installing KVM.

For KVM, it is not required to run on a host with a 64-bit kernel system, but it is generally recommended to run KVM on a host on a 64-bit system.

Step 2: install KVM

Use apt-get to install KVM and related user space tools.

The code is as follows:

$sudo apt-get install qemu-kvm libvirt-bin

During installation, the libvirtd user group (libvirtd-qemu user group on debian) will be created, and your user id will be automatically added to the group. The purpose of this is to allow you to manage the virtual machine as a normal user rather than a root user. You can confirm this with the id command, which will show you how to display your group id:

The code is as follows:

$id

If for some reason libvirt (libvirt-qemu in debian) is not found in your group id, you can also manually add yourself to the corresponding group, as shown below:

On ubuntu:

The code is as follows:

$sudo adduser [youruserID] libvirtd

On debian:

The code is as follows:

$sudo adduser [youruserID] libvirt-qemu

Reload the updated group membership as follows. If you are required to enter a password, then enter your login password.

The code is as follows:

$exec su-l $USER

At this point, you should be able to execute virsh as a normal user. Do a test as shown below, and this command will list the available virtual machines in the form of a list (the current list is empty). If you don't have a permission problem, that means everything is fine so far.

The code is as follows:

$virsh list

Id Name State

Step 3: configure the bridged network

One way to enable the KVM virtual machine to access the external network is by creating a Linux bridge on the KVM host. The created bridge can connect the virtual network card of the virtual machine with the physical network card of the host host, so the virtual machine can send and receive data packets transmitted by the physical network card. This method is called network bridging.

The following will show you how to create and configure a bridge. We will create a bridge and call it br0.

First, install a required package, and then create a bridge from the command line.

The code is as follows:

$sudo apt-get install bridge-utils

$sudo brctl addbr br0

The next step is to configure the bridge that has been created, that is, to modify the configuration file at / etc/network/interfaces. We need to set the bridge card to boot. In order to modify the configuration file, you need to close the network manager on your operating system (if you are using it). Follow the instructions in the instructions to close the network manager.

After closing the network manager, the next step is to configure the bridge by modifying the configuration file.

The code is as follows:

# auto eth0

# iface eth0 inet dhcp

Auto br0

Iface br0 inet dhcp

Bridge_ports eth0

Bridge_stp off

Bridge_fd 0

Bridge_maxwait 0

In the above configuration, I assume that eth0 is the primary network card and it is also the network card connected to the external network. Similarly, I assume that eth0 will automatically obtain the ip address through the DHCP protocol. Note that eth0 has not been configured in / etc/network/interfaces before. The bridging network card br0 refers to the configuration of eth0, and eth0 is also restricted by br0.

Restart the network service and verify that the bridge has been successfully configured. If successful, the ip address of br0 will be the ip address automatically assigned by eth0, and eth0 will not be assigned any ip address.

The code is as follows:

$sudo / etc/init.d/networking restart

$ifconfig

If for some reason eth0 still retains the ip address that was previously assigned to br0, you may have to delete the ip address of eth0 manually.

Step 4: create a virtual machine with the command line

For a virtual machine, its configuration information is stored in its corresponding xml file. Therefore, the first step in creating a virtual machine is to prepare a xml file corresponding to the virtual machine.

Here is a sample xml file that you can modify manually as needed.

XML/HTML Code copies content to the clipboard

Alice

F5b8c05b-9c7a-3211-49b9-2bd635f7e2aa

1048576

1048576

one

Hvm

Destroy

Restart

Destroy

/ usr/bin/kvm

The host xml configuration file above defines the following virtual machine content.

1GB memory, a virtual cpu and a hardware driver

Disk image: / home/dev/images/alice.img

Boot from CD-ROM (/ home/dev/iso/CentOS-6.5-x86_64-minomal.iso)

Network: a virtual network card bridged to br0

Remote access via VNC

The UUID string in can be generated randomly. To get a random uuid string, you may need to use the uuid command line tool.

The code is as follows:

$sudo apt-get install uuid

$uuid

The way to generate a host xml profile is to export its xml profile through an existing virtual machine. As shown below.

The code is as follows:

$virsh dumpxml alice > bob.xml

Step 5: start the virtual machine using the command line

Before starting the virtual machine, we need to create an initial disk image of it. To do this, you need to use the qemu-img command to generate a qemu-kvm image. The following command will create an empty disk of 10 GB size, and it will be in qcow2 format.

The code is as follows:

$qemu-img create-f qcow2 / home/dev/images/alice.img 10G

The advantage of using disk mirroring in qcow2 format is that it does not allocate full-size disk capacity (in this case, 10 GB) to it at the beginning of creation, but increases gradually as the number of files in the virtual machine increases. Therefore, it uses space more effectively.

Now you can start your virtual machine by using the xml configuration file you created earlier. The following command will create a virtual machine and start it automatically.

The code is as follows:

$virsh create alice.xml

Domain alice created from alice.xml

Note: if you execute the above command on an existing virtual machine, this operation will erase all information about the existing virtual machine without any warning. If you have created a virtual machine, you may use the following command to start the virtual machine.

The code is as follows:

$virsh start alice.xml

Use the following command to confirm that a new virtual machine has been created and successfully started.

The code is as follows:

$virsh list

Id Name State

3 alice running

Also, use the following command to confirm that the virtual network card of your virtual machine has been successfully added to the br0 bridge you created earlier.

The code is as follows:

$sudo brctl show

Remotely connect to the virtual machine

To remotely access the console of a running virtual machine, you can use the VNC client.

First, you need to find out the NNC port number for the virtual machine using the following command.

The code is as follows:

$sudo netstat-nap | egrep'(kvm | qemu)'

In this example, the VNC port number for the alice virtual machine is 5900. Then start a VNC client and connect to a VNC server with port number 5900. In our example, the virtual machine support is started by a CentOS CD file.

Use virsh to manage virtual machines

The general usage of the virsh command is listed below:

Create the client and start the virtual machine:

The code is as follows:

$virsh create alice.xml

Stop the virtual machine and delete the client:

The code is as follows:

$virsh destroy alice

Shut down the virtual machine (do not delete it):

The code is as follows:

$virsh shutdown alice

Pause the virtual machine:

The code is as follows:

$virsh suspend alice

Restore the virtual machine:

The code is as follows:

$virsh resume alice

Access the console of the running virtual machine:

The code is as follows:

$virsh console alice

Set the virtual machine to boot:

The code is as follows:

$virsh autostart alice

View the details of the virtual machine:

The code is as follows:

$virsh dominfo alice

Edit the configuration file for the virtual machine:

The code is as follows:

$virsh edit alice

The above command will use a default editor to invoke the host configuration file. Any changes in the configuration file will be automatically verified by libvirt.

You can also manage virtual machines in a virsh session. The following command creates and enters a virsh session:

The code is as follows:

$virsh

In the virsh prompt, you can use any virsh command.

Problem handling

I encountered an error when creating a virtual machine:

Error: internal error: no supported architecture for os type 'hvm'

You may encounter this error if your hardware does not support virtualization. (for example, Intel VT or AMD-V), which is required to run KVM. If you encounter this error and your cpu supports virtualization, here are some available solutions:

First, check to see if your kernel module is missing.

The code is as follows:

$lsmod | grep kvm

If the kernel module is not loaded, you must load it as follows.

The code is as follows:

$sudo modprobe kvm_intel (for Intel processor)

$sudo modprobe kvm_amd (for AMD processor)

The second solution is to add the-- connect qemu:///system parameter to the virsh command, as shown below. You need to add this parameter (e.g. VirtualBox,VMware) when you are using more than one virtual machine manager on your hardware platform.

$virsh-connect qemu:///system create alice.xml

I encountered an error when I tried to access the login console of my virtual machine:

The code is as follows:

$virsh console alice

Error: internal error: cannot find character device

This error occurs because you do not define the console device in your virtual machine configuration file. Add the following internal equipment section to the xml file.

XML/HTML Code copies content to the clipboard

This is the end of the content of "how to manage KVM virtual machines in the command line terminal of Ubuntu". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report