centos8 下安装kvm虚拟机及复制虚拟机脚本

kvm虚拟机安装步骤

步骤一: 检查系统配置

测试系统是否支持虚拟机话,不行的话要再bios中设置 虚拟化

[root@localhost ~]# lscpu
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              2
On-line CPU(s) list: 0,1
Thread(s) per core:  1
Core(s) per socket:  2
Socket(s):           1
NUMA node(s):        1
Vendor ID:           AuthenticAMD
CPU family:          16
Model:               4
Model name:          AMD Phenom(tm) II X2 B57 Processor
Stepping:            3
CPU MHz:             3200.000
CPU max MHz:         3200.0000
CPU min MHz:         800.0000
BogoMIPS:            6429.41
Virtualization:      AMD-V
L1d cache:           64K
L1i cache:           64K
L2 cache:            512K
L3 cache:            6144K
NUMA node0 CPU(s):   0,1
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt hw_pstate vmmcall npt lbrv svm_lock nrip_save

可以看到 本机器支持虚拟化  Virtualization:      AMD-V

如果没有相应的输出,就需要到bios设定或者是系统不支持。

确认有无kvm模块

[root@localhost ~]#  modprobe kvm
[root@localhost ~]# lsmod |grep kvm
kvm_amd               110592  0
ccp                    98304  1 kvm_amd
kvm                   786432  1 kvm_amd
irqbypass              16384  1 kvm

步骤二: 安装支撑包

确认系统已经安装好yum,网络通畅。

然后安装kvm虚拟机的安装包

# yum install qemu-kvm libvirt virt-install -y

步骤三:安装centos虚拟机,我们采用centos7.8 minimal镜像,命令行启动

virt-install --name=centos7 --memory=512,maxmemory=1980  --vcpus=2,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/root/CentOS-7-x86_64-DVD-1708.iso --disk path=/vms/centos7.qcow2,size=80 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args=“console=tty0 console=ttyS0”

错误信息如下:

[root@localhost ~]# virt-install --name=centos7 --memory=1980,maxmemory=1980  --vcpus=2,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/root/CentOS-7-x86_64-Minimal-2003.iso --disk path=/vms/centos7.qcow2,size=80 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
WARNING  The requested volume capacity will exceed the available pool space when the volume is fully allocated. (81920 M requested capacity > 47670 M available)
WARNING  OS name 'rhel7' is deprecated, using 'rhel7.0' instead. This alias will be removed in the future.
WARNING  /root/CentOS-7-x86_64-Minimal-2003.iso may not be accessible by the hypervisor. You will need to grant the 'qemu' user search permissions for the following directories: ['/root']
ERROR    The requested volume capacity will exceed the available pool space when the volume is fully allocated. (81920 M requested capacity > 47670 M available) (Use --check disk_size=off or --check all=off to override)

 这里出现了2个错误,一个是space空间问题,一个是qemu user的问题

解决:

增加 --connect参数

[root@localhost ~]# virt-install --name=centos7 --memory=1980,maxmemory=1980  --vcpus=2,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/root/CentOS-7-x86_64-Minimal-2003.iso --disk path=/vms/centos7.qcow2,size=80 --connect qemu:///system  --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
WARNING  The requested volume capacity will exceed the available pool space when the volume is fully allocated. (81920 M requested capacity > 47670 M available)
WARNING  OS name 'rhel7' is deprecated, using 'rhel7.0' instead. This alias will be removed in the future.
WARNING  /root/CentOS-7-x86_64-Minimal-2003.iso may not be accessible by the hypervisor. You will need to grant the 'qemu' user search permissions for the following directories: ['/root']
ERROR    The requested volume capacity will exceed the available pool space when the volume is fully allocated. (81920 M requested capacity > 47670 M available) (Use --check disk_size=off or --check all=off to override)

 将虚拟机的硬盘大小调整到40G

[root@localhost ~]# virt-install --name=centos7 --memory=1980,maxmemory=1980  --vcpus=2,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/root/CentOS-7-x86_64-Minimal-2003.iso --disk path=/vms/centos7.qcow2,size=40 --connect qemu:///system  --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
WARNING  OS name 'rhel7' is deprecated, using 'rhel7.0' instead. This alias will be removed in the future.
WARNING  /root/CentOS-7-x86_64-Minimal-2003.iso may not be accessible by the hypervisor. You will need to grant the 'qemu' user search permissions for the following directories: ['/root']
WARNING  /root/CentOS-7-x86_64-Minimal-2003.iso may not be accessible by the hypervisor. You will need to grant the 'qemu' user search permissions for the following directories: ['/root']

Starting install...
Retrieving file vmlinuz...                                                                                                                      | 6.4 MB  00:00:00     
Retrieving file initrd.img...                                                                                                                   |  53 MB  00:00:00     
Allocating 'centos7.qcow2'                                                                                                                      |  40 GB  00:00:05     
ERROR    Cannot get interface MTU on 'br0': No such device
Removing disk 'centos7.qcow2'                                                                                                                   |    0 B  00:00:00     
Domain installation does not appear to have been successful.
If it was, you can restart your domain by running:
  virsh --connect qemu:///system start centos7
otherwise, please restart your installation.

没有发现网桥 br0,使用ifconfig查看

[root@localhost ~]# ifconfig  |grep br0
virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500

 名字应该是virbr0

继续修改

[root@localhost ~]# mkdir -p /home/kvm
[root@localhost ~]# mv Cent* /home/kvm
[root@localhost ~]#chown -R qemu:qemu /home/kvm
[root@localhost ~]#virt-install --name=centos7 --memory=1980,maxmemory=1980  --vcpus=2,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/home/kvm/CentOS-7-x86_64-Minimal-2003.iso --disk path=/vms/centos7.qcow2,size=40 --connect qemu:///system  --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"

安装开始

Starting installer, one moment...
anaconda 21.48.22.158-1 for CentOS 7 started.
 * installation log files are stored in /tmp during the installation
 * shell is available on TTY2
 * when reporting a bug add logs from /tmp as separate text/plain attachments
11:50:35 Not asking for VNC because we don't have a network
================================================================================
================================================================================
Installation

 1) [x] Language settings                 2) [!] Time settings
        (English (United States))                (Timezone is not set.)
 3) [!] Installation source               4) [!] Software selection
        (Processing...)                          (Processing...)
 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]: 

[anaconda] 1:main* 2:shell  3:log  4:storage-lo> Switch tab: Alt+Tab | Help: F1 






最后提供一个复制虚拟机的脚本给大家

#!/bin/bash
SOURCE_VM=centos7
VM_QCOW2_PATH=/vms
NAME_PRIFIX=centos7
[ $# -lt 2 ] && echo "usage: $0 from to" && exit 1
for i in `seq -s ' ' $1 $2 `
do
virt-clone -o $SOURCE_VM -n "${SOURCE_VM}V$i" -f "${VM_QCOW2_PATH}/${NAME_PRIFIX}V${i}.qcow2"
done

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是在 CentOS 7 上安装 KVM 虚拟机的步骤: 1. 确认 CPU 支持虚拟化技术: ``` # egrep -c '(svm|vmx)' /proc/cpuinfo ``` 如果返回值大于 0,说明 CPU 支持虚拟化技术。 2. 安装 KVM 及相关组件: ``` # yum install qemu-kvm libvirt virt-install bridge-utils ``` 3. 启动 libvirtd 服务: ``` # systemctl start libvirtd ``` 4. 设置 libvirtd 开机自启: ``` # systemctl enable libvirtd ``` 5. 创建网络桥接: ``` # vi /etc/sysconfig/network-scripts/ifcfg-br0 ``` 将以下内容添加到文件中: ``` DEVICE=br0 TYPE=Bridge BOOTPROTO=static IPADDR=192.168.0.100 # 桥接网络的 IP 地址 NETMASK=255.255.255.0 # 子网掩码 GATEWAY=192.168.0.1 # 网关 IP 地址 DNS1=192.168.0.1 # DNS 服务器 IP 地址 ONBOOT=yes ``` 保存并退出文件。 6. 编辑物理网卡配置文件: ``` # vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 # 根据实际网卡名修改 ``` 将以下内容添加到文件中: ``` DEVICE=enp0s3 TYPE=Ethernet ONBOOT=yes BRIDGE=br0 ``` 保存并退出文件。 7. 重启网络服务: ``` # systemctl restart network ``` 8. 创建虚拟机: ``` # virt-install --name centos7 --ram 2048 --vcpus 2 --disk path=/var/lib/libvirt/images/centos7.img,size=20 --os-type linux --os-variant rhel7 --network bridge=br0 --graphics none --console pty,target_type=serial ``` 其中,--name 表示虚拟机名称,--ram 表示内存大小,--vcpus 表示虚拟 CPU 数量,--disk 表示虚拟磁盘路径和大小,--os-type 和 --os-variant 表示操作系统类型和版本,--network 表示网络配置,--graphics none 表示不使用图形界面,--console pty,target_type=serial 表示使用串口控制台。 9. 安装操作系统: 按照正常操作系统安装流程安装操作系统即可。 以上是在 CentOS 7 上安装 KVM 虚拟机的步骤,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老骥又出发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值