#!/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 subscription_args="" # If we have subscription details as an arg, upload those into the vm # Otherwise ensure subscription is skipped during setup if [ "$#" -gt 3 ]; then subscription_path=$4 subscription_args="--mkdir /root/.rhel \ --upload $subscription_path/login:/root/.rhel/login \ --upload $subscription_path/pass:/root/.rhel/pass" else subscription_args="--touch /root/.skip_repos:" fi 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/../../../test/common/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 %end EOF qemu-img create -f qcow2 "$out" 8G connect="--connect=qemu:///session" name=fedora-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 } } EOF } cleanup_builder_vm supervised_virt_install $connect \ --initrd-inject=$ks \ --extra-args="ks=file:/$ks console=ttyS0,115200" \ --name=$name \ --disk "path=$out,format=qcow2" \ --ram 4096 \ --vcpus=1 \ --os-type linux \ --os-variant fedora21 \ --location="$url" \ --nographics \ --noreboot rm $ks cleanup_builder_vm BASE=$(dirname $0) # Either upload subscription credentials or touch file to ensure we don't want to subscribe virt-customize --add $out --upload $BASE/fedora-network-ifcfg:/etc/sysconfig/network-scripts/ifcfg-eth0 $subscription_args