添加和删除网卡(虚拟硬件)

查看网卡列表

1
sudo virsh domiflist vm-yaohai

添加网卡(模式桥接到br0)

1
sudo virsh attach-interface vm-yaohai --type bridge --source br0 --model virtio --config

删除网卡(指定mac地址删除)

1
sudo virsh detach-interface vm-yaohai --type bridge --mac 52:54:00:61:4c:f3 --config

添加网卡或删除网卡后需要重新定义,不然重启会失效

virsh dumpxml vm-yaohai > /etc/libvirt/qemu/vm-yaohai.xml
virsh define /etc/libvirt/qemu/vm-yaohai.xml

kvm默认网桥

libvirt默认使用了一个名为default的nat网络,这个网络默认使用virbr0作为桥接接口,使用dnsmasq来为使用nat网络的虚拟机提供dns及dhcp服务,dnsmasq生效后的配置文件:/var/lib/libvirt/network/default.xml
当libvirtd服务启动的时候,它会将它管理的被标记为autostart的network一并启动起来,而启动network的时候就会自动调用dnsmasq并赋予其适宜的配置文件来运行服务。

KVM网桥启动

1
2
3
4
5
6
7
8
# default网卡自动启动(virbr0)
virsh net-autostart default

# default网卡启动(virbr0)
virsh net-start default

# 查看网卡列表
virsh net-list --all

修改网段

编辑配置文件

1
sudo virsh net-edit default

重新定义

```bash
sudo virsh net-define /etc/libvirt/qemu/networks/default.xml
```

重新启动网卡

1
2
sudo virsh net-destroy default
sudo virsh net-start default

kickstart自动装机

virt-install命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
virt-install \
--hvm --virt-type kvm \
--accelerate \
--name vm-1 \
--os-type linux \
--os-variant centos-stream8 \
--vcpus 2 \
--ram 4096 \
--disk path=/dev/mapper/lv_name \
--network "bridge:br0,model=virtio" \
--nographics \
--location https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/ \
--initrd-inject=./ks.cfg \
--cpu host \
--extra-args "ks=file:/ks.cfg console=tty0 console=ttyS0,115200n8 net.ifnames=0 biosdevname=0"

后面的net.ifnames=0 biosdevname=0 是内核参数,将网卡设备名固定为eth0..eth1等
–os-type 可以通过命令 osinfo-query os 获取

kickstart 内容

在系统安装后桌面上会生成一份anaconda-ks.cfg这个就是安装这个系统使用的kickstart配置

安装 centos8 streamks.cfg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Kickstart file automatically generated by anaconda.

#version=DEVEL
install
url --url="https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/"
repo --name="CentOS" --baseurl="https://mirrors.aliyun.com/centos/8-stream/BaseOS/x86_64/os/" --cost=100

lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto static --ip 10.0.0.120 --netmask 255.255.0.0 --gateway 10.0.0.8 --noipv6 --nameserver 10.0.0.35,10.0.0.30
network --hostname=vm-name

#default pwd is R/)ydu.?egUdT'Xl
rootpw --iscrypted $6$7ljjIEi8rX8ydAOv$N4qtXrPQZIp5n5pbLf/BQ/1BgE0ODHoXGJzCiWCYl006hOhZeITXl.yz0kpRvolScvAMOZ..XzPIX7iiRtV5v/
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux --enforcing
timezone --utc Asia/Shanghai
text
bootloader --location=mbr --driveorder=vda --append=" crashkernel=auto console=ttyS0,115200n8 net.ifnames=0 biosdevname=0"

# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work

clearpart --all --drives=vda
part /boot --fstype=xfs --size=500
part pv.01 --grow
volgroup vg_os pv.01
logvol / --fstype=xfs --name=lv_root --vgname=vg_os --percent 90

# any invalid partition tables found on disks are initialized

zerombr

%packages
@core
curl

# install puppet agent. But it could be very slow, so you have to install it manually

#%post
#(
# echo 'Install Puppet'
# curl -OL https://yum.puppetlabs.com/puppet6-release-el-7.noarch.rpm
# rpm -i puppet6-release-el-7.noarch.rpm
# yum update -y
# yum install -y puppet-agent
#) 1>> /root/post_install.log 2>&1

%end

kvm透传主机usb设备

1
sudo usermod -aG kvm $USER

kvm修改配置

修改内存大小

1
2
sudo virsh setmaxmem <vm-name> 16G --config
sudo virsh setmem <vm-name> 16G --config