summaryrefslogtreecommitdiffstats
path: root/pxe_wizard/Pxe.pm
blob: 25c0ef2b72a9de16e5fded236f0062a42cfecc60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/perl -w

# Drakwizard PXE
# release 0.2
# Copyright (C) 2004 Mandrakesoft
#
# Author: Antoine Ginies <aginies _ateuh _ mandrakesoft.com>
#
# 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

package MDK::Wizard::Pxe;
use lib qw(/usr/lib/libDrakX);
use strict;
use common;
use services;
use MDK::Wizard::Wizcommon;

my $wiz = new MDK::Wizard::Wizcommon;

#my $INSTALLDIR = "/var/install/pxe";
#my $IA64PATH = '/IA64PC';

my $TMPDIR = '/tmp';
my $HOSTNAME = chomp_(`hostname`);
my $TFTPDIR = "/var/lib/tftpboot";
my $CLIENTPATH = '/X86PC/linux';
my $X86 = $TFTPDIR . $CLIENTPATH;
my $IMGPATH = $X86 . '/images';
my $PXEHELP = $X86 . '/help.txt';
my $PXEMESSAGE = $X86 . '/messages';
my $PXEDEFAULT = $X86 . '/pxelinux.cfg/default';

my $CONF = '/etc/pxe.conf';
my $SYSLINUXPATH = '/usr/lib/syslinux/';
my $MEMDISK = $SYSLINUXPATH . '/memdisk';
my $XINETDDIR = "/etc/xinetd.d";


my $o = {
	 name => N("PXE Wizard (devel)"),
	 var => {
		 IMG => '/var/install/92/images/network.img',
		 PXENAME => '92',
		 DESCR => 'install 9.2',
		 IMGTOREMOVE => '',
		 IMGTOMODIFY => '',
		 IP => '127.0.0.1',
		 DIR => '/var/install/',
		 i => '',
		},
	 needed_rpm => [ 'pxe', 'tftp-server', 'xinetd' ],
	};

my %level = (
             1 => N("Set PXE server"),
             2 => N("Add boot image in PXE"),
	     3 => N("Remove boot image in PXE"),
	     4 => N("Modify boot image in PXE"),
	    );


# to get list menu entry
my @list_menu;
foreach (cat_($PXEDEFAULT)) {
  my ($ent) = /# TAG: (\w+)_BEGIN/;
  ! $ent or push @list_menu,$ent;
}

#set default list for entry in syslinux.cfg
my @list_method = qw(nfs http);
my @list_ram = qw(32000 64000 1280000);
my @list_vga = qw(788 normal 785 791 text);
my @list_acpi = qw(ht force off);
my @list_apic = qw(apic noapic);
my @list_eth = qw(eth0 eth1 eth2);


# to get list menu entry
my @list_menu;
foreach (cat_($PXEDEFAULT)) {
  my ($ent) = /# TAG: (\w+)_BEGIN/;
  ! $ent or push @list_menu,$ent;
}

#begin of wizard
$o->{pages} = {
	       welcome => {
			   name => N("PXE wizard") . "\n\n" . N("Set a PXE server.") . "\n" . N("This wizard will help you configuring the PXE server. PXE (Pre-boot Execution Environment) is a protocol designed by Intel that allows computers to boot through the network. PXE is stored in the ROM of new generation network cards. When the computer boots up, the BIOS loads the PXE ROM in the memory and executes it. A menu is displayed, allowing the computer to boot an operating system loaded through the network.") . "\n" . N("This wizard will provide a pxe service, and ability to add/remove/modify boot images."),
			   no_back => 1,
			   pre => sub {
			     $o->{var}{wiz_level} ||= 1;
			   },
			   post => sub {
			     if ($o->{var}{wiz_level} == 2) {
			       return 'addimg' }
			     elsif ($o->{var}{wiz_level} == 3) {
			       return 'removeimg' }
			     elsif ($o->{var}{wiz_level} == 4) {
			       return 'modifyimg' }
			   },
			   data => [
				    { label => N("What do you want to do:"), val => \$o->{var}{wiz_level}, list => [ keys %level ], format => sub { $level{$_[0]} } },
				   ],
			   no_back => 1,
			   next => 'pxeserver',
			  },
	       addimg => {
			  name => N("Add a boot image") . "\n\n" . N("Path to image: full path to image (need network boot image)") . "\n\n" . N("PXE name: name in PXE menu (one word/number, no space please)"),
			  data => [
				   { label => "PXE name:", val => \$o->{var}{PXENAME} },
				   { label => "PXE description:", val => \$o->{var}{DESCR} },
				   { label => "Full path to boot image:", val => \$o->{var}{IMG} },
				  ],
			  post => \&test_data,
			  next => 'summaryadd',
			 },
	       removeimg => {
			     name => N("Remove a boot image"). "\n\n" . N("Choose PXE boot image you want to remove from PXE server."),
			     data => [
				      { label => N("Boot image to remove:"), val => \$o->{var}{IMGTOREMOVE}, fixed_list => \@list_menu },
				     ],
			     next => 'summaryremove',
			    },
	       modifyimg => {
			     name => N("Add option to boot image:") . "\n\n" . N("Please choose PXE boot image to modify"),
			     data => [
				      { label => N("Boot image to configure:"), val => \$o->{var}{IMGTOMODIFY}, fixed_list => \@list_menu },
				     ],
			     post => \&read_option,
			     next => 'gomodify',
			    },
	       gomodify => {
			    name => N("Options to add to PXE boot disk") . "\n" . N("Server IP: IP address of server wich contain installation directory. You can create one with MDK install server wizard.") . "\n" . N("Install directory: full path to MDK install server directory") . "\n" . N("Installation method: choose nfs/http to install via nfs/http.") . "\n" . N("Network client interface: through wich interface client should be installed.") . "\n" . N("Ramsize: adjust ramsize on boot disk.") . "\n" . N("VGA option: if you encounter some problem whith VGA, please adjust. ") . "\n" . N("Acpi option: Advanced Configuration and Power Interface"),
			    data => [
				     { label => N("Boot image to modify:"), fixed_val => \$o->{var}{i} },
				     { label => N("Server IP:"), val => \$o->{var}{IP} },
				     { label => N("Install directory:"), val => \$o->{var}{DIR} },
				     { label => N("Installation method:"), val => \$o->{var}{METHOD}, fixed_list => \@list_method },
				     { label => N("Network client interface:"), val => \$o->{var}{ETH}, fixed_list => \@list_eth },
				     { label => N("Ramsize:"), val => \$o->{var}{RAM}, fixed_list => \@list_ram },
				     { label => N("VGA option:"), val => \$o->{var}{VGA}, fixed_list => \@list_vga },
				     { label => N("Acpi option:"), val => \$o->{var}{ACPI}, fixed_list => \@list_acpi },
				     { label => N("Apic option:"), val => \$o->{var}{APIC}, fixed_list => \@list_apic },
				    ],
			    next => 'summarymodify',
			   },
	       pxeserver => {
			     name => N("Set PXE server") . "\n\n" . N("We need to use a special dhcpd.conf with PXE parameter.") . "\n" . N("This will configure all needed default configurations files to set a PXE server."),
			     next => 'summaryserver',
			    },
	       error_img => {
			     name => N('Please provide a bootable image...'),
			     next => 'addimg',
			    },
	       error_name => {
			      name => N('Please provide a correct name in PXE entry (one word)'),
			      next => 'addimg',
			     },
	       simmilar_entry => {
				  name => N('Similar name is already used in PXE menu entry') . "\n\n" . N("Please provide another PXE Menu name"),
				  next => 'addimg',
				 },
	       summaryserver => {
				 name => N('Now will prepare all default files to set the PXE server'),
				 data => [
					  { label => N("Tftp directory:"), fixed_val => \$TFTPDIR },
					  { label => N("Boot image path:"), fixed_val => \$IMGPATH },
					  { label => N("PXE \'default\' file:"), fixed_val => \$PXEDEFAULT },
					  { label => N("PXE \'help\' file:"), fixed_val => \$PXEHELP },
					 ],
				 post => \&do_it_pxe,
				 next => 'endserver',
				},
	       summarymodify => {
				 name => N('Now will modify boot options in image'),
				 data => [
					  { label => N("Boot image to modify:"), fixed_val => \$o->{var}{i} },
					  { label => N("Server IP:"), fixed_val => \$o->{var}{IP} },
					  { label => N("Install directory:"), fixed_val => \$o->{var}{DIR} },
					  { label => N("Installation method:"), fixed_val => \$o->{var}{METHOD} },
					  { label => N("Network client interface:"), fixed_val => \$o->{var}{ETH} },
					  { label => N("Ramsize:"), fixed_val => \$o->{var}{RAM} },
					  { label => N("VGA option:"), fixed_val => \$o->{var}{VGA} },
					  { label => N("Acpi option:"), fixed_val => \$o->{var}{ACPI} },
					  { label => N("Apic option:"), fixed_val => \$o->{var}{APIC} },
					 ],
				 post => \&do_it_modify,
				 next => 'endmodify',
				},
	       summaryremove => {
				 name => N('Now will remove your PXE boot image'),
				 data => [
					  { label => N("PXE entry to remove:"), fixed_val => \$o->{var}{IMGTOREMOVE} },
					 ],
				 post => \&do_it_remove,
				 next => 'endremove',
				},
	       summaryadd => {
			      name => N('Now will add your PXE boot image'),
				 data => [
					  { label => "PXE name:", fixed_val => \$o->{var}{PXENAME} },
					  { label => "PXE description:", fixed_val => \$o->{var}{DESCR} },
					  { label => "Path to boot image:", fixed_val => \$o->{var}{IMG} },
					 ],
			      post => \&do_it_add,
			      next => 'endadd',
			     },
	       endadd => {
			     name => N('Congratulations'),
			     data => [ { label => N('The wizard successfully add a PXE boot image.') } ],
                             next => 'welcome',
			    },
	       endremove => {
			     name => N('Congratulations'),
			     data => [ { label => N('The wizard successfully remove a PXE boot image.') } ],
                             next => 'welcome',
			    },
	       endmodify => {
			     name => N('Congratulations'),
			     data => [ { label => N('The wizard successfully modify image(s).') } ],
                             next => 'welcome',
			    },
	       endserver => {
			     name => N('Congratulations'),
			     data => [ { label => N('The wizard successfully configured your PXE server.') } ],
                             no_back => 1,
                             end => 1,
                             next => 0
			    },
	      };


#cp memlinux from syslinux
sub memlinux_prep {
  if (!-f "$X86/memdisk") {
    cp_af($MEMDISK, $X86);
  }
}

# test img and similar entry in PXE
sub test_data {
  $o->{var}{PXENAME} =~ /^\w+$/ or return 'error_name';
  if (!-f $o->{var}{IMG}) {
    return 'error_img'
  }
  if (any { /^$o->{var}{PXENAME}/ } cat_($PXEHELP)) {
    return 'simmilar_entry'
  }
}

# enable tftp server
sub enable_tftps {
  substInFile { s/disable.*/disable = no/ } "$XINETDDIR/tftp";
}

# can change option in tftp server
sub tftp_blksize {
  # $o should be with W or not N
  my ($o) = @_;
  if ($o =~ /W/) {
    substInFile { s/server_args.*/server_args = -r blksize -s $TFTPDIR/ } "$XINETDDIR/tftp";
  } else {
    substInFile { s/server_args.*/server_args = -s $TFTPDIR/ } "$XINETDDIR/tftp";
  }
}


sub crea_wdir {
  if (-e $TMPDIR) { rm_rf($TMPDIR);
	     }
  mkdir_p($TMPDIR);
}

# add TAGGED entry in PXE menu 
sub add_in_default {
  my ($NAME) = @_;
  if (!any { /${NAME}_BEGIN/ } cat_($PXEDEFAULT)) {
    append_to_file($PXEDEFAULT ,<<EOF);
# TAG: ${NAME}_BEGIN
label $NAME
      KERNEL memdisk
      APPEND initrd=images/$NAME.img
# TAG: ${NAME}_END
EOF
  }
}

# add entry in Help.txt of  PXE
sub add_in_help {
  my ($NAME, $DESCR) = @_;
  append_to_file($PXEHELP ,<<EOF);
$NAME : $DESCR
EOF
}

# add bootable image in correct place
sub add_img {
  my ($SIMG, $PXENAME) = @_;
  cp_af($SIMG, $IMGPATH . "/" . $PXENAME . '.img');
}

# remove entry in PXE menu
sub remove_in_menu {
  my ($NAME) = @_;
  my $conf = cat_($PXEDEFAULT) . "#EndOfFile";
  my ($begin, $NAME_bloc, $end) = $conf =~ /(.*?)(# TAG: ${NAME}_BEGIN.*?# TAG: ${NAME}_END)\n(.*)#EndOfFile/s or die "unable to find Menu entry section";
  output($PXEDEFAULT, $begin, $end);
}

# get list of available PXE boot images
sub list_menu {
  my @list_menu;
  foreach (cat_($PXEDEFAULT)) {
    my ($ent) = /\# TAG: (\w+)_BEGIN/;
    ! $ent or push @list_menu,$ent; #print "$ent\n";
  }
  @list_menu;
}

# remove entry in help.txt
sub remove_in_help {
  my ($NAME) = @_;
  substInFile {
    s/^$NAME\s:.*//x;
    s/^\s*$//;
  } $PXEHELP;
}

sub remove_img {
  my ($NAME) = @_;
  rm_rf($IMGPATH . "/" . $NAME . '.img');
}

sub save_config {
  my ($FILE) = @_;
  if (!-f "$FILE.ori") {
    cp_af($FILE, "$FILE.ori");
  }
}

# set default help.txt file
sub default_pxe_menu {
  save_config($PXEHELP);
  output($PXEHELP, <<EOF);
Available images are:
---------------------
local: local boot
EOF

  save_config($PXEDEFAULT);
  output($PXEDEFAULT, <<EOF);
PROMPT 1
DEFAULT local
DISPLAY messages
TIMEOUT 50
F1 help.txt
label local
      LOCALBOOT 0

EOF
}

# set default pxe messages
sub default_pxe_messages {
  save_config($PXEMESSAGE);
  output($PXEMESSAGE, <<EOF);

                   Welcome to Mandrake Linux PXE Server
                                                       Pxelinux
              .              .-----------------------------------.
             /|\\            /    Press F1 for available images    \\
            /_|_\\           \\    Hosted by  $HOSTNAME
            \\ | /   _       /'-----------------------------------'
             \\|/   (')     /
              '.    U     /                                (O__
                 .   '.  /                 (o_  (o_  (0_   //\\
                 {o_   (o_  (o_  (o_  (o_  //\\  //\\  //\\  //  )
                 (')_  (`)_ (/)_ (/)_ (/)_ V_/_ V_/_ V_/_ V__/_
            ---------------------------------------------------------

 press F1 for help
EOF

}

# main add image in PXE menu
sub do_it_add {
  return if $::testing;
  add_in_help($o->{var}{PXENAME}, $o->{var}{DESCR});
  add_in_default($o->{var}{PXENAME});
  add_img($o->{var}{IMG}, $o->{var}{PXENAME});
}

# main remove image
sub do_it_remove {
  return if $::testing;
  remove_in_menu($o->{var}{IMGTOREMOVE});
  remove_in_help($o->{var}{IMGTOREMOVE});
  remove_img($o->{var}{IMGTOREMOVE});
}

# get default option for an image
sub read_option {
  my ($IMG) = $o->{var}{IMGTOMODIFY};
  my $mount = "/tmp/loop_for_images";
  mkdir_p($mount);
  system("mount $IMGPATH/$IMG.img $mount -o loop");
  my $line = cat_("$mount/syslinux.cfg");
  $o->{var}{i} = "$IMGPATH/$IMG.img";
  ($o->{var}{METHOD}, $o->{var}{ETH}, $o->{var}{IP}, $o->{var}{DIR}, $o->{var}{RAM}, $o->{var}{VGA}, $o->{var}{ACPI}, $o->{var}{APIC}) = $line =~ /method:(nfs|http),interface:(eth0|eth1|eth2),network:dhcp,server:(.*?),directory:(.*?) ramdisk_size=(.*?) root=\/dev\/ram3 rw vga=(.*?)acpi=(ht|force|off)\s(apic|noapic)/;
  system("umount $mount");
  if (-e $mount) {
    rm_rf($mount);
  }
}

# modify default boot option of an image
sub do_it_modify {
  my ($IMG, $METHOD, $ETH, $IP, $DIR, $RAM, $VGA, $ACPI, $APIC) = ($o->{var}{IMGTOMODIFY}, $o->{var}{METHOD}, $o->{var}{ETH}, $o->{var}{IP}, $o->{var}{DIR}, $o->{var}{RAM}, $o->{var}{VGA}, $o->{var}{ACPI}, $o->{var}{APIC});
  $o->{var}{i} = "$IMGPATH/$IMG.img";
  return if $::testing;
#APPEND initrd=images/ken/cooker/i586/isolinux/alt0/all.rdz automatic=method:nfs,interface:eth0,network:dhcp,server:192.168.100.7,directory:/c/cooker ramdisk_size=32000 root=/dev/ram3 rw vga=788 meta_class=discovery
  my $mount = "/tmp/loop_for_images";
  mkdir_p($mount);
  system("mount $o->{var}{i} $mount -o loop");
  output("$mount/syslinux.cfg", <<EOF);
default linux
prompt 1
timeout 72
display boot.msg
F1 help.msg
F2 advanced.msg
F3 boot.msg
label linux
  kernel vmlinuz
  append initrd=network.rdz automatic=method:$METHOD,interface:$ETH,network:dhcp,server:$IP,directory:$DIR ramdisk_size=$RAM root=/dev/ram3 rw vga=$VGA acpi=$ACPI $APIC
EOF

  system("umount $mount");
  if (-e $mount) {
    rm_rf($mount);
  }
}

# set PXE server
sub do_it_pxe {
  return if $::testing;
  my $in = 'interactive'->vnew('su', 'PXE server');
  my $w = $in->wait_message(N("PXE server"), N("Configuring PXE server on your system..."));
  mkdir_p($IMGPATH);
  memlinux_prep();
  default_pxe_menu();
  default_pxe_messages();
  enable_tftps();
  tftp_blksize('N');
  map { system("service $_ restart"); $_ } qw(xinetd pxe);
  undef $w;
}


sub new {
    my ($class, $conf) = @_;
    bless {
            o   => $o,
	   }, $class;
}

1;