# Kickstart file for creating the virt-p2v ISO.
# (C) Copyright 2014-2017 Red Hat Inc.
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.

# Generated by virt-p2v-make-kickstart __PACKAGE_VERSION_FULL__

lang en_US.UTF-8
keyboard us
timezone --utc GMT

rootpw --plaintext p2v

selinux --enforcing
firewall --enabled

# Make sure that systemd doesn't rename the network device.  We have
# to tell Anaconda we're using eth0, *and* we have to pass
# net.ifnames=0 on the kernel command line.
network --bootproto=dhcp --device=eth0
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200 rd_NO_PLYMOUTH net.ifnames=0"

zerombr
clearpart --all --initlabel
part / --size 3000 --fstype ext4

reboot

# Repository lines:
__REPOS__

# Packages to install in the ISO.  For dependencies, see
# p2v/Makefile.am.  Note that libguestfs is NOT required by virt-p2v.

%packages

@core

# rpm must be installed, else you'll hit RHBZ#1089566.
rpm

# Note you must have a kernel, else the boot menu won't work:
kernel

# This is required in order for RHEL to set the root password.
passwd

# RHEL needs this in order to get networking.
NetworkManager

# Required to run firewall --enabled kickstart command:
firewalld

# Needed by post script to unpack the blobs.
/usr/bin/base64
/usr/bin/gzip

# Work around https://bugzilla.redhat.com/show_bug.cgi?id=1182362
tar

# https://bugzilla.redhat.com/show_bug.cgi?id=1168223
dracut-live

# The dependencies of virt-p2v.
__DEPENDENCIES__

# Extra packages requested by the user via the virt-p2v-make-kickstart
# --install option (if any).
__EXTRA_PACKAGES__

%end

# Post-install configuration.

%post

# Base64-decoding of SSH Identity.

base64 -d -i > /var/tmp/id_rsa << EOF
__BASE64_SSH_IDENTITY__
EOF
if test -s /var/tmp/id_rsa; then
   chmod 0600 /var/tmp/id_rsa
else
   rm /var/tmp/id_rsa
fi

# Base64-decoding of /etc/issue

base64 -d -i > /etc/issue << EOF
__BASE64_ISSUE__
EOF

cp /etc/issue /etc/issue.net

# Base64-decoding of launch-virt-p2v

base64 -d -i > /usr/bin/launch-virt-p2v <<EOF
__BASE64_LAUNCH_VIRT_P2V__
EOF

chmod 0755 /usr/bin/launch-virt-p2v

# Base64-decoding of p2v.service unit file

base64 -d -i > /etc/systemd/system/p2v.service <<EOF
__BASE64_P2V_SERVICE__
EOF

# Base64-decoding of virt-p2v binary
# md5(virt-p2v) = __MD5SUM_VIRT_P2V__

base64 -d -i <<EOF | gzip -cd > /usr/bin/virt-p2v
__BASE64_VIRT_P2V__
EOF

chmod 0755 /usr/bin/virt-p2v

# Update the default getty target to login automatically as root without
# prompting for a password
sed -i 's/^ExecStart=\(.*\)/ExecStart=\1 -a root/' \
    /usr/lib/systemd/system/getty@.service

# Reserve tty1 as a getty so we can document it clearly
echo ReserveVT=1 >> /etc/systemd/logind.conf

# Force text mode
systemctl set-default multi-user.target

# Start p2v service
systemctl enable p2v.service

# Disable ssh service (RHBZ#1248678)
systemctl disable sshd.service

%end

%post --nochroot

PRODUCT='Virt P2V'
PRODUCT_SHORT='virt-p2v'
PACKAGE='__PACKAGE_NAME__'
VERSION='__PACKAGE_VERSION__'

echo "Customizing boot menu"
sed -i -e '
# Put product information at the top of the file
1 {
    i '"say $PRODUCT $VERSION"'
    i '"menu title $PRODUCT_SHORT $VERSION"'
}

# Remove any existing menu title
/^menu title .*/d

# Set the default timeout to 60 seconds
s/^timeout .*/timeout 600/
' $LIVE_ROOT/isolinux/isolinux.cfg

# store image version info in the ISO
cat > $LIVE_ROOT/isolinux/version <<EOF
PRODUCT='$PRODUCT'
PRODUCT_SHORT='${PRODUCT_SHORT}'
#PRODUCT_CODE=$PRODUCT_CODE
#RECIPE_SHA256=$RECIPE_SHA256
#RECIPE_RPM=$RECIPE_RPM
PACKAGE=$PACKAGE
VERSION=$VERSION
EOF

# overwrite user visible banners with the image versioning info
cat > $INSTALL_ROOT/etc/$PACKAGE-release <<EOF
$PRODUCT release $VERSION
EOF

# replace initramfs if regenerated
if [ -f "$INSTALL_ROOT/initrd0.img" ]; then
  mv -v "$INSTALL_ROOT/initrd0.img" "$LIVE_ROOT/isolinux/initrd0.img"
fi

%end
