本实验使用两台主机:
10.0.91.8 作为虚拟机的宿主机
10.0.91.10 配置httpd服务,提供10.0.91.8安装虚拟机要使用的镜像及自动应答文件kickstart
主机环境:
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[[email protected] ~]# uname -r
3.10.0-862.11.6.el7.x86_64
10.0.91.8主机上执行如下操作:
1、搭建kvm环境
1.1 linux从内核版本2.6.20之后,默认安装kvm模块,因此不需要安装kvm模块
1.2 安装qemu工具
linux上自带的qemu版本太老,最好是从官方下载最新的源码包编译安装
在QEMU 1.3版本之前,QEMU和QEMU-KVM是有区别的,从2012年底GA的QEMU 1.3版本开始,两者就完全一样
下载页面如下:
https://www.qemu.org/download/
这里下载当前最新版本2.12.0,如果本地事先已经下载源码,直接上传即可
yum install gcc gcc-c++ autoconf automake libtool glib* zlib* pixman* virt-manager virt-install virt-viewer libvirt libvirt-client libvirt-python python-virtinst -y
tar xvf qemu-2.12.0.tar.xz
cd qemu-2.12.0
./configure #默认安装路径为/usr/local
make -j 4 #这里指定编译时使用几个进程,设置为cpu数量
make install
which qemu-system-x86_64
qemu-system-x86_64 -version
为了使用上的便利,创建如下链接:
ln -s /usr/local/bin/qemu-system-x86_64 /usr/bin/qemu-kvm
qemu-kvm -version
systemctl status libvirtd
systemctl start libvirtd
systemctl enable libvirtd
systemctl status libvirtd
chgrp kvm /dev/kvm
sed -n ‘/#user = "root"/s/#user = "root"/user = "root"/p‘ /etc/libvirt/qemu.conf
sed -n ‘/#group = "root"/s/#group = "root"/group = "root"/p‘ /etc/libvirt/qemu.conf
systemctl restart libvirtd
systemctl status libvirtd
这里让虚拟机使用桥接网络(等同于vmare中的桥接)
yum install bridge-utils -y
brctl addbr br0
cat >/etc/sysconfig/network-scripts/ifcfg-br0<<end
STP=yes
BRIDGING_OPTS=priority=32768
TYPE=Bridge
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=10.0.91.8
PREFIX=24
GATEWAY=10.0.91.1
DNS1=1.1.1.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=br0
UUID=1b77d18d-9929-4cd3-8a32-52b858315867
DEVICE=br0
ONBOOT=yes
end
cat >/etc/sysconfig/network-scripts/ifcfg-em1<<end
TYPE=Ethernet
NAME=em1
UUID=dea57870-7fb5-46da-b068-7c007a827e50
DEVICE=em1
ONBOOT=yes
BRIDGE=br0
BRIDGE_UUID=1b77d18d-9929-4cd3-8a32-52b858315867
end
/etc/init.d/network restart
10.0.91.10主机上执行如下操作:
1、安装httpd
yum install httpd -y
systemctl start httpd
systemctl enable httpd
systemctl status httpd
mkdir /var/www/html/{os,ks}
mount /dev/sr0 /var/www/html/os
cat >> /var/www/html/ks/ks.cfg<<end
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
#url --url=http://10.0.91.10/os/
# Use graphical install
#graphical
cmdline
text
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=vda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts=‘us‘
# System language
lang en_US.UTF-8
# Network information
network --bootproto=dhcp --device=eth0 --onboot=on --ipv6=auto
#network --bootproto=static --device=eth0 --gateway=10.0.91.1 --ip=10.0.91.82 --nameserver=114.114.114.114 --netmask=255.255.255.0
network --hostname=test.com
# Root password
#rootpw --iscrypted $6$q4DFSYCTvcXUb14r$J/WTNLpNOlJbeDj3cGNM64oFMZB3sSEarJFbqNeZLK96LGG3snqE/SFMeA8lzRkM5upTaJxQgtR7K1X59dO6O1
rootpw china123
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
#user --groups=wheel --name=nick --password=$6$EOKAkARkDAe5WoLv$0ACn/K0QNrOP2cxnalhy/k1i666EMH/Hjt8R4kS2E48xZ6xTTbfebnCTkoZ40hCPXbUPE7ifdTNoWnxCGAn6s1 --iscrypted --gecos="nick"
user --name=webapp --groups=webapp --homedir=/home/webapp --password=china123 --shell=/usr/bin/bash --uid=1000
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=vda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=vda --size=500
part / --fstype="xfs" --ondisk=vda --grow --size=1
part swap --fstype="swap" --ondisk=vda --recommended
logging --host=10.0.91.83 --port=514 --level=info
zerombr
firewall --disabled
selinux --disabled
reboot
%packages
@^minimal
@compat-libraries
@core
@debugging
@development
kexec-tools
%end
%addon com_redhat_kdump --enable --reserve-mb=‘auto‘
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
end
在浏览器中访问10.0.91.10/os(显示镜像中文件列表)及10.0.91.10/ks/ks.cfg(显示ks.cfg文件内容)
在10.0.91.8上开始安装虚拟机:
mkdir /kvm/os/ -p
qemu-img create -f qcow2 /kvm/os/vm-01.qcow2
virt-install \
--name=vm-01 \
--disk path=/kvm/os/vm-01.qcow2,device=disk,bus=virtio,perms=rw,cache=writethrough \
--graphics none \
--vcpus sockets=2,cores=2,threads=2 \
--ram=16000 \
--location="http://10.0.91.10/os" \
--network bridge=br0 \
--os-type=linux \
--os-variant=rhel7 \
--extra-args="ks=http://10.0.91.10/ks/ks.cfg console=tty0 console=ttyS0,115200n8"
如果要同时安装台,将上述指令包装到for或while循环中放到脚本中,执行脚本即可
脚本示例:
cat >> auto_install.sh<<end
#!/bin/bash
for i in {01..10}
do
qemu-img create -f qcow2 /kvm/os/vm-$i.qcow2
virt-install \
--name=vm-$i \
--disk path=/kvm/os/vm-$i.qcow2,device=disk,bus=virtio,perms=rw,cache=writethrough \
--graphics none \
--vcpus sockets=2,cores=2,threads=2 \
--ram=16000 \
--location="http://10.0.91.10/os" \
--network bridge=br0 \
--os-type=linux \
--os-variant=rhel7 \
--extra-args="ks=http://10.0.91.10/ks/ks.cfg console=tty0 console=ttyS0,115200n8" &
virsh start vm-$i
done
执行脚本:
sh auto_install.sh
因为脚本安装指令中指定了终端,有使用了后台并发执行,这样回导致不能再当前终端安装过程而报错,直接回车,之后使用串口登录即可
[[email protected] ~]# virsh
Welcome to virsh, the virtualization interactive terminal.
Type: ‘help‘ for help with commands
‘quit‘ to quit
virsh # list
Id Name State
----------------------------------------------------
4 vm-01 running
8 vm-02 running
17 vm-03 running
18 vm-04 running
19 vm-05 running
20 vm-06 running
21 vm-07 running
22 vm-08 running
23 vm-09 running
24 vm-10 running
virsh # console 24 #使用串口登录,注意数字24是每台虚拟主机的Id
Connected to domain vm-10
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64
test login: root
Password:
[[email protected] ~]# #可以看到已经通过敞口登录到虚拟机上,在虚拟节点中进行网络及其他优化设置
原文地址:https://www.cnblogs.com/liliyang/p/9685992.html