centos 7安装好KVM之后还要安装虚拟机,通过VNC连接手动安装centos 7虚拟机太麻烦了,所以无人值守安装是做好的。简单记录下。
无人值守安装centos 7前提是要安装KVM,并且能手动创建虚拟机。
首先创建centos7.ks.cfg文件 vi /root/centos7.ks.cfg
install #禁用第一次启动时设置系统的向导 firstboot --disabled eula --agreed reboot # 系统语言 lang en_US.UTF-8 keyboard us # 设置时区 timezone --isUtc Asia/Shanghai # 关闭selinux和防火墙 selinux --disabled firewall --disabled # 开启的服务 services --enabled=sshd,ntpd,ntpdate,network # 网络设置比较复杂,建议安装后再设置 # network --hostname pandora # 启动方式、自动分区 clearpart --all --initlabel bootloader --location=mbr autopart --type=lvm zerombr # root 密码为123@abc# auth --useshadow --enablemd5 rootpw --iscrypted $6$nLlYENVDvCsM/2SX$ZD6JgRkeuN1nW239DA53b33Yyl7j0MuAKCY/CKQ/YSdA3/6sBOqInIO67JJmssHb3HTCQLj4OlVfzwt7m8Bin0
#rootpw 123456 #(明文) #repo --name=base --baseurl=http://centos.mirrors.ovh.net/ftp.centos.org/7/os/x86_64 #url --url="http://centos.mirrors.ovh.net/ftp.centos.org/7/os/x86_64" # 最小化安装 %packages --nobase --ignoremissing @core #安装一些组件 acpid net-tools ntp ntpdate wget %end %post echo "ttyS0" >> /etc/securetty sed -i 's/1:2345:respawn:/sbin/mingetty tty1/co:2345:respawn:/sbin/agetty ttyS0 115200 vt100-nav/' /etc/inittab sed -i 's/^HISTSIZE=.*$/HISTSIZE=100/' /etc/profile # /etc/security/limits.conf [ -z "`cat /etc/security/limits.conf | grep 'nproc 65535'`" ] && cat >> /etc/security/limits.conf < <EOF * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535 EOF [ -z "`cat /etc/rc.local | grep 'ulimit -SH 65535'`" ] && echo "ulimit -SH 65535" >> /etc/rc.local [ -z "`cat ~/.bashrc | grep ^PS1`" ] && echo 'PS1="[e[37;40m][[e[32;40m]u[e[37;40m]@h [e[35;40m]W[e[0m]]\$ "' >> /root/.bashrc # /etc/sysctl.conf sed -i 's/net.ipv4.tcp_syncookies.*$/net.ipv4.tcp_syncookies = 1/g' /etc/sysctl.conf [ -z "`cat /etc/sysctl.conf | grep 'fs.file-max'`" ] && cat >> /etc/sysctl.conf < < EOF fs.file-max=65535 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.ip_local_port_range = 1024 65000 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.route.gc_timeout = 100 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 1 net.core.somaxconn = 65535 net.core.netdev_max_backlog = 262144 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_max_orphans = 262144 EOF %end
关于配置里面的root的密码,首先看看python 的版本
[root@pandora qemu]# python -V Python 2.7.5 python的版本大于2.7的,运行设置密码 python -c 'import crypt,getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))' python的版本小于于2.7的,运行 python -c 'import crypt; print crypt.crypt("CLEARTEXTPASSWORD", "$6$saltsalt$")' 当然,直接用脚本配置里面的123@abc#的密码也可以,安装完后更改root密码
接着用virt-install安装虚拟机,就会自动按照centos7.ks.cfg的配置完成安装
qemu-img create -f qcow2 -o preallocation=metadata /home/storage/centos7.qcow2 100G #先创建磁盘
virt-install --accelerate --name=centos7-docker --hvm --virt-type kvm --boot hd --disk path=/home/storage/centos7-docker.qcow2,format=qcow2,size=300,sparse=true,cache=none,bus=scsi --network bridge:br0 --vcpus=8 --ram=8192 --os-type=linux --os-variant=rhel7 --graphics none --serial pty --console pty --location /home/iso/CentOS-7-x86_64-DVD-1511.iso --initrd-inject /home/iso/conf/base7.cfg --extra-args "inst.ks=file:/base7.cfg console=ttyS0"