| 1 | #!/usr/bin/perl -w |
|---|
| 2 | # |
|---|
| 3 | # $Id$ |
|---|
| 4 | # |
|---|
| 5 | # (c) Bruno Cornec, HP |
|---|
| 6 | # (c) Gallig Renaud, HP |
|---|
| 7 | # |
|---|
| 8 | # Provided under the GPL v2 |
|---|
| 9 | |
|---|
| 10 | use strict; |
|---|
| 11 | use Pod::Usage; |
|---|
| 12 | use Getopt::Long qw(:config auto_abbrev no_ignore_case); |
|---|
| 13 | use Data::Dumper; |
|---|
| 14 | use File::Copy; |
|---|
| 15 | use File::Basename; |
|---|
| 16 | use ProjectBuilder::Base; |
|---|
| 17 | use ProjectBuilder::Conf; |
|---|
| 18 | use Dploy::Base; |
|---|
| 19 | |
|---|
| 20 | =pod |
|---|
| 21 | |
|---|
| 22 | =head1 NAME |
|---|
| 23 | |
|---|
| 24 | dploy-add2pxe - Update PXE configuration file for dploy.org |
|---|
| 25 | |
|---|
| 26 | =head1 DESCRIPTION |
|---|
| 27 | |
|---|
| 28 | dploy-add2pxe helps you keeping your PXE configuration up to date |
|---|
| 29 | |
|---|
| 30 | =head1 SYNOPSIS |
|---|
| 31 | |
|---|
| 32 | dploy-add2pxe -t mondorescue|linuxcoe -m MAC-address -u ISO-image-URL |
|---|
| 33 | |
|---|
| 34 | dploy-add2pxe --type mondorescue|linuxcoe --mac MAC-address --url ISO-image-URL |
|---|
| 35 | |
|---|
| 36 | =head1 OPTIONS |
|---|
| 37 | |
|---|
| 38 | =over 4 |
|---|
| 39 | |
|---|
| 40 | =item B<-h|--help> |
|---|
| 41 | |
|---|
| 42 | Print a brief help message and exits. |
|---|
| 43 | |
|---|
| 44 | =cut |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | my %opts; # GetOpt Hash |
|---|
| 48 | my $url = undef; # url of the boot ISO image |
|---|
| 49 | my $ksdevice = "eth0"; # NIC device to boot on |
|---|
| 50 | my $prefix = undef; # Prefix of the MondoRescue image |
|---|
| 51 | my $cliparams; # Command Line parames of the MondoRescue image |
|---|
| 52 | my $destdir = undef; # Destination driectory for tftp files |
|---|
| 53 | |
|---|
| 54 | pb_syntax_init("dploy-add2pxe Version PBVER-PBREV\n"); |
|---|
| 55 | |
|---|
| 56 | GetOptions("help|?|h" => \$opts{'h'}, |
|---|
| 57 | "type|t=s" => \$opts{'type'}, # Type is MondoRescue or LinuxCOE |
|---|
| 58 | "mac|m=s" => \$opts{'mac'}, # Mac address |
|---|
| 59 | "url|u=s" => \$opts{'url'}, # URL of ISO image containing kernel, modules and answer file |
|---|
| 60 | ) || pb_syntax(-1,0); |
|---|
| 61 | |
|---|
| 62 | if (defined $opts{'h'}) { |
|---|
| 63 | pb_syntax(0,1); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | # type is always lowercase |
|---|
| 67 | $opts{'type'} = lc($opts{'type'}); |
|---|
| 68 | |
|---|
| 69 | my $dploydir = "PBCONF"; |
|---|
| 70 | pb_conf_add("$dploydir/dploy.conf"); |
|---|
| 71 | |
|---|
| 72 | # Root of the PXE setup |
|---|
| 73 | my ($pxe)=pb_conf_get("pathpxe"); |
|---|
| 74 | my $pathpxe = $pxe->{"default"}; |
|---|
| 75 | |
|---|
| 76 | # temporary mountpoint |
|---|
| 77 | pb_temp_init(); |
|---|
| 78 | my $mnt="$ENV{'PBTMP'}/iso"; |
|---|
| 79 | my $mnt2="$ENV{'PBTMP'}/nfs"; |
|---|
| 80 | |
|---|
| 81 | # Check Mac consitency |
|---|
| 82 | my $mac = dploy_check_mac($opts{'mac'}); |
|---|
| 83 | |
|---|
| 84 | my ($scheme, $account, $mrserver, $port, $mrpath) = pb_get_uri($opts{'url'}); |
|---|
| 85 | |
|---|
| 86 | # Get the ID given by LinuxCOE/MondoRescue to prepare kernel/initrd/ks |
|---|
| 87 | my $iso=basename($opts{'url'}); |
|---|
| 88 | my $id=$iso; |
|---|
| 89 | $id =~ s/img([0-9]+)\.iso/$1/; |
|---|
| 90 | my $k="k-$id"; |
|---|
| 91 | my $i="i-$id"; |
|---|
| 92 | my $ks="ks-$id.cfg"; |
|---|
| 93 | |
|---|
| 94 | # mount the ISO to get kernel and initrd |
|---|
| 95 | mkdir "$mnt",0755; |
|---|
| 96 | if ($opts{'type'} =~ /linuxcoe/) { |
|---|
| 97 | my $bn = basename($mrpath); |
|---|
| 98 | unlink("$ENV{'PBTMP'}/$bn"); |
|---|
| 99 | pb_system("cd \$PBTMP ; wget $opts{'url'}","Downloading $opts{'url'}"); |
|---|
| 100 | pb_system("sudo /bin/mount -o loop $ENV{'PBTMP'}/$bn $mnt","Mounting $bn"); |
|---|
| 101 | } elsif ($opts{'type'} =~ /mondorescue/) { |
|---|
| 102 | pb_system("sudo /bin/mount -t nfs $mrserver:$mrpath $mnt2","Mountig NFS $mrserver:$mrpath"); |
|---|
| 103 | pb_system("sudo /bin/mount -o loop $mnt2/$prefix-1.iso $mnt","Mounting LOOP $mnt2/$prefix-1.iso"); |
|---|
| 104 | } else { |
|---|
| 105 | # Type passed has parameter does not contain a valid value |
|---|
| 106 | pb_syntax(-1.0); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | # Install script will have to check (and create if not existing) /tftpboot/tmp |
|---|
| 110 | # $pathpxe is normaly setup correctly by package install |
|---|
| 111 | $destdir = "$pathpxe/../$opts{'type'}"; |
|---|
| 112 | copy("$mnt/isolinux/vmlinuz","$destdir/$k") or die "Copy of $mnt/isolinux/vmlinuz to $destdir/$k failed: $!"; |
|---|
| 113 | copy("$mnt/isolinux/initrd.img","$destdir/$i") or die "Copy of $mnt/isolinux/initrd.img to $destdir/$i failed: $!"; |
|---|
| 114 | if ($opts{'type'} =~ /linuxcoe/) { |
|---|
| 115 | copy("$mnt/KS.CFG","$destdir/$ks") or die "Copy of $mnt/KS.CFG to $destdir/$ks failed: $!"; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | # Get the Command Line parameters for MondoRescue restoration |
|---|
| 119 | if ($opts{'type'} =~ /mondorescue/) { |
|---|
| 120 | open(ISOLINUX, "$mnt/isolinux.cfg") || die "Unable to open $mnt/isolinux.cfg for writing"; |
|---|
| 121 | while ( <ISOLINUX> ) { |
|---|
| 122 | if (/label nuke/) { |
|---|
| 123 | $cliparams = <ISOLINUX>; # kernel line |
|---|
| 124 | $cliparams = <ISOLINUX>; # append line |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | $cliparams =~ s/initrd=[A-z.] /initrd=mondorescue\/$i /; |
|---|
| 128 | close ISOLINUX; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | # All done with ISO image... umounting |
|---|
| 132 | pb_system("sudo /bin/umount $mnt","Umount ISO image"); |
|---|
| 133 | if ($opts{'type'} =~ /mondorescue/) { |
|---|
| 134 | pb_system("sudo /bin/umount $mnt2","Unmountig NFS $mrserver:$mrpath"); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | # Updates PXElinux |
|---|
| 138 | open(PXE, "> $pathpxe/01-$mac") || die "Unable to open $pathpxe/01-$mac for writing"; |
|---|
| 139 | print PXE << "EOF"; |
|---|
| 140 | default local |
|---|
| 141 | prompt 1 |
|---|
| 142 | timeout 600 |
|---|
| 143 | display msg/linuxcoe.msg |
|---|
| 144 | allowoptions 1 |
|---|
| 145 | |
|---|
| 146 | label local |
|---|
| 147 | localboot 0 |
|---|
| 148 | |
|---|
| 149 | EOF |
|---|
| 150 | |
|---|
| 151 | # Customize pxe file depending on Job type submited |
|---|
| 152 | if ($opts{'type'} =~ /linuxcoe/) { |
|---|
| 153 | print PXE << "EOF"; |
|---|
| 154 | label linuxcoe |
|---|
| 155 | kernel linuxcoe/$k |
|---|
| 156 | append ksdevice=$ksdevice ks=$ks initrd=linuxcoe/$i text |
|---|
| 157 | EOF |
|---|
| 158 | } else { |
|---|
| 159 | print PXE << "EOF"; |
|---|
| 160 | label mondorescue |
|---|
| 161 | kernel mondorescue/$k |
|---|
| 162 | append $cliparams pxe prefix=$prefix ipconf=$ksdevice:dhcp nfsmount=$mrserver:$mrpath |
|---|
| 163 | EOF |
|---|
| 164 | } |
|---|
| 165 | close(PXE); |
|---|