source: devel/dploy-pxe/bin/dploy-add2pxe @ 60

Revision 60, 4.4 KB checked in by bruno, 4 years ago (diff)

Fix remaining typos. First working dploy-add2pxe. However ks isn't managed correctly yet.

  • Property svn:executable set to *
Line 
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
10use strict;
11use Pod::Usage;
12use Getopt::Long qw(:config auto_abbrev no_ignore_case);
13use Data::Dumper;
14use File::Copy;
15use File::Basename;
16use ProjectBuilder::Base;
17use ProjectBuilder::Conf;
18use Dploy::Base;
19
20=pod
21
22=head1 NAME
23
24dploy-add2pxe - Update PXE configuration file for dploy.org
25
26=head1 DESCRIPTION
27
28dploy-add2pxe helps you keeping your PXE configuration up to date
29
30=head1 SYNOPSIS
31
32dploy-add2pxe -t mondorescue|linuxcoe -m MAC-address -u ISO-image-URL
33
34dploy-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
42Print a brief help message and exits.
43
44=cut
45
46
47my %opts;                                       # GetOpt Hash
48my $url = undef;                        # url of the boot ISO image
49my $ksdevice = "eth0";          # NIC device to boot on
50my $prefix = undef;                     # Prefix of the MondoRescue image
51my $cliparams;                          # Command Line parames of the MondoRescue image
52my $destdir = undef;            # Destination driectory for tftp files
53
54pb_syntax_init("dploy-add2pxe Version PBVER-PBREV\n");
55
56GetOptions("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
62if (defined $opts{'h'}) {
63    pb_syntax(0,1);
64}
65
66# type is always lowercase
67$opts{'type'} = lc($opts{'type'});
68
69my $dploydir = "PBCONF";
70pb_conf_add("$dploydir/dploy.conf");
71
72# Root of the PXE setup
73my ($pxe)=pb_conf_get("pathpxe");
74my $pathpxe = $pxe->{"default"};
75
76# temporary mountpoint
77pb_temp_init();
78my $mnt="$ENV{'PBTMP'}/iso";
79my $mnt2="$ENV{'PBTMP'}/nfs";
80
81# Check Mac consitency
82my $mac = dploy_check_mac($opts{'mac'});
83
84my ($scheme, $account, $mrserver, $port, $mrpath) = pb_get_uri($opts{'url'});
85
86# Get the ID given by LinuxCOE/MondoRescue to prepare kernel/initrd/ks
87my $iso=basename($opts{'url'});
88my $id=$iso;
89$id =~ s/img([0-9]+)\.iso/$1/;
90my $k="k-$id";
91my $i="i-$id";
92my $ks="ks-$id.cfg";
93
94# mount the ISO to get kernel and initrd
95mkdir "$mnt",0755;
96if ($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'}";
112copy("$mnt/isolinux/vmlinuz","$destdir/$k") or die "Copy of $mnt/isolinux/vmlinuz to $destdir/$k failed: $!";
113copy("$mnt/isolinux/initrd.img","$destdir/$i") or die "Copy of $mnt/isolinux/initrd.img to $destdir/$i failed: $!";
114if ($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
119if ($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
132pb_system("sudo /bin/umount $mnt","Umount ISO image");
133if ($opts{'type'} =~ /mondorescue/) {
134        pb_system("sudo /bin/umount $mnt2","Unmountig NFS $mrserver:$mrpath");
135}
136
137# Updates PXElinux
138open(PXE, "> $pathpxe/01-$mac") || die "Unable to open $pathpxe/01-$mac for writing";
139print PXE << "EOF";
140default local
141prompt 1
142timeout 600
143display msg/linuxcoe.msg
144allowoptions 1
145
146label local
147  localboot 0
148
149EOF
150
151# Customize pxe file depending on Job type submited
152if ($opts{'type'} =~ /linuxcoe/) {
153        print PXE << "EOF";
154label linuxcoe
155  kernel linuxcoe/$k
156  append ksdevice=$ksdevice ks=$ks initrd=linuxcoe/$i text
157EOF
158} else {
159        print PXE << "EOF";
160label mondorescue
161  kernel mondorescue/$k
162  append $cliparams pxe prefix=$prefix ipconf=$ksdevice:dhcp nfsmount=$mrserver:$mrpath
163EOF
164}
165close(PXE);
Note: See TracBrowser for help on using the repository browser.