kvm

Enabling Hardware Virtualization Extension

####################################
cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
####################################
uname -a
Linux debian 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u2 (2019-11-11) x86_64 GNU/Linux
# 查看cpu
lscpu
cat /proc/cpuinfo
# 查看内存
cat /proc/meminfo
# 查看磁盘
fdisk -l
# 查看网卡
lspci | grep -i 'eth'
# 查看bios
dmidecode -t bios
# 查看硬件信息
dmidecode -q
lsmod |grep kvm
# Enabling Hardware Virtualization Extension:
grep --color --perl-regexp 'vmx|svm' /proc/cpuinfo

# svm = AMD processor
# vmx = Intel processor

$ sudo apt update
apt-get install vim curl wget jq ca-certificates vim libpq-dev build-essential openssl htop
# install KVM and QEMU 
apt install qemu qemu-kvm qemu-system qemu-utils
# Installing LibVirt:
apt install libvirt-clients libvirt-daemon-system virtinst

systemctl status libvirtd
systemctl start libvirtd



cat /etc/libvirt/libvirt.conf
cat /etc/libvirt/qemu/networks/default.xml
virsh net-list --all
virsh net-start default
virsh net-autostart default

mkdir -pv /kvm/{disk,iso}

wget http://releases.ubuntu.com/16.04.6/ubuntu-16.04.6-server-amd64.iso



aria2c -s 10 https://mirrors.aliyun.com/centos/8.0.1905/isos/x86_64/CentOS-8-x86_64-1905-dvd1.iso
aria2c -s 10 https://mirrors.aliyun.com/ubuntu-releases/18.04/ubuntu-18.04.3-live-server-amd64.iso
aria2c -s 10 http://mirrors.hust.edu.cn/debian-cd/10.2.0/amd64/iso-cd/debian-10.2.0-amd64-netinst.iso
aria2c -s 10 http://mirrors.huaweicloud.com/centos/7.7.1908/isos/x86_64/CentOS-7-x86_64-DVD-1908.iso
aria2c -s 10 https://mirrors.huaweicloud.com/debian-cd/9.9.0/amd64/iso-cd/debian-9.9.0-amd64-netinst.iso
aria2c -s 20 https://download.opensuse.org/distribution/leap/15.1/iso/openSUSE-Leap-15.1-DVD-x86_64.iso
apt install fdisk
/sbin/fdisk -l
fdisk /dev/vdb
# 1、在 Command (m for help): 提示后, 键入 n
# 2、以后的步骤直接 Enter确认.
# 3、Command (m for help): w
# The partition table has been altered.
# Calling ioctl() to re-read partition table.
# Syncing disks.
# 格式化
$ /sbin/mkfs.xfs /dev/vdb1 
$ /sbin/mkfs.xfs /dev/sda
mkfs.xfs: /dev/sda appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.

blkid /dev/sda

echo "/dev/sda               /data                  xfs    defaults        0 0" >> /etc/fstab
################################
kvm -version
QEMU emulator version 3.1.0 (Debian 1:3.1+dfsg-8+deb10u3)
Copyright (c) 2003-2018 Fabrice Bellard and the QEMU Project developers
################################
qemu-img -V
qemu-img version 3.1.0 (Debian 1:3.1+dfsg-8+deb10u3)
Copyright (c) 2003-2018 Fabrice Bellard and the QEMU Project developers
################################
qemu-system-x86_64  -nographic -device help |grep 9p
################################
virt-install --name server01 \
--os-type linux \
--arch x86_64 \
--ram 2048 \
--vcpus 2 \
--os-variant rhel7 \
--disk /data/kvm/disk/server01_0.img,device=disk,bus=virtio,size=100,format=qcow2 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole \
--hvm \
--cdrom /data/iso/CentOS-7-x86_64-DVD-1908.iso \
--boot cdrom,hd

WARNING  No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.

virsh list --all
# 定义虚拟机:xxx为xml文件所在的路径及文件名称
virsh define xxx.xml 
# 启动虚拟机:xyz为虚拟机xml配置文件中虚拟机的名字
virsh start xyz
# 停止虚拟机:正常关机方法,需要一段才能关机
virsh shutdown xyz 
# 下电虚拟机:此方法为暴力下电,虚拟机立即关闭
virsh destroy xyz
# 反定义虚拟机:从virsh列表里面删除
virsh undefine xxx 
# 临时起虚拟机: 方便开发调试等临时需求,不会持久化,虚拟机关机后就消失了,不推荐生产系统使用
virsh create xxx.xml
# 查看VNC端口: 查看VNC端口,其中xyz可通过virsh list查看
virsh vncdisplay xyz
# 导出虚拟机xml定义文件
virsh --connect qemu:///system dumpxml server01 >server01.xml
# 创建 qcow2 格式的磁盘
qemu-img create -f qcow2 /kvm_data/centos7.5.qcow2 20G
# 初始化磁盘
qemu-img info /kvm_data/centos7.5.qcow2
# 其他参数
--graphics vnc,listen=0.0.0.0,port=9527
--network network=default,model=virtio,mac=52:54:00:82:27:3f
# 热添加或热减少
virsh setvcpus CentOS-7.3-x86_64_1 2 --live
#!/usr/bin/python
# macgen.py script to generate a MAC address for guest virtual machines
#
import random
#
def randomMAC():
	mac = [ 0x00, 0x16, 0x3e,
		random.randint(0x00, 0x7f),
		random.randint(0x00, 0xff),
		random.randint(0x00, 0xff) ]
	return ':'.join(map(lambda x: "%02x" % x, mac))
#
print randomMAC()
# qemu-kvm宿主机和客户机之间的文件共享

# Enable virtio-pci and virtio-net drivers in kernel-rt
# 查看内核驱动
grep -i virtio  /boot/config-`uname -r`

virsh  qemu-monitor-command --hmp server01 "info network"
net0: index=0,type=nic,model=virtio-net-pci,macaddr=52:54:00:82:27:3f
 \ hostnet0: index=0,type=tap,fd=28
cat /boot/config-`uname -r`  |grep -i 9p

modprobe 9pnet
modprobe 9pnet_virtio
lsmod |grep 9p
# 宿主机更改定义
root@kvm:~# virsh edit server01
 ...
 <devices>
   ...
   <filesystem type='mount' accessmode='passthrough'>
     <source dir='/data/shell'/>
     <target dir='hostshare'/>
   </filesystem>
   ...
 </devices>
 ...
Domain kvm1 XML configuration edited.

cat > file.xml<<EOF
   <filesystem type='mount' accessmode='mapped'>
     <source dir='/data/shell2'/>
     <target dir='hostshare'/>
   </filesystem>
EOF



virsh attach-device demo-server file.xml --persistent

error: Failed to attach device from file.xml
error: Operation not supported: live attach of device 'filesystem' is not supported

virsh attach-device --domain demo-server --file file.xml --config
virsh shutdown demo-server
virsh dumpxml demo-server |grep -C2 filesystem
virsh start demo-server

# 在guest中挂载host共享的目录
mkdir -p /tmp/host_files
mount -t 9p -o trans=virtio,rw,version=9p2000.L hostshare /tmp/host_files
# 提示:mount: unknown filesystem type '9p'

# 需要在guest-os的kernel中添加9p的支持:
CONFIG_NET_9P=y
CONFIG_NET_9P_VIRTIO=y
CONFIG_NET_9P_DEBUG=y (Optional)
CONFIG_9P_FS=y
CONFIG_9P_FS_POSIX_ACL=y
# 错误1
[root@localhost host_files]# touch b.sh
touch: cannot touch 'b.sh': Permission denied
# 宿主机目录更改权限
chgrp libvirt-qemu /data/shell
chmod -R g+w /data/shell

# 错误2
[root@localhost host_files]# touch b.sh
touch: setting times of ‘b.sh’: No such file or directory

ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && dpkg-reconfigure --frontend noninteractive tzdata


[root@localhost host_files]# touch b.sh
touch: setting times of ‘b.sh’: No such file or directory
[root@localhost host_files]# mkdir xxx
mkdir: cannot create directory ‘xxx’: Operation not permitted
[root@localhost host_files]# mount | grep host
hostshare on /tmp/host_files type 9p (rw,relatime,sync,dirsync,access=client,trans=virtio)


# 参考
https://askubuntu.com/questions/772784/9p-libvirt-qemu-share-modes
https://unix.stackexchange.com/questions/257372/how-can-i-store-files-in-the-mounted-shared-folder

apt install acl
# sudo setfacl -R -m u:libvirt-qemu:rwx /data/shell
# Change the Mode settings for Filesystem /host from Passthrough to Mapped.



# 从server01克隆到server02
virt-clone \
--original server01 \
--mac 00:16:3e:35:e5:e0 \
--name server02 \
--file /data/kvm/disk/server02_0.img

# kvm磁盘缩小放大
qemu-img info disk/server01_0.img

qemu-img convert -O raw server01_0.img server01_0.raw
apt install kpartx

fdisk -l server01_0.raw
Disk server01_0.raw: 100 GiB, 107374182400 bytes, 209715200 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0009b45a

Device          Boot    Start       End   Sectors Size Id Type
server01_0.raw1 *        2048  10487807  10485760   5G 83 Linux
server01_0.raw2      10487808  14682111   4194304   2G 82 Linux swap / Solaris
server01_0.raw3      14682112 209715199 195033088  93G 83 Linux


parted server01_0.raw
GNU Parted 3.2
Using /data/kvm/disk/server01_0.raw
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model:  (file)
Disk /data/kvm/disk/server01_0.raw: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  5370MB  5369MB  primary  xfs             boot
 2      5370MB  7517MB  2147MB  primary  linux-swap(v1)
 3      7517MB  107GB   99.9GB  primary  xfs


virsh domblklist server01

qemu-img resize /data/kvm/disk/server01_0.raw -2G

WARNING: Image format was not specified for '/data/kvm/disk/server01_0.raw' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
qemu-img: warning: Shrinking an image will delete all data beyond the shrunken image\'s end. Before performing such an operation, make sure there is no important data there.
qemu-img: warning: Using the --shrink option will suppress this message. Note that future versions of qemu-img may refuse to shrink images without this option.
Image resized.

apt install libguestfs-tools
# centos7的xfs不能缩减?
virsh capabilities | xmllint  --xpath /capabilities/host/cpu -
virsh capabilities | xmllint  --xpath /capabilities/host/topology -
virsh dumpxml server01 |xmllint --xpath "string(//domain/devices/interface/mac/@address)" -
qemu-img convert -c -O qcow2 source.qcow2 shrunk.qcow2
## 获取当前运行的虚拟机的mac地址、ip地址
for vm in $(virsh list --state-running --name);
do
  vmmac=`virsh dumpxml $vm |xmllint --xpath "string(//domain/devices/interface/mac/@address)" -`
  vmip=`cat /proc/net/arp | grep $vmmac | awk '{print $1}'`
  echo $vmmac,$vmip,$vm
done
# TightVNC Java Viewer
# https://www.tightvnc.com/download.php
cd tvnjviewer-2.8.3-bin-gnugpl 
java -jar tightvnc-jviewer.jar
# 固定VNC端口
virsh --connect qemu:///system dumpxml server01 | grep 'mac address'
                           <mac address='虚拟机的mac地址'/>
virsh --connect qemu:///system net-list
virsh --connect qemu:///system net-edit default
<ip address='192.168.0.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.0.2' end='192.168.0.254'/>
  <host mac='虚拟机的mac地址' name='虚拟机名' ip='192.168.0.253'/>
</dhcp>
</ip>
virsh --connect qemu:///system net-destroy default && virsh --connect qemu:///system net-start default

## 虚拟机日志
/var/log/libvirt/qemu/
## 查看内核编译参数
cat /boot/config-$(uname -r) | grep VIRT
## 添加讷河模块开机启动
cat > /etc/modules-load.d/virtio-net.conf <<EOF
virtio-net
9pnet
9pnet_virtio
EOF
systemctl cat systemd-modules-load --no-pager
systemctl start systemd-modules-load

#-A INPUT -p udp --dport 67:68 --sport 67:68 -j ACCEPT
ifup eth0
Error: Connection activation failed: IP configuration could not be reserved (no available address, timeout, etc.)

ref