虚拟机KVM自动安装CentOS7

一、软件环境

宿主机版本如下:

>[root@xycto ~]# cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)
[root@xycto ~]# uname -rp
3.10.0-1062.1.1.el7.x86_64 x86_64
[root@xycto ~]# rpm -q qemu-kvm
qemu-kvm-1.5.3-167.el7_7.1.x86_64

之前在虚拟机已安装了一个RHEL7的版本。

安装virt-install
[root@xycto Packages]# rpm -qpl virt-install-1.5.0-1.el7.noarch.rpm
/usr/bin/virt-clone
/usr/bin/virt-install
/usr/bin/virt-xml
[root@xycto Packages]# yum install virt-install
[root@xycto Packages]# rpm -q virt-install
virt-install-1.5.0-7.el7.noarch

准备安装文件

方法1:http提供安装源

启动web服务
[root@xycto html]# systemctl start httpd
[root@xycto html]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2019-09-22 10:49:47 CST; 5s ago
  查看虚拟机所用网段
# ip ad
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:fe:6f:76 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
挂载光盘
# mkdir /var/www/html/inst
# mount /home/xy/CentOS-7-x86_64-DVD-1810.iso	/var/www/html/inst

通过浏览器输入 http://192.168.122.1/inst
此时应该能够正常打开网页,并显示目录结构。

方法2:ftp提供安装源

启动ftp服务
[root@xycto html]# systemctl start vsftpd
[root@xycto html]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2019-09-22 10:58:50 CST; 4s ago
挂载光盘
# mkdir /var/ftp/pub/centos7
即使提前修改属主,挂载后仍然是root权限,目录和文件都是可读的。
# mount /home/xy/CentOS-7-x86_64-DVD-1810.iso	/var/ftp/pub/centos7
[root@xycto pub]# ll
total 2
drwxrwxr-x. 8 root root 2048 Nov 26  2018 centos7
drwxr-xr-x. 2 ftp  ftp    18 Sep 21 21:26 updir
而且无法修改
[root@xycto pub]# chown ftp.ftp /var/ftp/pub/centos7
chown: changing ownership of ‘/var/ftp/pub/centos7’: Read-only file system
[root@xycto pub]# mount -o remount,rw /var/ftp/pub/centos7
mount: cannot remount /dev/loop1 read-write, is write-protected

实际验证ftp://192.168.122.1/能够显示pub目录和下面的其他目录文件,但无法显示inst目录。
尝试 chcon -R --reference=/var/ftp/pub /var/ftp/pub/centos7失败,因为iso文件挂载后是只读的。
可能的方法,拷贝过来再执行 cp -a /var/ftp/pub/centos7 /var/ftp/pub/inst

方法3:新增一个raw硬盘,放好ks文件。

[root@xycto ~]# cd /var/lib/libvirt/images
[root@xycto images]# qemu-img create -f qcow2 hd4ks.qcow2 200M
这种格式只能在kvm中使用,不方便拷入文件,练习一下转换(如果格式错误会提示)
[root@xycto images]# qemu-img convert -f qcow2 -O raw hd4ks.qcow2 hd4ks.raw
[root@xycto images]# qemu-img info hd4ks.raw
image: hd4ks.raw
file format: raw
virtual size: 200M (209715200 bytes)
disk size: 0
[root@xycto images]# mkfs.ext3 hd4ks.raw
mke2fs 1.42.9 (28-Dec-2013)
hd4ks.raw is not a block special device.
Proceed anyway? (y,n) y

[root@xycto images]# ls /mnt/tmp
[root@xycto images]# mount /var/lib/libvirt/images/hd4ks.raw /mnt/tmp
[root@xycto images]# cp /root/ks1.cfg /mnt/tmp/
[root@xycto images]# umount /mnt/tmp

准备自动安装脚本

方法1:可以从已安装的rhel或者centos获取文件/root/anaconda-ks.cfg,我采用的这个方法,修改后的几个参数

#cdrom
url --url http://192.168.122.1/inst
firstboot --disable

拷贝到www的目录下,需要在html目录下新建子目录

[root@xycto pub]# cd /var/www/html/
[root@xycto html]# ll
total 2
drwxrwxr-x. 8 root root 2048 Nov 26  2018 inst
drwxr-xr-x. 2 root root   25 Sep 22 10:23 my
[root@xycto html]# ls -Z
drwxrwxr-x. root root system_u:object_r:iso9660_t:s0   inst
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 my
[root@xycto html]# ll my
total 4
-rw-r--r--. 1 xy xy 1832 Sep 21 22:37 ks-rhel.cfg

方法2:使用kickstart工具生成。

# rpm -q system-config-kickstart
system-config-kickstart-2.9.7-1.el7.noarch

这种方法生成的文件可能不能自动识别,建议采用方法1。

二、kvm图形界面新建虚拟机自动安装

利用virt-manager先新建一个虚拟机,挂载iso文件,启动虚拟机,当提示install安装界面时,按tab键。
出现grub菜单,由于ks文件采用方法1,在行末输入空格 ks=http://192.168.122.1/my/ks-rhel.cfg
如果这里无法访问ks文件,则无法进入自动安装界面,会报错
The following problem occurred on line 1 of the kickstart file:
Unknown command: <!DOCTYPE
通过sendkey ALT-F2进入目录 /run/install/repo/addons可以看到这个文件内容。
自动安装完成后,需要点reboot按钮才能完成安装,ks文件待完善。

三、命令行自动安装

ks文件采用方法3,使用virt-install命令安装
先停止
[root@xycto html]# systemctl stop vsftpd
[root@xycto html]# systemctl stop httpd

virt-install -n server2.example.com -r 1024 --disk
path=/var/lib/libvirt/images/server2.centos.qcow2,size=20
–location /home/xy/CentOS-7-x86_64-DVD-1810.iso
–disk /var/lib/libvirt/images/hd4ks.raw
-x ks=hd:vdb:/ks1.cfg
–graphics vnc

如果ks文件正确,则会自动分区并安装,重启。

如果创建失败成功后,需要彻底删除该虚拟机,则运行

# virsh list --all
# virsh shutdown server2.example.com
# virsh destroy server2.example.com  强行停止
# virsh undefine server2.example.com 删除虚拟机
  只删除/etc/libvirt/qemu/server2.example.com.xml,镜像文件会保存/var/lib/libvirt/images/server2.centos.qcow2

问题1:virt-install运行后有virt-viewer报错:

WARNING Unable to connect to graphical console: virt-viewer not installed. Please install the ‘virt-viewer’ package.
WARNING No console to launch for the guest, defaulting to --wait -1

# yum install virt-viewer

在virt-install运行后会自动弹出virt-viewer的安装界面,也可以手工运行该命令,弹出当前 运行的虚拟机供选择。

问题2:vnc连接被拒绝的问题:

报错:

[root@xycto ~]# vncviewer 192.168.122.1:5900

TigerVNC Viewer 64-bit v1.8.0
Built on: 2019-08-09 03:01
Copyright (C) 1999-2017 TigerVNC Team and many others (see README.txt)
See http://www.tigervnc.org for information on TigerVNC.

Sun Sep 22 14:23:02 2019
 DecodeManager: Detected 4 CPU core(s)
 DecodeManager: Creating 4 decoder thread(s)
 CConn:       unable connect to socket: Connection refused (111)
[root@xycto ~]#

原因:
[root@xycto ~]# netstat -an|grep 5900
tcp 0 0 127.0.0.1:5900 0.0.0.0:* LISTEN
解决方法:
[root@xycto ~]# vncviewer 127.0.0.1:5900
只能用127的地址,用其他网卡地址都无法连接。

问题3:进入安装界面后提示“No disks selected”

具体现象
通过virt-manager进行安装,此时可以进入安装界面,但是提示“No disks selected” 没有选择硬盘。
Ctrl_Alt_F2可以mount /dev/vdb看到ks1.cfg
Ctrl_Alt_F6回到安装界面。
处理步骤
原有ks1.cfg配置的bootloader和part需要指定硬盘,修改为
bootloader --location=mbr --boot-drive=vda
part /boot --fstype=“xfs” --ondisk=vda --size=500
part / --fstype=“xfs” --ondisk=vda --size=20000
part swap --fstype=“swap” --ondisk=vda --size=4000

尝试通过http获取ks文件
systemctl start httpd
cp /root/ks1.cfg /var/www/html
virt-install -n server2.example.com -r 1024 --disk
path=/var/lib/libvirt/images/server2.centos.qcow2,size=20
–location /home/xy/CentOS-7-x86_64-DVD-1810.iso
-x ks=http://192.168.122.1/ks1.cfg
进入安装界面,但还是报错“No disks selected”
这里说明新的ks1.cfg配置有问题。再次修改ks1.cfg
在firstboot --disable行下面新增
ignoredisk --only-use=vda
同样错误。

重新在正常使用的ks文件基础上修改
[root@xycto images]# mount /var/lib/libvirt/images/hd4ks.raw /mnt/tmp
[root@xycto ~]# cp /var/www/html/my/ks-rhel.cfg /mnt/tmp/ks1.cfg
[root@xycto ~]# vi /mnt/tmp/ks1.cfg
修改为从cdrom启动
cdrom
#url --url http://192.168.122.1/inst
主机名修改一下
network --hostname=server2.example.com
[root@xycto images]# umount /mnt/tmp
重新执行virt-install采用vdb的ks文件自动安装成功。
之前/root/ks1.cfg的配置文件应该是GUI工具手工创建未完成的工具。

附录:ks1.cfg

说明,在上面成功安装的基础上,下面的ks文件增加了reboot命令,修改了2个密码,增加了post段内容。

[root@xycto html]# mount /var/lib/libvirt/images/hd4ks.raw /mnt/tmp
[root@xycto ~]# cat /mnt/tmp/ks1.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
repo --name="Server-HighAvailability" --baseurl=file:///run/install/repo/addons/HighAvailability
repo --name="Server-ResilientStorage" --baseurl=file:///run/install/repo/addons/ResilientStorage
# Use CDROM installation media
cdrom
#url --url http://192.168.122.1/inst
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --disable
ignoredisk --only-use=vda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8 --addsupport=zh_CN.UTF-8

# Network information
network  --bootproto=dhcp --device=eth0 --onboot=off --ipv6=auto --no-activate
network  --hostname=server2.example.com

# Root password
rootpw --iscrypted $6$o22lHc.4NnFvMw9T$T0wWh/.xbC0XETDSSnC8yukpScQErclHerGGJH.1
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
user --name=xy --password=$6$X1SHCWMuj2LeRgBq$.AOv8L.wC071RFxueaPo4sFNmADn./TckbBu6VXitrA/ --iscrypted --gecos="xy"
# 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=400
part swap --fstype="swap" --ondisk=vda --size=2600
part / --fstype="xfs" --ondisk=vda --size=17408

# Reboot after installation
reboot

%packages
@^minimal
@core
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

%post
date >> /root/osinit.txt
hostname >> /root/osinit.txt
uname -a >> /root/osinit.txt
%end
[root@xycto ~]#

四 使用ks1.cfg重新创建虚拟机

此时可以在自动安装完成后,自动重启,但无登录密码。

破坏硬盘数据再次测试

[root@xycto images]# dd if=/dev/zero of=server2.centos.qcow2 bs=10M count=20
20+0 records in
20+0 records out
209715200 bytes (210 MB) copied, 0.142677 s, 1.5 GB/s
此时运行virt-manager会报错,需要删除该文件
ERROR    internal error: qemu unexpectedly closed the monitor: 2019-09-22T08:09:46.787918Z qemu-kvm: -drive file=/var/lib/libvirt/images/server2.centos.qcow2,format=qcow2,if=none,id=drive-virtio-disk0: could not open disk image /var/lib/libvirt/images/server2.centos.qcow2: Image is not in qcow2 format

五 恢复root密码

重启操作系统
virsh reboot server2.example.com

进入急救模式的4种方法

在grub引导菜单下按e,找到linux16行,在末尾增加
systemd.unit=emergency-target 按Ctrl+x进入紧急模式,仍然要求输入root密码。
输入 rd.break 按Ctrl+x进入switch_root#的提示符。
输入init=/sysroot/bin/sh 按Ctrl+x进入:/#的提示符,在journalctl可以看到无法找到/sysroot/sysroot/bin/sh的报错。
输入init=/bin/sh 按Ctrl+x进入sh-4.2#的提示符,此时可以输入更多命令,比如lsblk。

在init=/bin/sh模式下的恢复

以下操作在此模式下进行:
df可以看到挂载了/dev/vda2 到根下,但无法写文件。
mount -o remount,rw /dev/vda2 /
passwd设置新密码
由于默认开启了seLinux但此时并未启动selinux,所以需要在下次启动时强制刷新
touch /.autorelabel
此时只能强制重启 virsh reset server2.example.com
系统会自动重启两次,之后就可以登录了。

在rd.break模式下的恢复

不能执行df命令,mount|grep vd可以看到/dev/vda2挂载在/sysroot下,而且是只读ro的。
mount -o remount,rw /dev/vda2 /sysroot
chroot /sysroot
passwd
./autorelabel
两次exit会自动重启。

检查自动执行脚本

cat /root/osinit.txt能正常显示安装时记录的信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值