使用脚本部署虚拟机

官方网站

创建一个虚拟硬盘

qemu-img create -f qcow2 /home/kvm/data/vm-ks-test.qcow2 10G

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=kube-master
IP_ADDRESS="192.168.122.10"

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

## Pre-defined variables
echo ""
MEM_SIZE=4096
VCPUS=2
OS_VARIANT="rhel7"
ISO_FILE="/root/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=vm-kube-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 /home/kvm/data/${VM_NAME}.qcow2,cache=writeback,io=threads,bus=virtio  \
     --initrd-inject="/home/kvm/ks.cfg" \
     --extra-args="ks=file:/home/kvm/ks.cfg console=tty0 console=ttyS0,115200n8"

#     --network bridge=virbr0 --network bridge=docker0 \

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=sda
# 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=kube-master

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

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

# Root password
rootpw --iscrypted $6$664MrIxqJRdWGVFl$YOzpdGg6HrzFw/OPeJde/HLv533xqFhmA.zsV1efftetxuQMowxNOeKjX9cUigHifQx2.q9HXtdfPvY3/u24o0
#这个密码在自己机器上 /etc/shadow   和自己root密码相同
#root
#123456

user --groups=wheel --name=king --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

你可能感兴趣的:(使用脚本部署虚拟机)