[root@KVM ~]# yum install qemu-kvm qemu-img virt-manager libvirt virt-manager libvirt-client virt-install virt-viewer -y Waiting for process with pid 8674 to finish. Last metadata expiration check: 0:00:05 ago on Sun 08 Jan 2023 06:03:34 AM EST. Package qemu-kvm-15:6.2.0-20.module_el8.7.0+1218+f626c2ff.1.x86_64 is already installed. Package qemu-img-15:6.2.0-20.module_el8.7.0+1218+f626c2ff.1.x86_64 is already installed. Package virt-manager-3.2.0-4.el8.noarch is already installed. Package libvirt-8.0.0-10.module_el8.7.0+1218+f626c2ff.x86_64 is already installed. Package libvirt-client-8.0.0-10.module_el8.7.0+1218+f626c2ff.x86_64 is already installed. Package virt-install-3.2.0-4.el8.noarch is already installed. Package virt-viewer-9.0-11.el8.x86_64 is already installed. Dependencies resolved. Nothing to do. Complete!
启动libvirtd服务
启动服务并设置韦开机自启动,查看状态
1 2 3 4 5 6 7
[root@KVM ~]# systemctl enable --now libvirtd [root@KVM ~]# systemctl status libvirtd ● libvirtd.service - Virtualization daemon Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2023-01-08 06:05:12 EST; 12s ago Docs: man:libvirtd(8) https://libvirt.org
Connection 'br0' (ac3429bc-907c-4ad1-bd54-bbf39d853a53) successfully added. 查看是否创建成功 [root@KVM ~]# nmcli c NAME UUID TYPE DEVICE br0 ac3429bc-907c-4ad1-bd54-bbf39d853a53 bridge br0 ens160 0d45e631-b256-4e08-b8d8-2c42b9481594 ethernet ens160 virbr0 7075d19e-a20c-43da-b161-e7c7519febdb bridge virbr0 网桥创建成功后会自动生成配置文件 [root@KVM ~]# ls -l /etc/sysconfig/network-scripts/ -rw-r--r--. 1 root root 312 Jan 8 06:20 ifcfg-br0 -rw-r--r--. 1 root root 365 Jan 8 05:40 ifcfg-ens160
设置br0和ens160网卡
将br0桥接到ens160
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[root@KVM ~]# vi /etc/sysconfig/network-scripts/ifcfg-br0 BOOTPROTO=static IPADDR=192.168.48.10 GATEWAY=192.168.48.2 PREFIX=24 DNS=114.114.114.114 [root@KVM ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens160 BOOTPROTO=none BRIDGE=br0 [root@KVM ~]# nmcli c reload [root@KVM ~]# nmcli c drow br0r 此时如果你是用ssh工具连着的,你会断开,此时你要去vm那里输入 [root@KVM ~]# nmcli c up br0 [root@KVM ~]# nmcli c up ens160 然后等1分钟左右,就可以连上ssh工具了
1 2 3 4 5 6 7 8 9 10
测试网络连通性 [root@KVM ~]# ping -c 2 jd.com PING jd.com (211.144.24.218) 56(84) bytes of data. 64 bytes from 211.144.24.218 (211.144.24.218): icmp_seq=1 ttl=128 time=50.7 ms 64 bytes from 211.144.24.218 (211.144.24.218): icmp_seq=2 ttl=128 time=51.9 ms
[root@KVM ~]# mkdir -p /opt/iso [root@KVM ~]# chown root:root /opt/iso/ [root@KVM ~]# chmod 777 /opt/iso/ [root@KVM ~]# virsh pool-define-as iso --type dir --target /opt/iso/ Pool iso--type defined #名称为iso的存储池定义成功 如果名字打错 用 virsh pool-destroy [名字] virsh pool-undefine [名字] 删除 [root@KVM ~]# virsh pool-list --all Name State Autostart ------------------------------ iso inactive no
[root@KVM ~]# virsh pool-build iso Pool iso built #创建名为iso的存储池 [root@KVM ~]# virsh pool-start iso Pool iso started #启动iso存储池 [root@KVM ~]# virsh pool-autostart iso Pool iso marked as autostarted #设置iso存储池自启动 查看iso信息 [root@KVM ~]# virsh pool-info iso Name: iso UUID: a966995e-2722-4a3f-a318-e158a642439e State: running Persistent: yes Autostart: yes Capacity: 63.84 GiB Allocation: 2.75 GiB Available: 61.09 GiB
Connected to domain 'Centos7' Please make your choice from above ['q' to quit | 'b' to begin installation | 'r' to refresh]: Please make your choice from above ['q' to quit | 'b' to begin installation | ================================================================================ ================================================================================ Installation
1) [x] Language settings 2) [!] Time settings (English (United States)) (Timezone is not set.) 3) [x] Installation source 4) [x] Software selection (Local media) (Minimal Install) 5) [!] Installation Destination 6) [x] Kdump (No disks selected) (Kdump is enabled) 7) [ ] Network configuration 8) [!] Root password (Not connected) (Password is not set.) 9) [!] User creation (No user will be created) Please make your choice from above ['q' to quit | 'b' to begin installation | 'r' to refresh]: Please make your choice from above ['q' to quit | 'b' to begin installation |
输入2设置时区
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
================================================================================ ================================================================================ Time settings
Timezone: not set
NTP servers:not configured
1) Set timezone 2) Configure NTP servers Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]: ================================================================================ ================================================================================ 按1根据提示选择Asia/shanghai 然后按c
Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]: Autopartitioning Options 再按c ================================================================================ [ ] 1) Replace Existing Linux system(s)
[x] 2) Use All Space
[ ] 3) Use Free Space
Installation requires partitioning of your hard drive. Select what space to use for the install target.
Please make your choice from above ['q' to quit | 'c' to continue | 再按c ================================================================================ Partition Scheme Options
[ ] 1) Standard Partition
[ ] 2) Btrfs
[x] 3) LVM
[ ] 4) LVM Thin Provisioning
Select a partition scheme configuration.
Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]: c Generating updated storage configuration Checking storage configuration...
按8设置root密码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
'r' to refresh]: 8 ================================================================================ (Not connected) 9) [!] User creation ================================================================================ Please select new root password. You will have to type it twice.
'r' to refresh]: b ================================================================================ ================================================================================ Progress Setting up the installation environment . Creating disklabel on /dev/vda . Creating xfs on /dev/vda1 . Creating lvmpv on /dev/vda2 . Creating swap on /dev/mapper/centos-swap . Creating xfs on /dev/mapper/centos-root . Running pre-installation scripts . Starting package installation process 就会开始安装
进入登入界面
1 2 3 4 5
CentOS Linux 7 (Core) Kernel 3.10.0-1160.el7.x86_64 on an x86_64
[root@KVM ~]# virsh dominfo Centos7 Id: 1 Name: Centos7 UUID: 735f1ca1-c82a-4734-81ec-ea3e82e98bae OS Type: hvm State: running CPU(s): 1 CPU time: 17.3s Max memory: 2097152 KiB Used memory: 2097152 KiB Persistent: yes Autostart: enable Managed save: no Security model: selinux Security DOI: 0 Security label: system_u:system_r:svirt_t:s0:c202,c740 (enforcing)
关闭虚拟机
1 2 3 4 5 6
[root@KVM ~]# virsh shutdown Centos7 Domain 'Centos7' is being shutdown [root@KVM ~]# virsh list --all Id Name State -------------------------- - Centos7 shut off
Clone 'C7' created successfully. 克隆成功 [root@KVM ~]# virsh list --all Id Name State -------------------------- - C7 shut off - Centos7 shut off
开启C7,登入并且修改主机名为C7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[root@KVM ~]# virsh start C7 Domain 'C7' started
[root@KVM ~]# virsh console C7 Connected to domain 'C7' Escape character is ^] (Ctrl + ]) CentOS Linux 7 (Core) Kernel 3.10.0-1160.el7.x86_64 on an x86_64
centos7 login: root Password: Last login: Mon Jan 9 15:56:16 from 192.168.48.250 [root@centos7 ~]# hostnamectl set-hostname C7 && bash [root@c7 ~]# 克隆后ip回合Centos7一样,记得后期修改
设置Kvm虚拟机快照
登入C7 创建/opt/dev ,关闭虚拟机,创建快照
1 2 3 4 5 6 7 8 9 10 11 12 13 14
[root@c7 ~]# mkdir /opt/dev [root@c7 ~]# ls /opt dev 按ctrl + ] 退回到KVM宿主机 [root@KVM ~]# virsh shutdown C7 Domain 'C7' is being shutdown 创建快照 [root@KVM ~]# virsh snapshot-create C7 Domain snapshot 1673252946 created 查看C7的快照列表 [root@KVM ~]# virsh snapshot-list C7 Name Creation Time State --------------------------------------------------- 1673252946 2023-01-09 03:29:06 -0500 shutoff
开启C7 删除/opt/dev ,恢复快照
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[root@KVM ~]# virsh start C7 Domain 'C7' started
[root@KVM ~]# virsh console C7 Connected to domain 'C7'
[root@c7 ~]# rm -rf /opt/dev [root@c7 ~]# ll /opt total 0 按ctrl + ] 退回到KVM宿主机 [root@KVM ~]# virsh snapshot-list C7 Name Creation Time State --------------------------------------------------- 1673252946 2023-01-09 03:29:06 -0500 shutoff