#!/bin/bash MACHINE_CONF_FILE="/root/machine.conf" if [ ! -f "$MACHINE_CONF_FILE" ]; then echo "please checke existance of $MACHINE_CONF_FILE" exit 99 fi source $MACHINE_CONF_FILE # we should now have the following variables echo "SERVER_NAME $SERVER_NAME" echo "NET_IPV4_MAC $NET_IPV4_MAC" echo "NET_IPV4_ADDRESS $NET_IPV4_ADDRESS" echo "NET_IPV4_GATEWAY $NET_IPV4_GATEWAY" echo "NET_IPV4_BROADCAST $NET_IPV4_BROADCAST" echo "NET_IPV4_NETMASK $NET_IPV4_NETMASK" read -p "Do you cant to create a machine with this config? " -n 1 -r if [[ ! $REPLY =~ ^[Yy]$ ]] then exit 1 fi ########################################### ########################################### ### ### ### no customisation below this point ### ### ### ########################################### ########################################### # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=740895 #NET_IPV6_ADDRESS="2a01:4f8:141:21a3::3" #NET_IPV6_NETMASK="64" #NET_IPV6_GATEWAY="fe80::1" HOST_NAME=$SERVER_NAME # create passwd PASSWORD=$(makepasswd --minchars=10 --maxchars=12) echo "Generated random password" echo "ADD TO PASSWORD STORE/KEEP IN SAFE PLACE" echo $PASSWORD read -p "Are you reay to continue? " -n 1 -r if [[ ! $REPLY =~ ^[Yy]$ ]] then exit 1 fi # helper function to wait until the VM has shut down wait_for_shutdown() { echo "Waiting for shutdown of $1" while true; do virsh list | grep -c $1 > /dev/null if [ $? -eq 1 ]; then echo echo "VM stopped: $1" return 0 fi sleep 1 echo -n "." done } # note on preseeding # file must be injected in the root/intrd filesysetm # if file is not named preseed.cfg the file name must be passed to the kerne like preseed/file=/some/whatever.cfg # see here for the full story: # https://www.debian.org/releases/stable/amd64/apbs02.html.en#preseed-loading #VOLUME=$SERVER_NAME #--network bridge=br0 \ #--network bridge=br-ext,mac=$NET_IPV4_MAC,model=virtio \ virt-install \ --connect qemu:///system \ --name $SERVER_NAME \ --ram 2048 \ --disk pool=vgpool,size=50,bus=ide \ --vcpus 2 \ --os-type linux \ --os-variant generic \ --network bridge=br-int,model=virtio \ --graphics none \ --console pty,target_type=serial \ --location 'http://mirror.hetzner.de/debian/packages/dists/jessie/main/installer-amd64/' \ --initrd-inject '/usr/local/virtinstaller/preseed.cfg' \ --extra-args "auto=true netcfg/get_hostname=$HOST_NAME netcfg/hostname=$HOST_NAME passwd/root-password=$PASSWORD passwd/root-password-again=$PASSWORD netcfg/get_ipaddress=$NET_IPV4_ADDRESS netcfg/get_netmask=$NET_IPV4_NETMASK netcfg/get_gateway=$NET_IPV4_GATEWAY console=ttyS0,115200n8 serial" echo "Finished with virt-install." read -p "Did the installation finish and the VM shut down? " -n 1 -r if [[ ! $REPLY =~ ^[Yy]$ ]] then exit 1 fi echo "Doing some aftermath..." # shutdown virsh destroy $SERVER_NAME wait_for_shutdown $SERVER_NAME # copy ssh-keys of admins into machine # generate ssh-authorized keys so they can be uploaded into the root-account SCRIPT_PATH=$(dirname $([ -L $0 ] && readlink -f $0 || echo $0)) $SCRIPT_PATH/gatherkeys.sh > $SCRIPT_PATH/authorized_keys virt-copy-in -d $SERVER_NAME $SCRIPT_PATH/authorized_keys /root/.ssh/ # start again virsh start $SERVER_NAME # set domain to autostart virsh autostart $SERVER_NAME