#!/usr/bin/perl -w # use strict; use File::Copy; use LWP::Simple; use Getopt::Std; my $DHCPD_CONF = "/etc/dhcpd.conf"; my $PXE_CONF = "/etc/pxe.conf"; my $PXE_PATH = "/var/lib/tftpboot/X86PC/linux/pxelinux.cfg/"; my $PXE_DEFAULT = $PXE_PATH . "default"; my $PXE_KA = $PXE_PATH . "clone.pxe"; my $mntka = "/mnt/ka"; my $mnttmp = "/tmp/temp_loop"; my $interface = "eth0"; # from MDK::Common, needed cause this script will run on non-Mandriva distro sub cat_ { open(my $F, $_[0]) or return; my @l = <$F>; wantarray() ? @l : join '', @l; } sub prepare_dhcpd_conf { my ($GW, $NET, $TFTP, $DNS) = @_; my $FD; open($FD, "> /tmp/dhcpd.conf.ready"); foreach (cat_("/etc/dhcpd.conf.ka")) { s/IPADDR_GW/$GW/g; s/NET/$NET/g; s/IPADDR_TFTP/$TFTP/g; s/IPADDR_DNS/$DNS/g; print $FD $_; } close($FD); } sub save_config { my ($config) = @_; if (!copy($config, $config . ".sav")) { print "Can't backup $config: $!\n" } } sub prepare_pxe_conf { my ($PXE, $TFTP) = @_; my $FD; open($FD, "> /tmp/pxe.conf.ready"); foreach (cat_("/etc/pxe.conf.ka")) { s/IPADDR_TFTP/$TFTP/; s/IPADDR_PXE/$PXE/; print $FD $_; } close($FD); } sub update_conf { print "Do you really want to update your configuration file for PXE and DHCPD server ? (y/n)\n"; my $yesno = <STDIN>; chomp ($yesno); if ($yesno eq "y") { if (!copy("/tmp/pxe.conf.ready", $PXE_CONF)) { print "Can't update PXE configuration: $!\n" } if (!copy("/tmp/dhcpd.conf.ready", "/etc/dhcpd.conf")) { print "Can't update DHCPD configuration: $!\n" } if (!copy($PXE_KA, $PXE_DEFAULT)) { print "Can't update PXE default configuration files: $!\n" } } else { print "No update, exiting...\n"; exit; } } sub restart_needed_services { map { system("service $_ restart"); $_ } qw(dhcpd pxe xinetd); } sub save_all_conf { map { save_config($_); $_ } $DHCPD_CONF, $PXE_CONF, $PXE_DEFAULT; } sub prepare_chroot_img { my ($rescue) = @_; if (! -e $rescue) { print "Where is the rescue.clp file ?\n"; exit } if (-e "/usr/bin/extract_compressed_fs") { # my $FD; # open($FD, "|extract_compressed_fs /tmp/patched_rescue.clp > /tmp/patched_rescue.iso"); map { ! -d $_ and mkdir("$_") } $mntka, $mnttmp; print "Extract $rescue\n"; system("extract_compressed_fs $rescue > /tmp/rescue.iso"); system("umount $mntka"); print "Mount patched rescue in $mntka\n"; system("mount -o loop /tmp/rescue.iso $mntka"); print "Creating stage2.img\n"; system("dd if=/dev/zero of=stage2.img bs=1M count=45"); system("/sbin/mkfs.ext2 -q stage2.img"); system("mount stage2.img $mnttmp -o loop"); system("cp -a $mntka/* $mnttmp/; sync"); system("umount $mnttmp/"); #system("umount $mntka/"); print "\n$mntka directory is ready (ka method)\n"; print "stage2.img is ready (use it in dolly mode: dolly -v -s -f dol)\n"; print " Typical dolly.cfg file could be: ------------------------------ infile /tmp/stage2.img outfile /dev/ram3 server guibpiv.guibland.com firstclient 10.0.1.34 lastclient 10.0.1.34 clients 1 10.0.1.34 endconfig ------------------------------ "; } else { print "Please install cloop-utils\n"; } } sub install_needed { print "- Needed package are: tftp-server dhcp-server pxe\n"; if (-e "/usr/sbin/urpmi") { my $err = eval { system("urpmi --no-verify-rpm --auto tftp-server pxe dhcp-server cloop-utils") }; # $err = /1/ and exit; } else { print "Your are not running a Mandriva system, i can't install needed package: tft-server, pxe, dhcp-server\n"; } } sub interface_to_ip { my ($interface) = @_; my ($ip) = `/sbin/ip addr show dev $interface` =~ /^\s*inet\s+(\d+\.\d+\.\d+\.\d+)/m; $ip; } sub usage { print "script to auto-configure PXE and DHCPD server ie: $0 -I -p 10.0.1.21 -w 10.0.1.253 -n 10.0.1 -s 10.0.1.253 -t 10.0.1.21 -I: install needed software -p: IP address of the PXE server (should be this computer) -w: IP address of the gateway -n: NET base address for dhcpd conf (ie: 10.0.1) -s: IP address of the DNS server -t: IP address of the tftpserver (with vmlinuz and all.rdz) ie: $0 -c path/rescue.clp You need mount right. -c: prepare chroot with rescue.clp and a stage2.img "; exit; } ##################################### # MAIN # my %opt; getopts("p:w:n:s:t:c:", \%opt) or usage(); $opt{c} or usage; if ($opt{c}) { prepare_chroot_img($opt{c}); } else { # $opt{pxe} and $opt{gw} and $opt{net} and $opt{dns} and $opt{tftp} or usage; save_all_conf; # pxe, tftp prepare_pxe_conf($opt{p}, $opt{t}); # GW, net, tftp, dns prepare_dhcpd_conf($opt{w}, $opt{n}, $opt{t}, $opt{s}); update_conf; install_needed; update_conf; restart_needed_services; }