Debian Codegraph @

bin/fai-chboot

raw
1 #! /usr/bin/perl
2
3 #*********************************************************************
4 #
5 # fai-chboot -- manage configuration for network boot
6 #
7 # This script is part of FAI (Fully Automatic Installation)
8 # Copyright (C) 2003-2025 Thomas Lange, [email protected]
9 # Universitaet zu Koeln
10 #
11 #*********************************************************************
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # General Public License for more details.
21 #
22 # A copy of the GNU General Public License is available as
23 # '/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
24 # or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
25 # can also obtain it by writing to the Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 #*********************************************************************
28
29 # variable needed: $nfsroot
30
31 use strict;
32 use Socket;
33 use Net::hostent;
34 use Getopt::Std;
35 use File::Copy;
36
37 our ($opt_D,$opt_p,$opt_h,$opt_t,$opt_s,$opt_C,$opt_P,$opt_E,$opt_c,$opt_l,$opt_d,$opt_i,$opt_S,$opt_L,$opt_B,$opt_I,$opt_n,$opt_v,$opt_e,$opt_F,$opt_f,$opt_k,$opt_g,$opt_o,$opt_u,$opt_U,$opt_q,$opt_a);
38 our ($error,$pxedir,$nfsroot,$nfsroot_server,$tftproot,$verbose,$append,$action,$kernelsuffix);
39 our ($mac,$ipadr,$cfdir,$flags,$debug,$match,$bootprot);
40 our (%hname,%hexname,%type,@allfiles,@templates,@enabled,@other,@patterns,@disabled);
41 our ($initrd,$kernelname,$rootfs_pre,$rootfs);
42 $Getopt::Std::STANDARD_HELP_VERSION=1;
43
44 $0=~ s#.+/##; # remove path from program name
45 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46 sub ip2hex {
47
48 my $ipadr = shift;
49 my $hex = sprintf("%02X%02X%02X%02X", split(/\./,$ipadr));
50 return ($ipadr,$hex);
51 }
52 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53 sub host2hex {
54
55 my $host = shift;
56 my $h;
57
58 if ($host =~ /^\d+\.\d+\.\d+\.\d+$/) {
59 # hostname is already an IP address
60 return ip2hex($host);
61 }
62 return ('no IP','default') if ($host =~ /^default/);
63 return ('no IP',"$host") if ($host =~ /\.tmpl$/);
64
65 # MAC address with separator "-" or ":"
66 if ($host =~ /^([0-9a-fA-F]{2}([:-]|$)){6}$/i) { # pattern matching
67 $host = lc $host; # lowercase for pxe-file
68 my @nums = split /[:-]/, $host;
69 $mac = join ('-', "01",@nums); # build pxe-filename
70 return ('no IP',$mac);
71 }
72
73 if ($host =~ /^[0-9a-fA-F]{12}$/i) { # pattern matching MAC address w/o separator
74 $host = lc $host;
75 my @nums = split /(.{2})/, $host;
76 $mac = join ('-', "01",@nums[1,3,5,7,9,11]); # build pxe-filename
77 return ('no IP',$mac);
78 }
79
80 $h = gethost($host);
81 die "$0: unknown host: $host\n" unless $h;
82
83 if ( @{$h->addr_list} > 1 ) {
84 my $i;
85 for my $addr ( @{$h->addr_list} ) {
86 $ipadr = inet_ntoa($addr);
87 printf "$host \taddr #%d is [%s]\n", $i++, $ipadr if $debug;
88 }
89 } else {
90 $ipadr = inet_ntoa($h->addr);
91 printf "$host \taddress is [%s]\n", $ipadr if $debug;
92 }
93 ip2hex($ipadr);
94 }
95 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96 sub readpxedir {
97
98 # read all files in pxedir and add them to different arrays
99
100 opendir(DIR, $pxedir) || die "Can't opendir $pxedir: $!";
101 foreach (readdir(DIR)) {
102 next if /^\./;
103 next if /~$/;
104 next if /\.bak$/;
105 if (/^(default|[0-9A-F]+)$/) { push @enabled, $_ ; next}
106 if (/\.tmpl$/) { push @templates, $_ ; next}
107 if (/\.disable$/) {
108 my $filename=(split (/\./,$_))[0];
109 if (-f "$pxedir/$filename") {
110 warn "Skipping $_ as disabled host because also enabled.\n" if $verbose;
111 } else {
112 push @disabled, $_;
113 }
114 next;
115 }
116 push @other, $_;
117 }
118 }
119 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120 sub lsdir {
121
122 # -ld list only disabled hosts
123 # -le list only enabled hosts
124 # -lt list only templates
125 # -lo list only "other", i.e that does not match templates or hosts
126 # -lg list by goups (enabled, disabled, templates, others)
127 # -l[detog] <pattern> list matching pattern
128
129 my ($h,$n,$host,$iaddr,$filename,$type);
130
131 @patterns = @_; # a global variable
132
133 readpxedir();
134
135 # create list which entries we want to list
136 @allfiles = (@enabled,@disabled,@templates,@other);
137 $opt_d and @allfiles = @disabled;
138 $opt_e and @allfiles = @enabled;
139 $opt_t and @allfiles = @templates;
140 $opt_o and @allfiles = @other;
141 $opt_g and @allfiles = (@enabled,@disabled,@templates,@other);
142
143 # map all entries (in HEX) to hostname or IP
144 foreach $filename (@allfiles) {
145 undef $host;
146 undef $type;
147
148 if ($filename =~ /^default(.disable)?$/) {
149 $host = $filename;
150 $type = '[DEFAULT]';
151 }
152 if ($filename =~ /\.tmpl$/) {
153 $host = "$filename";
154 $type = '[Template]';
155 }
156
157 if ($filename =~ /^01-[0-9A-F-]{17}$/i) {
158 $host = "$filename";
159 $type = '[MAC address]';
160 }
161
162 if (($filename =~ /^[0-9A-F]+/) && ($filename !~ /^01-/) ) {
163 my $hexstrip = (split /\./,$filename)[0]; # remove .disable if necessary
164 $n = $hexstrip;
165 # hex to ip/subnet address
166 while ( length $n ) {
167 $host = sprintf( "%s%d" ,$host?"$host.":$host,
168 (hex substr $n,0,1,"" ) * 16 + (hex substr $n,0,1,""));
169 }
170 if ( $host and ( length($hexstrip) < 8) ) {
171 $host = "Subnet: $host/". length($hexstrip)*4
172 } else {
173 # ip to hostname
174 $iaddr = inet_aton($host);
175 if ($h = gethostbyaddr($iaddr, AF_INET)) {
176 $host = $h->name;
177 $host =~ s/^([^.]+).*/$1/; # strip domain from FQDN so we have short hostnames
178 }
179 }
180 }
181
182 if ( !$host ) {
183 $host = $filename;
184 $type = '[Other]';
185 }
186
187 $hexname{$host} = $filename;
188 $hname{$filename}= $host;
189 $type{$filename} = $type if $type;
190 }
191
192 if ($opt_g) { # print in group, sorted inside each group
193 prtsorted(@enabled);
194 prtsorted(@disabled);
195 prtsorted(@templates);
196 prtsorted(@other);
197 exit 0;
198 }
199
200 prtsorted(keys %hname);
201 exit 0;
202 }
203 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
204 sub prtsorted {
205
206 my @list = @_;
207
208 # sort by readable host names
209 @list = sort { $hname{$a} cmp $hname{$b} } @list;
210
211 foreach (@list) {
212 printpxe ($hname{$_},$_, $type{$_});
213 }
214 }
215 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
216 sub printpxe {
217
218 my ($host,$hex, $type) = @_;
219 my ($kernelname,$append);
220
221 $match = (@patterns) ? 0: 1; # set match to 1 if no pattern is given
222 foreach (@patterns) {
223 $match = 1 if $host =~ /$_/;
224 }
225 return unless $match; # do not print entries if no pattern matches
226
227 # read pxe config file for a host
228 undef $kernelname;
229 open (CFG,"$pxedir/$hex") || die "$! $@\n";
230 while (<CFG>) {
231 next if /^#/;
232 /\bkernel\s+(\S+)/ and $kernelname = $1;
233 /\b(localboot.+)/ and $kernelname = $1;
234 /\bappend\s+(.+)/ and $append = $1;
235 }
236 close (CFG);
237
238 if ($opt_q) {
239 printf "%-16.16s $pxedir/$hex \n",$host;
240 } elsif ($opt_l && ! $opt_L) {
241 if ($append =~ /FAI_ACTION=(\S+)/) {
242 $append = $1;
243 } else {
244 $append = 'NOACTION';
245 }
246 # printf "%-16.16s $append $kernelname %-8s\n",$host,$hex;
247 printf "%-26.26s %-22.22s $append $kernelname\n",$type?$type:$host,$hex;
248 } else {
249 printf "%s %s $kernelname $append\n",$type?$type:$host,$hex;
250 }
251
252 undef $append;
253 undef $kernelname;
254 }
255
256 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
257 sub usage {
258
259 &HELP_MESSAGE;
260 exit 0;
261 }
262 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
263 sub HELP_MESSAGE {
264
265 print << "EOM";
266 Please read the manual pages fai-chboot(8).
267 EOM
268 }
269 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
270 sub mkpxecfg {
271
272 my ($host,$kernel,$rootfs,$initrd) = @_;
273 my ($ipadr,$hex) = host2hex($host);
274
275 warn "$host has $ipadr in hex $hex\n" if $verbose;
276 if (-f "$pxedir/$hex.disable") {
277 unlink "$pxedir/$hex.disable";
278 print "removed old $pxedir/$hex.disable\n";
279 }
280 warn "Writing file $pxedir/$hex for $host\n" if $verbose;
281 return if $opt_n;
282
283 if ($opt_p && -e "$pxedir/$hex") {
284 warn "WARNING: $pxedir/$hex already exists.\nSkipping file creation. ";
285 return;
286 }
287 open (FILE,"> $pxedir/$hex") or warn "$0 $@ $!";
288 print FILE << "EOM";
289 default fai-generated
290
291 label fai-generated
292 $kernel
293
294 # generated by fai-chboot for host $host with IP $ipadr
295 EOM
296
297 $append="append $initrd $bootprot $rootfs $opt_k $flags $action\n";
298 print FILE "$append" unless ($append =~ /append\s+$/);
299 print FILE "IPAPPEND 6\n" if $opt_P;
300 close FILE;
301 }
302 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
303 sub disable {
304
305 # rename network config file
306 my ($host) = shift;
307 my ($ipadr,$hex) = host2hex($host);
308 if (! -e "$pxedir/$hex") {
309 print "$host ($hex) is not enabled\n";
310 return;
311 }
312 print "disable pxe config for $host in hex $hex\n" if $verbose;
313 return if $opt_n;
314 rename "$pxedir/$hex","$pxedir/$hex.disable" or $error .= "\nRename for $hex failed. $! $@";
315 }
316 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
317 sub enable {
318
319 # rename network config file
320 my ($host) = shift;
321 my ($ipadr,$hex) = host2hex($host);
322
323 -e "$pxedir/$hex" and print "$host ($hex) is already enabled\n" and return;
324 if (! -e "$pxedir/$hex.disable") {
325 print "$host ($hex) is not disabled\n";
326 return;
327 }
328
329 print "reenable pxe config for $host in hex $hex\n" if $verbose;
330 return if $opt_n;
331 rename "$pxedir/$hex.disable","$pxedir/$hex" or $error .= "\nRename for $hex failed. $! $@";
332 }
333 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
334 sub docopy {
335
336 my ($srcfile, $pxedir, $desthex, $desthost, $ipadr) = @_;
337 open (SOURCE, "$srcfile");
338 open (DEST, ">$pxedir/$desthex") || die "Can't opendir $pxedir: $!";
339 while (<SOURCE>) {
340 s/\$IP/$ipadr/g;
341 s/\$HOST/$desthost/g;
342 print DEST $_;
343 }
344 print DEST "\n# template generated by fai-chboot for host $desthost with IP $ipadr from source $srcfile\n";
345 close SOURCE;
346 close DEST;
347 }
348 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
349 sub tcopy {
350
351 my ($srchost,$desthost) = @_;
352 my ($ipadr,$srcfile,$srchex,$desthex);
353
354 if (gethost($srchost)) {
355 ($ipadr,$srchex) = host2hex($srchost);
356
357 if (-e "$pxedir/$srchex") {
358 $srcfile = "$srchex";
359 } elsif (-e "$pxedir/$srchex.disable") {
360 $srcfile = "$srchex.disable";
361 } elsif (-e "$pxedir/$srchost" ) {
362 $srcfile = "$srchost";
363 } elsif (-e "$pxedir/$srchost.tmpl" ) {
364 $srcfile = "$srchost.tmpl";
365 } else {
366 warn "Source file for $srchost ($srchex) not available\n";
367 return;
368 }
369 } elsif ( -e "$pxedir/$srchost") {
370 $srcfile = "$srchost";
371 } elsif ( -e "$pxedir/$srchost.tmpl") {
372 $srcfile = "$srchost.tmpl";
373 } else {
374 warn "Source file for $srchost not available\n";
375 return;
376 }
377 if ($desthost =~ /\.tmpl$/) {
378 if (-e "$pxedir/$desthost") {
379 warn "Template $desthost already exist. Copying aborted.\n";
380 return;
381 }
382 print "copy pxe config from $srchost to template $desthost\n" if $verbose;
383 docopy("$pxedir/$srcfile",$pxedir,$desthost,$desthost,$ipadr);
384 } else {
385 ($ipadr,$desthex) = host2hex($desthost);
386 if (-f "$pxedir/$desthex.disable") {
387 unlink "$pxedir/$desthex.disable";
388 print "removed old $pxedir/$desthex.disable\n";
389 }
390 print "copy pxe config from $srchost to $desthost, filenames: $srcfile -> $desthex\n" if $verbose;
391 docopy("$pxedir/$srcfile",$pxedir,$desthex,$desthost,$ipadr);
392 }
393 }
394 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
395 getopts('gBc:C:D:eEdhnvlLiIpf:Fk:Ss:tu:U:oPqa:') || usage;
396 $opt_h and usage;
397
398 $opt_n and $opt_v = 1;
399 $opt_v and $verbose = 1;
400 $cfdir = $opt_C || $ENV{'FAI_ETC_DIR'} || '/etc/fai';
401 -d $cfdir || die "ERROR: $cfdir does not exists.";
402 $opt_L and $opt_l = 1;
403 $opt_l or ( @ARGV ) or usage;
404
405 ($opt_B and $opt_F) && die "ERROR: use only one option out of -B and -F\n";
406 ($opt_S and $opt_I) && die "ERROR: use only one option out of -I and -S\n";
407 ($opt_S and $opt_a) && die "ERROR: use only one option out of -S and -a\n";
408 ($opt_I and $opt_a) && die "ERROR: use only one option out of -I and -a\n";
409
410
411 #TODO: also -e, -r and -c can't be used together
412
413 if ($opt_U && $opt_U !~ m#/$#) {
414 die "URL $opt_U must end with a slash\n";
415 }
416
417 # read the nfsroot variable; a little bit ugly, but it works
418 $nfsroot = `. $cfdir/nfsroot.conf 2>/dev/null; echo \$NFSROOT`;
419 chomp $nfsroot;
420 $nfsroot = '/srv/fai/nfsroot' unless $nfsroot;
421
422 $nfsroot_server = `. $cfdir/nfsroot.conf 2>/dev/null; echo \$NFSROOT_SERVER`;
423 chomp $nfsroot_server;
424 $nfsroot_server = 0 unless $nfsroot_server;
425
426 $tftproot = `. $cfdir/nfsroot.conf 2>/dev/null; echo \$TFTPROOT`;
427 chomp $tftproot;
428 $tftproot = '/srv/tftp/fai' unless $tftproot;
429
430 $pxedir = $opt_D || "$tftproot/pxelinux.cfg";
431
432 (-d $pxedir) || die "PXE directory $pxedir does not exist.";
433
434 $opt_l and lsdir(@ARGV);
435 $opt_q and lsdir(@ARGV);
436
437 if ($opt_d) {
438 die "Missing host name(s). Can't disable network booting.\n" unless @ARGV;
439 foreach (@ARGV) {
440 disable($_);
441 }
442 $error and die "$0: $error\n";
443 exit 0;
444 }
445
446 if ($opt_c) {
447 die "Missing destination host name(s). Can't copy.\n" unless @ARGV;
448 # copy a template config to multiple hosts
449 foreach (@ARGV) {
450 tcopy($opt_c,$_);
451 }
452 $error and die "$0: $error\n";
453 exit 0;
454 }
455
456 if ($opt_e) {
457 die "Missing host name(s). Can't reenable network booting.\n" unless @ARGV;
458 foreach (@ARGV) {
459 enable($_);
460 }
461 $error and die "$0: $error\n";
462 exit 0;
463 }
464
465 if ($opt_S) {
466 $opt_i = 1;
467 $action="FAI_ACTION=sysinfo";
468 }
469 if ($opt_I) {
470 $opt_i = 1;
471 $action="FAI_ACTION=install";
472 }
473 if ($opt_a) {
474 $opt_i = 1;
475 $action="FAI_ACTION=$opt_a";
476 }
477
478 if ($opt_i || $opt_s) {
479 $kernelsuffix = (glob "$nfsroot/boot/vmlinuz*${opt_s}*")[-1];
480 $kernelsuffix //= (glob "$tftproot/vmlinuz*${opt_s}*")[-1];
481 $kernelsuffix=~ s/.+vmlinuz-//;
482 die "No kernel found matching $tftproot/vmlinuz*$opt_s\n" unless $kernelsuffix;
483 }
484
485 if ($opt_i) {
486
487 my $bopt;
488 -d "$nfsroot/lib/modules/$kernelsuffix/kernel/fs/overlayfs" and $bopt="rd.live.overlay.overlayfs";
489
490 # create config so host will boot the install kernel
491 $rootfs_pre = ($nfsroot_server) ? "nfs:$nfsroot_server:" : "";
492 $kernelname = "linux ${opt_U}vmlinuz-$kernelsuffix";
493 $initrd = "initrd=${opt_U}initrd.img-$kernelsuffix";
494 $rootfs = "root=${rootfs_pre}$nfsroot $bopt";
495 $bootprot = "ip=dhcp ";
496
497 } elsif ($opt_o) {
498 $kernelname = 'localboot -1';
499 $rootfs = '';
500 $bootprot = '';
501 $flags = '';
502 } else {
503 $kernelname = shift;
504 $kernelname = "linux $kernelname";
505 # $rootfs = shift or die "No rootfs specified.\n";
506 }
507
508 $opt_F and $opt_f="verbose,sshd,createvt";
509 $opt_B and $opt_f="verbose,sshd,reboot";
510 $opt_E and $opt_f="$opt_f,initial";
511 $opt_f and $flags="FAI_FLAGS=$opt_f";
512 $opt_u and $flags.=" FAI_CONFIG_SRC=$opt_u"; # set -u as FAI_CONFIG_SRC
513
514 die "No host specified.\n" unless @ARGV;
515 warn "Booting $kernelname\n" if $verbose;
516 warn " append $initrd $bootprot $opt_k\n" if ( $verbose and ("$initrd $bootprot $opt_k" !~ /^\s+$/));
517 warn " $flags\n\n" if ($verbose and $flags);
518 $opt_k && print "Kernel parameters: $opt_k\n";
519
520 foreach (@ARGV) { mkpxecfg($_,$kernelname,$rootfs,$initrd); }