启动 KVM 虚拟机

发布时间: 更新时间: 总字数:3945 阅读时间:8m 作者:IP:上海 网址

下面介绍使用 virt-install/kvm 创建 VM。

默认路径

  • 镜像默认目录 /var/lib/libvirt/images/
  • 虚拟机配置文件默认目录 /etc/libvirt/qemu/

kvm

Help

qemu-kvm

示例

# 初始化磁盘
$ qemu-img create -f qcow2 test.qcow2 20G
或
$ dd if=/dev/zero of=./test.img bs=1M count=20480

# 启动虚拟机
$ kvm -m 1024 -vnc :0 -hda ./test.qcow2 -cdrom CentOS-7-x86_64-DVD-1908.iso # -cpu host

然后使用 vnc-viewer 访问 5900(= 5900 + vnc 后面的id) 端口,安装操作系统。系统安装完成后,test.qcow2 就是一个 KVM 镜像。

参数说明:

  • -m 内存
  • -smp CPU个数,格式为 -smp n[,maxcpus=cpus][,cores=cores][,threads=threads][,sockets=sockets]
    • n vcpu 数量,默认为 1
    • maxcpus 最大 vcpu 数量
    • cores 每个 cpu 的 core 数量
    • threads 每个 core 线程数量
    • sockets cpu 插槽数
  • -cdrom CD
  • -drive file 文件
  • -fda 驱动
  • -boot boot启动
    • a, b (floppy 1 and 2)
    • c (first hard disk),hard disk boot is the default
    • d (first CD-ROM)
      • n-p (Etherboot from network adapter 1-4)
  • -vnc VNC端口

其他说明:

  • 虚拟机中 cpu 被称为 vcpu
  • 每个虚拟机是宿主机中的一个 qemu 进程
  • 每个 vcpuqemu 进程派生出来的一个线程

virt-install

Help

virt-install

示例

  • 使用默认网络
virt-install \
  --name centos7 \
  --virt-type kvm \
  --vcpus 1 \
  --cpu IvyBridge \
  --memory 1024 \
  --location /data/CentOS-7-x86_64-DVD-2009.iso \
  --os-variant rhel7.0 \
  --disk path=/data/centos7.qcow2,size=15,format=qcow2 \
  --network network=default \
  --boot hd,cdrom,menu=on \
  --graphics vnc,port=5901,listen=0.0.0.0 \
  --extra-args='console=ttyS0' \
  --force

然后通过如下命令进入 console,[!]代表需要配置的,[x]代表已经设置了。输入对应的数字,配置和安装系统:

virsh console centos7

指定 --graphics 参数,需要通过命令行窗口,输入以下命令启动虚拟机管理GUI程序:

virt-manager

指定 linux bridge 网络

参考 Linux 网桥介绍 创建 bridge:

root@ubuntu:~# brctl show
bridge name	bridge id		STP enabled	interfaces
root@ubuntu:~# brctl addbr br0
root@ubuntu:~# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.c6c584f5fb2c	no
root@ubuntu:~# ip link set dev br0 up
root@ubuntu:~# ip addr add 192.168.179.1/24 dev br0
root@ubuntu:~# ip a show br0
11: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether c6:c5:84:f5:fb:2c brd ff:ff:ff:ff:ff:ff
    inet 192.168.179.1/24 scope global br0
       valid_lft forever preferred_lft forever
virt-install \
  --name centos7 \
  --virt-type kvm \
  --vcpus 1 \
  --cpu IvyBridge \
  --memory 1024 \
  --location /data/CentOS-7-x86_64-DVD-2009.iso \
  --disk path=/data/centos7.qcow2,size=15,format=qcow2 \
  --network bridge=br0 \
  --boot hd,cdrom,menu=on \
  --graphics vnc,port=5901,listen=0.0.0.0 \
  --extra-args='console=ttyS0' \
  --force
本文总阅读量 次 本站总访问量 次 本站总访客数