Create VM base on KVM

Environment

hosts

  • OS: OpenSuSE Leap 42.1
  • Desktop: GNOME

guest

  • OS: OpenSuSE Leap 42.1
  • Desktop: GNOME

Introduce of KVM

Kernel-based Virtual Machine (KVM) is a virtualization infrastructure for the Linux kernel that turns it into a hypervisor. We know kernel pervide this support, so we need more simple tool use this support.
I choice virt-install and virt-manager

Install virt-install/virt-mananger and related dependencies

You need install those package below:

avahi
libvirt
virt-manager
virt-install
virt-viewer
python-avahi

Then restart service libvirtd

systemctl restart libvirtd

Create VM with virt-install/virt-manager

There are two ways to install VM with KVM.

  • virt-install:CLI,manager VM use virsh
  • virt-manager:GUI,easy to operating

This artical will introduce create VM through virt-install.

New VM need disk, wo should create virtual disk for VM, fortunally linuk have qemu disk manage tool–qemu-img. Qemu-img example under below, more informations check man page.
specific format is qcow2

qemu-img create -f qcow2 /YOUR_IMG_LOCATION/opensuse.qcow2 20G

New VM need network card, so you need create virtual network card. You can create an bridge or use default.
If you choice use network bridge,then you need create bridge with yast2.

#GUI, persistent
yast2 lan -> Network Setting -> Add bridge-> bind to physical device-> configure ip -> done

#CLI, not persistent
brctl addbr BRIDGE_NAME(say br0)
brctl addif br0 eth0(Physical device name)
ifconfiugre eth0 0.0.0.0
ifconfigure br0 up
dhclient br0

In this case you create VM need specific give network--network bridge=YOUR_BRIDGE_NAME

If you choice VM use NAT connect to internet, you should double check network default is active or not.

#check dedault is active or not
virsh net-list --all
#if not then convert to active
virsh net-start default
#this is optional ,you can convert network default to autostart
virsh net-autostart default

Network should be set to defaule when u create VM.--network default
then you can create your VM use virt-install:

#to create a VM ,you need give vm name, size of ram, number fo cpu, mirror path, disk path, network, os type and so on.

virt-install --name YOUR_VM_NAME -r 1024 -vcpus=2 --location=/YOUR_IOS_PATH/ --disk=/YOUR_VDISK_PATH/,size=YOUR_VDISK_SIZE,format=YOUR_VDISK_FORMAT --network YOUR_NETWORK_SETTINGS --os_type=linux --force

If you install virt-viewer, then you can see the install graphics interface.

Connect VM to outside

Because of VM network use NAT, so I only explain the way connect to internet of this type VM.
Good news you do have to do anything, when you create VM you also create local network, but if you host cannot connect to outside same as you guests.
Any way, you create VM successfully, congratulations!
BTW, you may want to open ssh service on your VM, so you can ssh to your VM rather than open VM GUI.

open ssh service

#change sshd congifure
vim /etc/ssh/sshd_config
port 22
permitRootlogin yes
...
#open ssh service
systemctl enable sshd.service
systemctl start sshd.service
#change firewall setting
#I just disable the firewall
systemctl stop SuSEfirewall2
systemctl disable SuSEfirewall2

then you can ssh to your VM

Manage your VM

You can use virsh(CLI) or virt-manager(GUI) to manage you VM. I would like use virsh, because it looks more geek! Anyway, this session main focus on usually usage, more info please check man page.

Virsh is a command line interface tool for managing guests and the hypervisor.

The virsh tool is built on the libvirt management API and operates as an alternative to the xm tool and the graphical guest Manager(virt-manager). Unprivileged users can employ this utility for read-only operations. If you plan on running the xend, you should enable xend to run as a service. After modifying the respective configuration file, reboot the system, and xendwill run as a service. You can use virsh to load scripts for the guest machines.

Subommand Param Descriptions Examples
list --inactive/--all Check VM status virsh list --all
reboot Domain Reboot guest virsh reboot Domain_name
shutdown Domain Shotdown guest virsh shotdown Domain_name
TODO

Questions

The first question I met is when I create bridge for VM then host and guest both cannot connect to outside. In order to solve this this problem I try my best do everything I can, I change my network manager I check kernel route table…Still host cannot connect to outside but can ping to other PC in LAN. So I have to rollback, thanks for brtfs!

你可能感兴趣的:(Linux系统,SUSE实习,集群,kvm,opensuse,libvirt)