Create VM base on KVM

本文详细介绍如何使用KVM在Linux环境下搭建虚拟机,包括安装必要的软件包、配置虚拟磁盘与网络设置,并通过virt-install命令创建虚拟机。此外,还介绍了如何管理虚拟机以及配置SSH服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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!

### 虚拟机自动化脚本概述 在 Linux 中,通过编写自动化脚本来简化虚拟机的创建过程是一种常见的实践方法。以下是一个基于 `libvirt` 和 `qemu-kvm` 的虚拟机自动化脚本示例[^1]。 #### 创建虚拟机磁盘文件 以下是用于创建虚拟机磁盘文件的部分代码: ```bash #!/bin/bash IMG_DIR="/var/lib/libvirt/images" BASE_DISK="base.qcow2" create_disk() { local vm_name=$1 qemu-img create -f qcow2 \ -b "${IMG_DIR}/${BASE_DISK}" \ "${IMG_DIR}/${vm_name}.img" 30G >/dev/null } ``` 此部分代码的功能是基于基础镜像 (`base.qcow2`) 创建一个新的虚拟机磁盘文件,并设置其大小为 30GB[^2]。 --- #### 配置 XML 文件并定义虚拟机 接下来,配置虚拟机的 XML 定义文件并将其实例化到系统中: ```bash CONF_DIR="/etc/libvirt/qemu" configure_vm() { local vm_name=$1 sed "s/BASEVM/${vm_name}/g" "/path/to/base.xml" > "${CONF_DIR}/${vm_name}.xml" } define_vm() { local vm_name=$1 sudo virsh define "${CONF_DIR}/${vm_name}.xml" >/dev/null echo "Virtual Machine '${vm_name}' has been defined." } ``` 在此部分中,`sed` 命令被用来替换模板中的占位符(如 `BASEVM`),从而生成特定于新虚拟机的 XML 文件。随后使用 `virsh define` 将该虚拟机实例化到系统中。 --- #### 启动虚拟机 最后一步是启动已定义好的虚拟机: ```bash start_vm() { local vm_name=$1 sudo virsh start "${vm_name}" echo "Virtual Machine '${vm_name}' is now running." } ``` 以上函数会调用 `virsh start` 来启动指定名称的虚拟机[^1]。 --- ### 综合脚本示例 将上述功能组合成一个完整的脚本如下所示: ```bash #!/bin/bash IMG_DIR="/var/lib/libvirt/images" CONF_DIR="/etc/libvirt/qemu" BASE_DISK="base.qcow2" create_disk() { local vm_name=$1 qemu-img create -f qcow2 \ -b "${IMG_DIR}/${BASE_DISK}" \ "${IMG_DIR}/${vm_name}.img" 30G >/dev/null } configure_vm() { local vm_name=$1 sed "s/BASEVM/${vm_name}/g" "/path/to/base.xml" > "${CONF_DIR}/${vm_name}.xml" } define_vm() { local vm_name=$1 sudo virsh define "${CONF_DIR}/${vm_name}.xml" >/dev/null } start_vm() { local vm_name=$1 sudo virsh start "${vm_name}" } main() { if [[ $# -ne 1 ]]; then echo "Usage: $0 <vm_name>" exit 1 fi local vm_name=$1 create_disk "$vm_name"[^2] configure_vm "$vm_name"[^1] define_vm "$vm_name"[^1] start_vm "$vm_name"[^1] echo "Virtual Machine '$vm_name' created and started successfully!" } main "$@" ``` --- ### 注意事项 - 确保安装了必要的工具包,例如 `qemu-kvm`, `libvirt`, 和 `virt-install`。 - 替换 `/path/to/base.xml` 为实际的基础 XML 模板路径。 - 使用前赋予脚本执行权限:`chmod +x script.sh`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值