1 |
dpavlin |
129 |
package pxelinux; |
2 |
|
|
|
3 |
dpavlin |
138 |
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
use autodie; |
6 |
|
|
|
7 |
dpavlin |
129 |
our $magic = 0xF100747E; |
8 |
|
|
|
9 |
|
|
our $config_file; #= 'pxelinux.cfg/default'; |
10 |
|
|
our $path_prefix; |
11 |
|
|
our $reboot_time; # 300 s |
12 |
|
|
|
13 |
dpavlin |
138 |
use server; |
14 |
|
|
use File::Slurp; |
15 |
|
|
use Carp qw/confess/; |
16 |
|
|
|
17 |
|
|
sub config_for_ip { |
18 |
|
|
my ($ip,$config) = @_; |
19 |
|
|
confess "$ip not IP" unless $ip =~ m{^\d+\.\d+\.\d+\.\d+$}; |
20 |
|
|
|
21 |
|
|
$path_prefix = (caller(1))[3]; |
22 |
dpavlin |
463 |
$path_prefix = (caller(2))[3] unless $path_prefix =~ m{config::}; |
23 |
|
|
$path_prefix =~ s{config::}{} || warn "# caller isn't package config !"; |
24 |
dpavlin |
138 |
$path_prefix .= '/'; |
25 |
|
|
|
26 |
|
|
warn "# $ip $path_prefix"; |
27 |
|
|
|
28 |
|
|
my $dir = "$server::base_dir/tftp"; |
29 |
|
|
$dhcpd::file = 'pxelinux.0'; |
30 |
|
|
symlink "/usr/lib/syslinux/pxelinux.0", "$dir/$dhcpd::file" unless -e "$dir/$dhcpd::file"; |
31 |
|
|
|
32 |
|
|
$dir .= "/$path_prefix"; |
33 |
|
|
mkdir $dir unless -d $dir; |
34 |
|
|
|
35 |
|
|
mkdir "$dir/pxelinux.cfg" unless -e "$dir/pxelinux.cfg"; |
36 |
|
|
|
37 |
|
|
$config_file = 'pxelinux.cfg/' . uc sprintf "%02x%02x%02x%02x", split(/\./, $ip, 4); |
38 |
|
|
warn "$ip config_file $config_file"; |
39 |
|
|
|
40 |
|
|
my $path = "$dir/$config_file"; |
41 |
|
|
write_file $path, $config; |
42 |
|
|
|
43 |
|
|
warn "# config: $path ", -s $path; |
44 |
|
|
|
45 |
dpavlin |
153 |
return $config; |
46 |
dpavlin |
138 |
} |
47 |
|
|
|
48 |
dpavlin |
463 |
sub config_ip_boot { |
49 |
|
|
my ( $ip, $boot, $append ) = @_; |
50 |
|
|
|
51 |
|
|
my $default; |
52 |
|
|
my $config; |
53 |
|
|
|
54 |
|
|
foreach my $kernel ( glob "$boot/vmlinuz*" ) { |
55 |
|
|
my $ver = $1 if $kernel =~ m{vmlinuz(.+)}; |
56 |
|
|
$default ||= $ver; |
57 |
|
|
|
58 |
|
|
$config .= qq{ |
59 |
|
|
|
60 |
|
|
label boot$ver |
61 |
|
|
kernel $ip/boot/vmlinuz$ver |
62 |
|
|
append initrd=$ip/boot/initrd.img$ver $append |
63 |
|
|
|
64 |
|
|
}; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
config_for_ip( $ip, qq{ |
68 |
|
|
|
69 |
|
|
default boot$default |
70 |
|
|
prompt 5 |
71 |
|
|
|
72 |
|
|
$config |
73 |
|
|
|
74 |
|
|
}); |
75 |
|
|
|
76 |
|
|
} |
77 |
|
|
|
78 |
dpavlin |
129 |
1; |