#!/bin/bash

# Fetches a CentOS netinstall ISO, installs it automatically in a VM and sets
# it up for Nmap regression testing.
#
# Requires mtools to prepare the floppy image and qemu to run the VM. Doesn't
# require root privileges. Tested on Fedora 20.

ARCH=x86_64
MIRROR=http://ftp.wcss.pl/pub/linux/fedora/linux/releases/20/Fedora/$ARCH
CD_FILE=Fedora-20-x86_64-netinst.iso
CD_SIZE=336592896
CD_URL="$MIRROR/iso/$CD_FILE"
SSH_PORT=2233
SSH_ARGS=("-i" "vm-key" "root@localhost" "-o" "Port=$SSH_PORT"
          "-o" "UserKnownHostsFile=/dev/null" "-o" "StrictHostKeyChecking=no")

set -e # Whatever fails, just exit

cat > ks.cfg << END-OF-FILE
# This is a CentOS Kickstart installation script. Consult CentOS documentation
# for details.

# Most of the settings were just copied from various files that can be found
# here: https://nazar.karan.org/tree/bluecain.git

poweroff # After installation, poweroff the VM.
install
url --url $MIRROR/os

lang en_US.UTF-8
keyboard us
timezone America/New_York

network --device ens3 --bootproto dhcp
firewall --enabled --port=22:tcp

rootpw --plaintext d
authconfig --enableshadow --enablemd5
selinux --enforcing

bootloader --location=mbr --driveorder=sda --append="rhgb quiet"
clearpart --all --initlabel
part /boot --fstype ext3 --size=250
part pv.2 --size=5000 --grow
volgroup VolGroup00 --pesize=32768 pv.2
logvol / --fstype ext4 --name=LogVol00 --vgname=VolGroup00 --size=1024 --grow
logvol swap --fstype swap --name=LogVol01 --vgname=VolGroup00 --size=256 --grow --maxsize=512

%packages --ignoremissing

openssh-server

gnuplot

svn
gcc
gcc-c++
autoconf
%end
END-OF-FILE

# First, let's prepare a kickstart floppy.
dd if=/dev/zero of=ks.img count=1440 bs=1k
/sbin/mkfs.msdos ks.img
mcopy -i ks.img ks.cfg ::/

# Download the NetInstall ISO file if it's not already there.
if [ `(wc -c $CD_FILE || echo 0) | cut -f1 -d' '` != "$CD_SIZE" ]; then
  wget --continue $CD_URL
fi
# Modify its bootloader to make it load kickstart once started.
sed -i  's@rd.live.check quiet@ks=hd:fd0          @g' $CD_FILE

# Create a sparse file for the VM HDD.
rm -f vm.img
dd of=vm.img bs=1M seek=$(( 1024 * 8 )) count=0

echo -e '\e[1;32m
Okay, now relax. We are installing the VM. This might take quite a lot of time.
To monitor the progress, take a look at the first column of "ls -lhs vm.img".
If it is still zero after a few minutes, something went wrong.
\e[0m'

# Run the installation process. This takes about 10-15min on a KVM-enabled VM
# under Intel i7 and will be even slower without KVM.
qemu-kvm -cdrom $CD_FILE vm.img -m 2g -net nic -net user -boot d -fda ks.img \
         -monitor stdio -display none

qemu-kvm vm.img -m 2g -net nic -net user -monitor stdio -display none \
         -redir tcp:2233::22 &
VM_PID=$!

# We're not waiting for the VM to start sshd, but for Qemu to bind to the port.
# Once it does that, it will accept connections even if the VM doesn't and let
# us in as soon as the VM opens the port.
sleep 10
# Generate a passwordless SSH key and install it in the VM.
rm -f -v vm-key vm-key.pub
ssh-keygen -f vm-key -N ""
chmod 600 vm-key vm-key.pub
echo 'd' | ./sshaskpass.sh ssh-copy-id "${SSH_ARGS[@]}"

ssh "${SSH_ARGS[@]}" shutdown -h now &
wait
