长春网站制作建设,政务网站建设云计算中心,兼职做网站平台,数码网站建设的规模与类别文章目录 安装KVM开启cpu虚拟化安装KVM检查环境是否正常 KVM图形化创建虚拟机上传ISO创建虚拟机加载镜像配置内存添加磁盘能否手工指定存储路径呢#xff1f;创建成功安装完成查看虚拟机 KVM命令行创建虚拟机创建磁盘通过命令行创建虚拟机手动安装虚拟机 KVM命令行创建虚拟机-… 文章目录 安装KVM开启cpu虚拟化安装KVM检查环境是否正常 KVM图形化创建虚拟机上传ISO创建虚拟机加载镜像配置内存添加磁盘能否手工指定存储路径呢创建成功安装完成查看虚拟机 KVM命令行创建虚拟机创建磁盘通过命令行创建虚拟机手动安装虚拟机 KVM命令行创建虚拟机-kickstart全自动安装使用Centos7生成cfg文件使用Centos8自动安装测试 KVM基本命令总结 安装KVM
开启cpu虚拟化 注意务必开启虚拟机CPU虚拟化否则无法嵌套虚拟机 不开启后期报错 安装KVM 配置yum源 --》安装KVM [rootcloud ~]# mount /dev/cdrom /mnt/
mount: /mnt: WARNING: device write-protected, mounted read-only.
[rootcloud ~]# vim /etc/yum.repos.d/
[rootcloud ~]# vim /etc/yum.repos.d/
bak/ cloud.repo
[rootcloud ~]# vim /etc/yum.repos.d/cloud.repo
[rootcloud ~]# yum clean all
14 files removed
[rootcloud ~]# yum repolist all
repo id repo name status
AppStream AppStream enabled
BaseOS BaseOS enabled
[rootcloud ~]# yum grouplist
AppStream 316 MB/s | 5.7 MB 00:00
BaseOS 455 MB/s | 2.2 MB 00:00
Available Environment Groups:ServerMinimal InstallWorkstationCustom Operating System
Installed Environment Groups:Server with GUIVirtualization Host
Installed Groups:Container ManagementHeadless Management
Available Groups:.NET Core DevelopmentRPM Development ToolsDevelopment ToolsGraphical Administration ToolsLegacy UNIX CompatibilityNetwork ServersScientific SupportSecurity ToolsSmart Card SupportSystem Tools[rootcloud ~]# yum groupinstall -y Virtualization* ### 安装的软件包组
Last metadata expiration check: 0:01:01 ago on Sat 05 Aug 2023 03:08:26 PM CST.
No match for group package insights-client
No match for group package centos-release-eula
Dependencies resolved.
Package Arch Version Repo SizeInstalling group/module packages:virt-install noarch 2.2.1-3.el8 AppStream 99 kvirt-top x86_64 1.0.8-32.el8 AppStream 729 kvirt-viewer x86_64 7.0-9.el8 AppStream 397 kvirt-who noarch 0.27.6-1.el8 AppStream 247 k
Installing dependencies:python3-suds noarch 0.7-0.8.94664ddd46a6.el8 AppStream 242 k
Installing Environment Groups:Virtualization HostInstalling Groups:Virtualization HypervisorVirtualization PlatformVirtualization ClientVirtualization ToolsBase Core Standard Transaction SummaryInstall 5 PackagesTotal size: 1.7 M
Installed size: 6.2 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionPreparing : 1/1 Installing : python3-suds-0.7-0.8.94664ddd46a6. 1/5 Installing : virt-who-0.27.6-1.el8.noarch 2/5 Running scriptlet: virt-who-0.27.6-1.el8.noarch 2/5 Installing : virt-viewer-7.0-9.el8.x86_64 3/5 Installing : virt-top-1.0.8-32.el8.x86_64 4/5 Installing : virt-install-2.2.1-3.el8.noarch 5/5 Running scriptlet: virt-install-2.2.1-3.el8.noarch 5/5 Verifying : python3-suds-0.7-0.8.94664ddd46a6. 1/5 Verifying : virt-install-2.2.1-3.el8.noarch 2/5 Verifying : virt-top-1.0.8-32.el8.x86_64 3/5 Verifying : virt-viewer-7.0-9.el8.x86_64 4/5 Verifying : virt-who-0.27.6-1.el8.noarch 5/5
Installed products updated.Installed:python3-suds-0.7-0.8.94664ddd46a6.el8.noarch virt-install-2.2.1-3.el8.noarch virt-top-1.0.8-32.el8.x86_64 virt-viewer-7.0-9.el8.x86_64 virt-who-0.27.6-1.el8.noarch Complete!
可以通过桌面化图标打开或者命令行 [rootcloud ~]# virt-manager 检查环境是否正常 [rootcloud ~]# ifconfig
ens160: flags4163UP,BROADCAST,RUNNING,MULTICAST mtu 1500inet 192.168.129.141 netmask 255.255.255.0 broadcast 192.168.129.255inet6 fe80::4b45:520e:3195:48b4 prefixlen 64 scopeid 0x20linkether 00:0c:29:9b:91:4d txqueuelen 1000 (Ethernet)RX packets 339 bytes 60656 (59.2 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 201 bytes 23207 (22.6 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags73UP,LOOPBACK,RUNNING mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10hostloop txqueuelen 1000 (Local Loopback)RX packets 48 bytes 4080 (3.9 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 48 bytes 4080 (3.9 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
### 有正常
virbr0: flags4099UP,BROADCAST,MULTICAST mtu 1500inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255ether 52:54:00:bd:e4:f0 txqueuelen 1000 (Ethernet)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
KVM图形化创建虚拟机
上传ISO 创建虚拟机 加载镜像 配置内存 添加磁盘 能否手工指定存储路径呢
在指定的路径下首先创建虚拟机所用的磁盘 添加物理磁盘 -- 分区 -- 格式化 -- 挂载 -- 创建磁盘 [rootcloud /]# fdisk /dev/nvme0n2Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Command (m for help): n
Partition typep primary (0 primary, 0 extended, 4 free)e extended (container for logical partitions)
Select (default p): Using default response p.
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, sectors or size{K,M,G,T,P} (2048-41943039, default 41943039): Created a new partition 1 of type Linux and of size 20 GiB.
Partition #1 contains a LVM2_member signature.Do you want to remove the signature? [Y]es/[N]o: p
Do you want to remove the signature? [Y]es/[N]o: yThe signature will be removed by a write command.Command (m for help): p
Disk /dev/nvme0n2: 20 GiB, 21474836480 bytes, 41943040 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: 0x87cd3c01Device Boot Start End Sectors Size Id Type
/dev/nvme0n2p1 2048 41943039 41940992 20G 83 LinuxFilesystem/RAID signature on partition 1 will be wiped.Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.[rootcloud /]# mkfs.xfs /dev/nvme0n2p1
meta-data/dev/nvme0n2p1 isize512 agcount4, agsize1310656 blks sectsz512 attr2, projid32bit1 crc1 finobt1, sparse1, rmapbt0 reflink1
data bsize4096 blocks5242624, imaxpct25 sunit0 swidth0 blks
naming version 2 bsize4096 ascii-ci0, ftype1
log internal log bsize4096 blocks2560, version2 sectsz512 sunit0 blks, lazy-count1
realtime none extsz4096 blocks0, rtextents0[rootcloud /]# mkdir /kvm
[rootcloud /]# cd kvm/
### qcow2为磁盘格式
[rootcloud kvm]# qemu-img create -f qcow2 /kvm/centos1.qcow2 20g
Formatting /kvm/centos1.qcow2, fmtqcow2 size21474836480 cluster_size65536 lazy_refcountsoff refcount_bits16
[rootcloud kvm]# ls
centos1.qcow2 创建成功 安装完成 查看虚拟机
[rootcloud ~]# virsh list Id Name State
----------------------------------------------------3 centos7.0-2 runningKVM命令行创建虚拟机
创建磁盘
[rootcloud kvm]# qemu-img create -f qcow2 /kvm/os1.qcow2 20g
Formatting /kvm/os1.qcow2, fmtqcow2 size21474836480 cluster_size65536 lazy_refcountsoff refcount_bits16[rootcloud kvm]# ls
os1.qcow2### 之后通过图形化创建虚拟机的时候可以选择刚才手工创建的存储磁盘 centos701.qcow2
通过命令行创建虚拟机
### 其实就是把前面图形化点击的步骤转换成了命令行操作。
[rootcloud kvm]# virt-install \--name centos702 \--memory 2048 \--vcpus 1 \--disk path/kvm/os1.qcow2 \--location /data/CentOS-7-x86_64-DVD-2009.iso \--network networkdefault \--noautoconsoleStarting install...
Retrieving file vmlinuz... | 6.5 MB 00:00:00
Retrieving file initrd.img... | 53 MB 00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.### 创建成功
[rootcloud kvm]# virsh listId Name State
----------------------------------------------------3 centos7.0-2 running4 centos702 running
手动安装虚拟机 KVM命令行创建虚拟机-kickstart全自动安装 注意CentOS 8 版本是没有 kickstart 工具的可以使用 CentOS 7 版本生成配置文件给 8 版本使用。 使用Centos7生成cfg文件 yum install -y system-config-kickstart.noarch --安装工具system-config-kickstart --启动工具注意禁用udev命名规则 net.ifnames0 biosdevname0 进行配置最后导出 xxx.cfg 文件 yum install -y httpd --安装apachesystemctl start httpd --启动apache服务注意关闭防火墙将工具生成好的配置文件保存。 ### 关闭防火墙
[rootcloud html]# systemctl stop firewalld.service ### 临时关闭
[rootcloud html]# systemctl disable firewalld.service ### 禁止启用
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.[rootcloud ~]# yum install -y httpd ### 安装httpd
[rootcloud ~]# systemctl start httpd ### 开启服务
[rootcloud ~]# cd /var/www/html/ ### 开启服务后会自动生成目录 /var/www/html/
[rootcloud html]# mv /data/ks01.cfg . ### 将centos7通过工具生成的cfg文件放到html目录下
[rootcloud html]# ls
ks01.cfg### 使用虚拟机 ip 目录文件 直接访问
# 例如 http://192.168.129.141/ks01.cfg使用Centos8自动安装
### 创建磁盘
[rootcloud html]# qemu-img create -f qcow2 /kvm/centos703.qcow2 20g
Formatting /kvm/centos703.qcow2, fmtqcow2 size21474836480 cluster_size65536 lazy_refcountsoff refcount_bits16[rootcloud html]# ls /kvm/
centos703.qcow2 os1.qcow2### 如果有错误会在安装过程中卡住
[rootcloud ~]# virt-install \--name centos703 \ ### 虚拟机名称--memory 2048 \ ### 内存--vcpus 1 \ ### cpu--disk path/kvm/centos703.qcow2 \ ### 磁盘--location /data/CentOS-7-x86_64-DVD-2009.iso \ ### 镜像--network networkdefault \ ### 网络--noautoconsole \-x kshttp://192.168.129.141/ks/ks01.cfg ### 也可以使用本地文件 ks/data/ks01.cfgStarting install...
Retrieving file vmlinuz... | 6.5 MB 00:00:00
Retrieving file initrd.img... | 53 MB 00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.### 使用本地文件二选其一即可
[rootcloud ~]# virt-install --name centos703 --memory 2048 --vcpus 1 --disk path/kvm/centos703.qcow2 --location /data/CentOS-7-x86_64-DVD-2009.iso --network networkdefault --noautoconsole -x ks/data/ks01.cfg#### 之后即可使用图形化界面进行查看自动化安装有个问题虽然是通过命令行可以进行全自动安装但是因为有图形化界面所以便于观察虚拟机的具体信息。如果没有图形化界面呢安装过程及安装完成后根本不知道虚拟机的ip地址也不知道进行到哪一步了这时候怎么办 在kickstart文件中添加 consolettyS0 bootloader --append“net.ifnames0 biosdevname0 consolettyS0” --locationmbr 并通过 virsh console xxx 方式连接到虚拟机里面相当于插上了一根console线。 ### 添加 consolettyS0
[rootcloud ~]# vim /data/ks01.cfg
...
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
# bootloader --appendnet.ifnames0 biosdevname0 --locationmbr
bootloader --appendnet.ifnames0 biosdevname0 consolettyS0 --locationmbr
# Clear the Master Boot Record
zerombr
....[rootcloud ~]# qemu-img create -f qcow2 /kvm/os333.qcow2 20g[rootcloud ~]# virt-install --name centos705 --memory 2048 --vcpus 1 --disk path/kvm/os333.qcow2 --location /data/CentOS-7-x86_64-DVD-2009.iso --network networkdefault --noautoconsole -x ks/data/ks01.cfgStarting install...
Retrieving file vmlinuz... | 6.5 MB 00:00:00
Retrieving file initrd.img... | 53 MB 00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.[rootcloud ~]# virsh console centos705
Connected to domain centos705
Escape character is ^]CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64localhost login: root
Password: ### ctrl ] 退出如果安装文件中没有加入也可在后续虚拟机中加入
[root localhost ]# cd /etc/def auu lt/
[root localhost default]# ls
grub nss useradd
[root localhost default]# vi grub a
GRUB TIMEOUT5
GRUB DISTRIBUTORS(sed s, release .*5 ,g’/etc/system-re lease )
GRUB DEFAULT-saved
GRUB DISABLE SUBMENUtrue
GRUB_ TERMINAL OUTPUTconso le
# GRUB CMDL INE L INUX crashkerne lauto rd . lum. lucentos/root rd. lum. lucentos/swap rhgb quiet
### 加入到此处 consolettyS0
GRUB CMDL INE L INUX crashkerne lauto rd . lum. lucentos/root rd. lum. lucentos/swap rhgb quiet consolettyS0
GRUB_ DISABLE RECOUERYtrue[root localhost default]# grub2-mkconfig -o /boot/grub2/grub.cfg
[root localhost default]# reboot测试
[rootcloud ~]# virsh console centos7.0
Connected to domain centos7.0
Escape character is ^]CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64localhost login: root
Password:
Last login: Sat Aug 5 06:42:12 on tty1
[rootlocalhost ~]#
KVM基本命令总结
[rootcloud ~]# virsh --help 查看帮助
[rootcloud ~]# virsh list --all 列出所有虚拟机包括正在运行的和关闭的
[rootcloud ~]# virsh list 列出所有虚拟机仅运行中的
[rootcloud ~]# virsh start centos705 --启动虚拟机
[rootcloud ~]# virsh shutdown centos705 -- 关闭虚拟机
[rootcloud ~]# virsh reboot centos705 --重启虚拟机### 如果虚拟机关不了强制关闭非正常关闭
[rootcloud ~]# virsh destroy centos705
### 清除配置
[rootcloud ~]# virsh undefine centos705# 移除软件包
[rootlocalhost ~]# yum groupremove -y Virtualization*
[rootlocalhost ~]# rpm -qa | grep virtualization
[rootlocalhost ~]# systemctl list-units | grep virtualization
[rootlocalhost ~]# lsmod | grep virtualization