Skip to content

Instantly share code, notes, and snippets.

@akagisho
Last active December 25, 2016 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akagisho/0f7fc8b05f78e1dc13bd to your computer and use it in GitHub Desktop.
Save akagisho/0f7fc8b05f78e1dc13bd to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
[ ! -z $DEBUG ] && set -x
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
if [ $# -ne 4 ]; then
echo "Usage: `basename $0` NAME IPADDR NETMASK GATEWAY" 1>&2
exit 1
fi
NAME=$1
IPADDR=$2
NETMASK=$3
GATEWAY=$4
PASSWD=`cat /dev/urandom | tr -cd '[:alnum:]' | fold -w32 | head -1`
RELEASE=${RELEASE:-6.8}
TIMEZONE=${TIMEZONE:-Asia/Tokyo}
BRIDGE=${BRIDGE:-br0}
MEMORY=${MEMORY:-512}
DISK=${DISK:-20}
CPU=${CPU:-2}
KS_CFG=ks_$NAME.cfg
cd `dirname $0`
PUBKEYS=""
if [ -f authorized_keys ]; then
PUBKEYS=`cat authorized_keys`
fi
mkdir -p tmp
cat << __EOM__ > tmp/$KS_CFG
install
lang en_US.UTF-8
keyboard us
network --device eth0 --onboot yes --bootproto=static --ip=$IPADDR --netmask=$NETMASK --gateway=$GATEWAY --nameserver=8.8.8.8 --noipv6 --hostname $NAME
zerombr
bootloader --location=mbr --append="crashkernel=auto rhgb quiet"
clearpart --all --initlabel --drives=vda
autopart
rootpw --plaintext $PASSWD
authconfig --enableshadow --passalgo=sha512
selinux --disabled
firewall --disabled
firstboot --disabled
timezone --utc $TIMEZONE
reboot
%packages
openssh-server
%end
%post
/bin/sed -i -e 's/^\(\s*kernel .*\)$/\1 console=tty0 console=ttyS0,115200n8/' /boot/grub/grub.conf
/bin/mkdir /root/.ssh
/bin/echo "$PUBKEYS" >> /root/.ssh/authorized_keys
%end
__EOM__
virt-install \
--name $NAME \
--virt-type kvm \
--ram $MEMORY \
--vcpus $CPU \
--arch x86_64 \
--os-type linux \
--os-variant rhel6 \
--boot hd \
--disk path=/var/lib/libvirt/images/$NAME.img,size=$DISK \
--network bridge=$BRIDGE \
--graphics vnc \
--serial pty \
--console pty,target_type=serial \
--location http://ftp.riken.jp/Linux/centos/$RELEASE/os/x86_64/ \
--initrd-inject tmp/$KS_CFG \
--extra-args "ks=file:/$KS_CFG" \
--noautoconsole
echo ======================================
echo "USER: root"
echo "PASS: $PASSWD"
echo ======================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment