hoki


#!/usr/bin/perl -w
#
# do a hands-off Red Hat kickstart install
#

use strict;
use File::Copy;

my $pxeurl = 
    "http://okoboji.unh.edu/yarrow/images/boot.iso"; # ISO image URL
my $cddir = "/mnt/cdrom";			     # ISO image mount pt
my $imgdir = "/mnt/img";			     # ramdisk mount pt
my $rhurl = "http://okoboji.unh.edu/yarrow";         # install tree URL

my $rootdev;		# /dev name of boot partition
my $hostname;		# hostname for kickstart network config
my $gateway;		# gateway IP for kickstart
my $maxpack;		# used to determine active interface
my $interf;		# used to determine active interface
my $data;		# used to determine active interface
my @nums;		# used to determine active interface
my $active_if;		# active interface (eg "eth1")
my $ifconfig_out;	# output from ifconfig
my $ipaddr;		# ip address of active interface
my $mask;		# netmask for active interface
my $nameserver;		# nameserver IP
my $device;		# used to find root partition mount point
my $mountpt;		# used to find root partition mount point

#
# find root partition mount point
#
open(DF, "/bin/df /|");
while (<DF>) {
    chomp;
    ($device, $mountpt) = (split)[0,5];
    if ($mountpt eq "/") {
	$rootdev = $device;
    }
}
print "Root device is $rootdev\n";


#
# find hostname and gateway
#
open(P, "</etc/sysconfig/network") ||
    die "Can't open /etc/sysconfig/network: $!\n";
                                                                                
while (<P>) {
    chomp;
    /HOSTNAME=(.+)/ && ($hostname = $1);
    /GATEWAY=(.+)/ && ($gateway = $1);
}
close(P);
print "Hostname: $hostname\n";
print "Gateway: $gateway\n";
                                                                                
#
# determine active interface for machines with perhaps more than one
#
open(P, "</proc/net/dev") || die "Can't open /proc/net/dev: $!\n";
$maxpack = 0;
while(<P>) {
    chomp;
    if (/\s*([^:]+):([^:]+)/) {
        # print "DEBUG: saw interface $1, data $2\n";
        $interf = $1; $data = $2;
	next if ($interf eq "lo"); # never use lo
        @nums = split(' ', $data);
        if ($nums[0] > $maxpack) {
            $maxpack = $nums[0];
            $active_if = $interf;
        }
    }
}
close(P);
print "Active interface is $active_if\n";
                                                                                
#
# determine IP address and netmask for active interface
#
$ifconfig_out = `/sbin/ifconfig $active_if`;
                                                                                
if ($ifconfig_out =~ /inet addr:(\d+\.\d+\.\d+\.\d+)/) {
    $ipaddr = $1;
}
else {
    die "Couldn't get IP address from ifconfig output\n";
}
                                                                                
if ($ifconfig_out =~ /Mask:(\d+\.\d+\.\d+\.\d+)/) {
    $mask = $1;
}
else {
    die "Couldn't get mask from ifconfig output\n";
}
                                                                                
print "IP Address is $ipaddr\n";
print "Netmask is $mask\n";

#
# find a default name server
#
$nameserver = "132.177.128.99";
open(P, "</etc/resolv.conf") ||
    die "Can't open /etc/resolv.conf: $!\n";
while (<P>) {
    if (/nameserver (\d+\.\d+\.\d+\.\d+)/) {
	$nameserver = $1;
	last unless ($nameserver eq $ipaddr);
    }
}
close(P);
print "Nameserver is $nameserver\n";

#
# last chance to bail out...
#
print "Will create ks.cfg with this network line:\n";
print "network --device=$active_if --bootproto=static --ip=$ipaddr --netmask=$mask --gateway=$gateway --nameserver=$nameserver --hostname=$hostname\n";
print "Control-C in 10 sec if that's not OK...!\n";
sleep 10;

chdir("/var/tmp");
print "Getting boot.iso...\n";
system("curl -O $pxeurl");
(-d $cddir) || mkdir $cddir;
print "Mounting boot.iso on $cddir...\n";
system("mount -o loop /var/tmp/boot.iso $cddir");
print "Copying $cddir/isolinux/vmlinuz...\n";
copy("$cddir/isolinux/vmlinuz", "/boot/vmlinuz-iso") ||
    die "Copy failed $!\n";
print "Copying $cddir/isolinux/initrd.img...\n";
copy("$cddir/isolinux/initrd.img", "/boot/initrd-iso.img") ||
    die "Copy failed: $!\n";


(-d $imgdir) || mkdir $imgdir;
print "Uncompressing initrd-iso.img...\n";
system("/bin/gunzip < /boot/initrd-iso.img > /var/tmp/unc.img");
print "Mounting uncompressed initrd.img...\n";
system("/bin/mount -o loop /var/tmp/unc.img $imgdir");

print "Creating kickstart config...\n";
open(KS, ">$imgdir/ks.cfg");
print KS <<xxxFUNNYxxx;
lang en_US
langsupport  --default=en_US
keyboard us
timezone America/New York
rootpw UnrealPwd
mouse
network --device=$active_if --bootproto=static --ip=$ipaddr --netmask=$mask --gateway=$gateway --nameserver=$nameserver --hostname=$hostname
reboot
text
install
url --url $rhurl
bootloader --location=mbr --md5pass=\$1\$viPZdGGr\$0Gq4oW4xy56yGNb23P149.
zerombr yes
clearpart --all --initlabel
part / --fstype ext3 --size 1 --grow
part swap --recommended
part /boot --fstype ext3 --size 100
auth  --useshadow  --enablemd5
firewall --enabled --ssh
xconfig --depth=32 --resolution=1280x1024 --defaultdesktop=GNOME --startxonboot
\%packages --resolvedeps
\@ X Window System
\@ GNOME Desktop Environment
\@ KDE Desktop Environment
\@ Editors
\@ Engineering and Scientific
\@ Graphical Internet
\@ Text-based Internet
\@ Office/Productivity
\@ Graphics
\@ Authoring and Publishing
\@ Development Tools
\@ Kernel Development
\@ X Software Development
\@ GNOME Software Development
\@ KDE Software Development
\@ Administration Tools
\@ System Tools
\@ Printing Support
\%pre
mkdir -p /tmp/sshkeys
mkdir -p /mnt/oldroot
mount $rootdev /mnt/oldroot
cp /mnt/oldroot/etc/ssh/ssh_host_* /tmp/sshkeys/
cp /mnt/oldroot/var/log/wtmp /tmp/
umount /mnt/oldroot
\%post --nochroot
cp /tmp/sshkeys/* /mnt/sysimage/etc/ssh/
cp /tmp/wtmp /mnt/sysimage/var/log/
\%post
curl -o /var/tmp/cislinux.customize http://okoboji.unh.edu/cislinux/scripts/cislinux.customize
chmod 700 /var/tmp/cislinux.customize
/var/tmp/cislinux.customize
xxxFUNNYxxx
close(KS);
print "Unmouting uncompressed initrd.img...\n";
system("/bin/umount $imgdir");
print "Recompressing initrd.img...\n";
system("/bin/gzip < /var/tmp/unc.img > /boot/initrd-iso.img");

#
# this used to be a lot harder before I knew about grubby
#
print "Running grubby...\n";
system("/sbin/grubby --add-kernel=/boot/vmlinuz-iso --initrd=/boot/initrd-iso.img --copy-default --args=ks=file:ks.cfg --title=kickstart --make-default");

print "Rebooting to installation...\n";
system ("/sbin/shutdown -r now");