Create VM base on KVM

2 篇文章 0 订阅
2 篇文章 0 订阅

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

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.

SubommandParamDescriptionsExamples
list--inactive/--allCheck VM statusvirsh list --all
rebootDomainReboot guestvirsh reboot Domain_name
shutdownDomainShotdown guestvirsh 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!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值