#!/bin/bash # # Copyright (C) 2015 Red Hat Inc. # Author: # Further contributions: # Adapted: # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # Adapted from create-guest-qcow2. We don't use that script as is # since it is, to quote Mr Parker, "2% code and 98% config stuff". set -ex if [ "$#" -lt 3 ]; then echo >&2 "Usage: virt-install-fedora IMAGE ARCH URL [SUBSCRIPTION_PATH]" exit 1 fi out=$1 arch=$2 url=$3 base=$(dirname $0) ks=fedora.ks cat <$ks install text shutdown lang en_US.UTF-8 keyboard us network --bootproto dhcp rootpw foobar firewall --enabled --ssh selinux --enforcing timezone --utc America/New_York bootloader --location=mbr --append="console=ttyS0,115200 rd_NO_PLYMOUTH" zerombr clearpart --all --initlabel autopart %packages @core %end %post mkdir /root/.ssh chmod 700 /root/.ssh echo "$(cat $base/../../machine/identity.pub)" > /root/.ssh/authorized_keys chmod 600 /root/.ssh/authorized_keys mkdir -p /etc/sysconfig/network-scripts echo "$(cat $base/network-ifcfg-eth0)" > /etc/sysconfig/network-scripts/ifcfg-eth0 echo "$(cat $base/network-ifcfg-eth1)" > /etc/sysconfig/network-scripts/ifcfg-eth1 sed -i 's/GRUB_TIMEOUT.*/GRUB_TIMEOUT=0/; /GRUB_CMDLINE_LINUX=/ s/"$/ net.ifnames=0 biosdevname=0"/' /etc/default/grub grub2-mkconfig -o /boot/grub2/grub.cfg systemctl is-enabled sshd.socket || systemctl is-enabled sshd.service || systemctl enable sshd.socket %end EOF qemu-img create -f qcow2 "$out" 12G connect="--connect=qemu:///session" name=$(basename $1)-builder cleanup_builder_vm () { if virsh $connect list --state-running --name | grep -q $name; then virsh $connect destroy $name fi if virsh $connect list --all --name | grep -q $name; then virsh $connect undefine $name fi } # Using virt-install is fundamentally an interactive process. We # expect the installation to finish unattended, but it might drop into # an emergency shell upon unexpected errors, for example. Thus, we # run it under 'expect' and catch a few known scenarios that require # special handling. # # (Also, virt-install works best when it has a tty.) supervised_virt_install () { expect -- - virt-install "$@" <<'EOF' eval spawn $argv set timeout -1 expect { "emergency mode" { send_user "\n\n\n\nABORT - emergency shell\n" exit 1 } "Please make your choice" { send_user "\n\n\n\nABORT - Anaconda stopped\n" exit 1 } "Please make a selection" { send_user "\n\n\n\nABORT - Anaconda stopped\n" exit 1 } } EOF } cleanup_builder_vm # HACK: Due to #1686464 ks_path="file:/$ks" ks_inject="--initrd-inject=$ks" if [[ "$url" == *30/Server* ]] then tmpdir=`mktemp -d` pushd . cp $ks $tmpdir cd $tmpdir python3 -m http.server & popd server_pid=$! ks_path="http://_gateway:8000/$ks" ks_inject="" fi supervised_virt_install $connect \ $ks_inject \ --extra-args="ks=$ks_path console=ttyS0,115200" \ --name=$name \ --disk "path=$out,format=qcow2" \ --ram 4096 \ --vcpus=1 \ --os-type linux \ --os-variant fedora21 \ --location="$url" \ --nographics \ --noreboot if [ -n "$server_pid" ] then kill $server_pid rm -rf $tmpdir fi rm $ks cleanup_builder_vm