WinXP:

#!/bin/bash

name=winxp

qemu-system-x86_64 \
-enable-kvm \
-cpu host -smp ,sockets=,cores=,threads= \
-m \
-net user \
-drive file=./WinXP.img,if=none,cache=writeback,id=${name}_disk -device ide-hd,drive=${name}_disk \
-drive file=./WinXP.iso,readonly=on,media=cdrom \
-boot order=cd \
-name ${name} \
-display gtk

一、管理 images

1、创建

  qemu-img create -f raw -o size=10G,nocow=on base.raw  #nocow 用于关闭 btrfs 的写时复制,仅对新创建的文件生效

2、扩容

  qemu-img resize base.raw +20G

3、查询信息

  qemu-img info base.raw

4、转换格式

  qemu-img convert -c -O qcow2 base.raw base.qcow2

5、增量创建

  qemu-img create -f qcow2 -o size=20G,nocow=on,backing_file=base.qcow2 linux.qcow2  #批量运行时减少磁盘占用

6、重改基础镜像

  qemu-img rebase [-u] -b /PATH/TO/xxx.qcow2 linux.qcow2  # -u 指 unsafe 模式,无需检验,通常用于基础镜像移动位置或重命名之后

二、管理 snapshot

1、创建

  qemu-img snapshot -c v0.1_snapshot linux.qcow2

2、查询

  qemu-img snapshot -l linux.qcow2

3、应用

  qemu-img snapshot -a v0.1_snapshot linux.qcow2

4、删除

  qemu-img snapshot -d v0.1_snapshot linux.qcow2

三、热拔插

# 首先需要客户机载入:modprobe pci_hotplug

# 然后在 monitor 中操作

  drive_add 0 media=disk,file=/PATH/TO/xxx.qcow2,if=none,id=vd0  #添加到 Qemu

  device_add virtio-blk-pci,drive=vd0  #插入到客户机

  drive_del tmp_0  #移除

四、Gentoo

  /etc/portage/package.use/qemu:

  vhost-net -nls -iscsi -nfs -opengl -jpeg -png -usb -usbredir -curl -selinux -spice -ncurses -sdl -gtk -xen -alsa -bluetooth

五、示例

  init.sh:

 #!/usr/bin/env sh
#
# 、目录结构:conf、images、ISOs
# 、安装镜像存放于"../ISOs"路径下,命名格式:FreeBSD.iso
# 、配置好每个OS的基础镜像,置于"../images"路径下,命名格式:FreeBSD_base.img
# if [[ $ == '' ]];then
VmNum=
printf "\033[31;01m\$VmNum is not specified, defaults to 1\n\033[00m"
elif [[ $ =~ [-] ]];then
VmNum=$
else
printf "\033[31;01m\$1 is not a number!!!\n\033[00m"
exit
fi if [[ $ == '' ]];then
OS=Gentoo
printf "\033[31;01mOS is not specified, defaults to \"Gentoo\"\n\033[00m"
else
OS=$
fi ISO=${OS}.iso
BaseImg=${OS}_base.img
CpuNum=
Mem=4G
MaxMem=8G
DiskSiz=200G
MaxVmNum=
HostIP=192.168.0.254 if [[ $ -gt $MaxVmNum ]];then
printf "\033[31;01mvmNum > ${MaxVmNum}!!!\n\033[00m"
exit
fi case $OS in
FreeBSD)
Version=
AddrPos=
;;
Gentoo)
Version=
AddrPos=
;;
RHEL)
Version=
AddrPos=
;;
Debian)
Version=
AddrPos=
;;
Ubuntu)
Version=
AddrPos=
;;
*)
printf "\033[31;01mUnknown OS name!!!\n\033[00m"
printf "Supported OS name:FreeBSD Gentoo RHEL Debian Ubuntu\n"
exit
;;
esac vmFunc() {
qemu-system-x86_64 \
-enable-kvm \
-machine q35,accel=kvm -device intel-iommu \
-cpu host -smp $CpuNum,sockets=$CpuNum,cores=,threads= \
-m $Mem,slots=,maxmem=$MaxMem \
-netdev tap,ifname=${OS}_$,script=tap.sh,downscript=no,id=vmNic_${OS}_$ -device virtio-net-pci,mac=::::$:${Version},netdev=vmNic_${OS}_$ \
-drive file=../images/$ImgName,if=none,cache=writeback,media=disk,id=vmDisk_${OS}_$ -device virtio-blk-pci,drive=vmDisk_${OS}_$ \
-drive file=../ISOs/$ISO,readonly=on,media=cdrom \
-boot order=cd \
-name vm${OS}_$ \
-monitor tcp:$HostIP:$(($ * )),server,nowait \
-vnc :$ \
-daemonize
} OpsFunc() {
eval x=$(($ + $AddrPos))
local ImgName=${OS}_$x.img if [[ -eq $(ls ../images | grep -c $ImgName) ]];then
qemu-img create -f qcow2 -o size=$DiskSiz,nocow=on,backing_file=../images/$BaseImg ../images/$ImgName
if [[ -ne $? ]];then
printf "\033[31;01mCan't create $ImgName!!!\n\033[00m"
exit
fi
fi vmFunc $x
} i=
while [[ $i -lt $VmNum ]]
do
OpsFunc $i
let i+=
done

...

/*基于硬件IOMMU支持的硬件直接分配技术:VFIO*/
#modprobe vfio-pci
#echo 8c20 > /sys/bus/pci/drivers/vfio-pci/new_id
#echo ::1b. > /sys/bus/pci/devices/::1b./driver/unbind
#echo ::1b. > /sys/bus/pci/drivers/vfio-pci/bind
/*Qemu设置直连物理网卡示例*/
#-device vfio-pci,host=:1b.
#-net none

  net.sh

#!/usr/bin/env sh

modprobe tun
modprobe vhost
modprobe vhost_net
modprobe bonding IF_0=enp2s0
IF_1=eno1
BD=bond0
SW=br0 ADDR=192.168.0.254/
ROUTE=192.168.0.1 ip link del $BD #echo -$BD > /sys/class/net/bonding_masters if [[ -eq $(ip link | grep -c $SW) ]];then
ip link add $SW type bridge
fi
ip link add $BD type bond #echo +$BD > /sys/class/net/bonding_masters for x in $IF_0 $IF_1 $BD $SW
do
ip addr flush dev $x
ip route flush dev $x
done ip link set $IF_0 down
ip link set $IF_1 down
ip link set $BD down
echo active-backup > /sys/class/net/$BD/bonding/mode #balance-rr/active-backup/broadcast
echo > /sys/class/net/bond0/bonding/miimon
echo +$IF_0 > /sys/class/net/$BD/bonding/slaves
echo +$IF_1 > /sys/class/net/$BD/bonding/slaves
ip link set $IF_0 up
ip link set $IF_1 up
ip link set $BD up #ip link set $IF_0 promisc on
#ip link set $IF_1 promisc on
#ip link set $BD promisc on ip link set $BD master $SW
ip addr add $ADDR dev $SW
ip link set $SW up
ip route replace default via $ROUTE dev $SW printf "\033[31;01mSlaves of $BD:\033[00m\n"
cat /sys/class/net/$BD/bonding/slaves #printf "\n\033[31;01m$BD info:\033[00m\n"
#cat /proc/net/bonding/$BD

  tap.sh

      开头必须加 #!/usr/bin/env bash  一行

#!/usr/bin/env bash

ip link set $ up
sleep .1s
ip link set $ master br0
05-28 14:12