1. 基础环境设置
#更新环境
apt update
apt upgrade
#配置网卡
cat >/etc/netplan/br.yml<<EOF
network:
ethernets:
eth2: {}
bridges:
br0:
interfaces:
- eth2
addresses:
- 192.192.1.213/24
gateway4: 192.192.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
EOF
#安装组件
apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager virt-viewer
systemctl enable libvirtd && systemctl start libvirtd
2.设置 虚拟机网络
root@test-kvm-sam:~# virsh net-list
Name State Autostart Persistent
----------------------------------------
root@test-kvm-sam:~#
#创建网络模板
vi /etc/libvirt/qemu/networks/br0.xml
<network>
<name>virt_br0</name>
<uuid>a1247b77-a308-4567-a60d-d61b79264a0f</uuid>
<forward mode='bridge'/>
<bridge name='br0'/>
</network>
#定义
root@test-kvm-sam:~# virsh net-define /etc/libvirt/qemu/networks/br0.xml
Network virt_br0 defined from /etc/libvirt/qemu/networks/br0.xml
root@test-kvm-sam:/etc/libvirt/qemu/networks# sudo virsh net-start virt_br0
Network virt_br0 started
root@test-kvm-sam:/etc/libvirt/qemu/networks# sudo virsh net-autostart virt_br0
Network virt_br0 marked as autostarted
3.创建磁盘
root@test-kvm-sam:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 63.5M 1 loop /snap/core20/2015
loop1 7:1 0 91.9M 1 loop /snap/lxd/24061
loop2 7:2 0 40.9M 1 loop /snap/snapd/20290
sda 8:0 0 3.5T 0 disk
sdb 8:16 0 3.5T 0 disk
sdc 8:32 0 3.5T 0 disk
sdd 8:48 0 3.5T 0 disk
sde 8:64 0 446.6G 0 disk
├─sde1 8:65 0 8M 0 part
├─sde2 8:66 0 1G 0 part /boot
├─sde3 8:67 0 445.6G 0 part /
└─sde4 8:68 0 65M 0 part
#pg
root@test-kvm-sam:~# pvcreate kvm_pv /dev/sdd
Device kvm_pv not found.
Physical volume "/dev/sdd" successfully created.
#vg
root@test-kvm-sam:~# vgcreate kvm_vg /dev/sdd
Volume group "kvm_vg" successfully created
#lv
root@test-kvm-sam:~# lvcreate -L 1000G -n kvm_lv kvm_vg
Logical volume "kvm_lv" created.
root@test-kvm-sam:~# lvcreate -L 1000G -n kvm_lv2 kvm_vg
Logical volume "kvm_lv2" created.
root@test-kvm-sam:~# lvcreate -L 1000G -n kvm_lv3 kvm_vg
Logical volume "kvm_lv3" created.
#确认
root@test-kvm-sam:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 63.5M 1 loop /snap/core20/2015
loop1 7:1 0 91.9M 1 loop /snap/lxd/24061
loop2 7:2 0 40.9M 1 loop /snap/snapd/20290
sda 8:0 0 3.5T 0 disk
sdb 8:16 0 3.5T 0 disk
sdc 8:32 0 3.5T 0 disk
sdd 8:48 0 3.5T 0 disk
├─kvm_vg-kvm_lv 253:0 0 1000G 0 lvm /kvm_data
├─kvm_vg-kvm_lv2 253:1 0 1000G 0 lvm
└─kvm_vg-kvm_lv3 253:2 0 1000G 0 lvm
sde 8:64 0 446.6G 0 disk
├─sde1 8:65 0 8M 0 part
├─sde2 8:66 0 1G 0 part /boot
├─sde3 8:67 0 445.6G 0 part /
└─sde4 8:68 0 65M 0 part
#格式化
root@test-kvm-sam:~# mkfs.xfs /dev/kvm_vg/kvm_lv
meta-data=/dev/kvm_vg/kvm_lv isize=512 agcount=4, agsize=65536000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=262144000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=128000, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
#挂载
mkdir /kvm_data
#blkid 确认uuid
/dev/mapper/kvm_vg-kvm_lv: UUID="fb699261-1b85-4edd-bf1f-22cfdde2ded6" TYPE="xfs"
#写到开机挂载
root@test-kvm-sam:~# cat /etc/fstab
LABEL=rootfs / ext4 defaults 0 1
LABEL=mkfs_boot /boot ext4 defaults 0 1
UUID="fb699261-1b85-4edd-bf1f-22cfdde2ded6" /kvm_data xfs defaults 0 1
#挂载
mount -a
df -h
root@test-kvm-sam:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 252G 0 252G 0% /dev
tmpfs 51G 2.8M 51G 1% /run
/dev/sde3 418G 2.8G 398G 1% /
tmpfs 252G 0 252G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 252G 0 252G 0% /sys/fs/cgroup
/dev/loop0 64M 64M 0 100% /snap/core20/2015
/dev/loop1 92M 92M 0 100% /snap/lxd/24061
/dev/loop2 41M 41M 0 100% /snap/snapd/20290
/dev/sde2 974M 209M 698M 24% /boot
tmpfs 51G 0 51G 0% /run/user/0
none 252G 164K 252G 1% /run/qemu
/dev/mapper/kvm_vg-kvm_lv 1000G 7.1G 993G 1% /kvm_data
4.创建虚拟机
#下载centos7.9
wget https://mirrors.cqu.edu.cn/CentOS/7.9.2009/isos/x86_64/CentOS-7-x86_64-Minimal-2009.iso
root@test-kvm-sam:~# cp cCentOS-7-x86_64-Minimal-2009.iso /kvm_data/
root@test-kvm-sam:~# cd /kvm_data
#查看支持的类型
virt-install --os-variant list
创建数据盘
root@test-kvm-sam:~# qemu-img create -f qcow2 /kvm_data/testcentos_data7.9.qcow2 200G
Formatting '/kvm_data/testcentos_data7.9.qcow2', fmt=qcow2 size=214748364800 cluster_size=65536 lazy_refcounts=off refcount_bits=16
#通过iso
```
root@test-kvm-sam:~# virt-install --virt-type kvm --name testcentos7.9 --ram 8192 --vcpus 8 \
--cdrom=/kvm_data/CentOS-7-x86_64-Minimal-2009.iso \
--disk /kvm_data/testcentos_data7.9.qcow2,format=qcow2 \
--network bridge=br0 \
--graphics vnc,listen=0.0.0.0 --noautoconsole \
--os-type=linux --os-variant=centos7.0
Starting install...
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
root@test-kvm-sam:~#
````
#查看vnc 信息
virsh vncdisplay testcentos7.9
#用vnc 客户端登录安装
5.配置IP 测试
ip addr add 192.192.1.214/24 dev eth0
ip route add default via 192.192.1.1 dev eth0
cat >/etc/resolv.conf<<EOF
nameserver 8.8.8.8
EOF
ping gogole.com