kvm一键装机

本文链接 https://www.jianshu.com/p/f586f4ce0722

官方网站
创建虚拟磁盘

qemu-img create -f qcow2 /opt/kvm/data/ks-master.qcow2 50G

create_kvm.sh

#!/usr/bin/env bash

# Update kickstart file
#echo -en "Enter Hostname: "
#read HOST_NAME
#echo -en "Enter IP Address: "
#read IP_ADDRESS

HOST_NAME=ks-master
IP_ADDRESS="192.168.122.100"

sed -i 's/server1/'$HOST_NAME'/g' /opt/kvm/ks.cfg
sed -i 's/192.168.122.100/'$IP_ADDRESS'/g' /opt/kvm/ks.cfg

## Pre-defined variables
echo ""
MEM_SIZE=4096
VCPUS=2
OS_VARIANT="rhel7"
ISO_FILE="/data/CentOS-7-x86_64-DVD-1810.iso"

#echo -en "Enter vm name: "
#read VM_NAME
#echo -en "Enter virtual disk size : "
##read DISK_SIZE
VM_NAME=ks-master
OS_TYPE="linux"
sudo virt-install -v \
     --arch x86_64 --virt-type kvm \
     --name ${VM_NAME} \
     --memory=${MEM_SIZE} \
     --vcpus=${VCPUS} \
     --os-type ${OS_TYPE} \
     --location ${ISO_FILE} \
     --network default \
     --graphics=none \
     --os-variant=${OS_VARIANT} \
     --disk /opt/kvm/data/${VM_NAME}.qcow2,cache=writeback,io=threads,bus=virtio  \
     --initrd-inject="/opt/kvm/ks.cfg" \
     --extra-args="ks=file:/ks.cfg  console=ttyS0"

ks.cfg

命令创建

yum -y install system-config-kickstart
system-config-kickstart
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
#cdrom  # 光盘
# Use graphical install
#graphical # 图形化

# Use text install
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

reboot

###############################################################
#
# network configuration
#
###############################################################
# Network information  网卡信息
network --bootproto=static --ip=192.168.122.100 --gateway=192.168.122.1  --netmask=255.255.255.0  --noipv6 --device=eth0 --nameserver=192.168.122.1,8.8.8.8 --activate

#network  --bootproto=dhcp --device=ens33 --ipv6=auto --activate
network  --hostname=ks-master

###############################################################
#
# SELinux and Firewalld
#
###############################################################

#selinux --enforcing
#selinux --permissive
#selinux --disabled

# Root password
rootpw --iscrypted $6$664MrIxqJRdWGVFl$YOzpdGg6HrzFw/OPeJde/HLv533xqFhmA.zsV1efftetxuQMowxNOeKjX9cUigHifQx2.q9HXtdfPvY3/u24o0


user --groups=wheel --name=shark --password=$6$664MrIxqJRdWGVFl$YOzpdGg6HrzFw/OPeJde/HLv533xqFhmA.zsV1efftetxuQMowxNOeKjX9cUigHifQx2.q9HXtdfPvY3/u24o0 --gecos="Mutai Josphat"

###############################################################
#
# SELinux and Firewalld
#
###############################################################

#selinux --enforcing
#selinux --permissive
selinux --disabled


firewall --disabled

# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc
# System bootloader configuration
bootloader --location=mbr --boot-drive=vda
autopart --type=lvm
# Partition clearing information
clearpart --none --initlabel

%packages
@^minimal
@core
@development
chrony
tree
wget
%end

%addon com_redhat_kdump --disable --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

优化脚本

#!/usr/bin/env bash

# Update kickstart file
echo -en "Enter Hostname: "
read HOST_NAME
echo -en "Enter IP Address: "
read IP_ADDRESS

HOST_NAME=$HOST_NAME
IP_ADDRESS=$IP_ADDRESS

sed -i 's/server1/'$HOST_NAME'/g' ./ks.cfg
sed -i 's/192.168.122.100/'$IP_ADDRESS'/g' ./ks.cfg

## Pre-defined variables
echo ""
MEM_SIZE=4096
VCPUS=2
OS_VARIANT="rhel7"
ISO_FILE="/data/CentOS-7-x86_64-DVD-1810.iso"

echo -en "Enter vm name: "
read VM_NAME
echo -en "Enter virtual disk size : "
read DISK_SIZE
qemu-img create -f qcow2 ./data/${VM_NAME}.qcow2 $DISK_SIZE
VM_NAME=$VM_NAME
OS_TYPE="linux"
DISK_PATH="./data/${VM_NAME}.qcow2"
CFG_PATH="./ks.cfg"
sudo virt-install -v \
     --arch x86_64 --virt-type kvm \
     --name ${VM_NAME} \
     --memory=${MEM_SIZE} \
     --vcpus=${VCPUS} \
     --os-type ${OS_TYPE} \
     --location ${ISO_FILE} \
     --network default \
     --graphics=none \
     --os-variant=${OS_VARIANT} \
     --disk $DISK_PATH,cache=writeback,io=threads,bus=virtio  \
     --initrd-inject=$CFG_PATH \
     --extra-args="ks=file:/ks.cfg  console=ttyS0"


sed -i 's/$HOSTNAME/server1/g' ./ks.cfg
sed -i 's/$IP_ADDRESS/192.168.122.100/g' ./ks.cfg

你可能感兴趣的:(kvm一键装机)