Changeset 41
- Timestamp:
- 04/15/08 17:49:15 (4 years ago)
- Location:
- devel
- Files:
-
- 2 edited
-
dploy-dhcp/bin/dploy-add2dhcp (modified) (1 diff)
-
dploy-pxe/bin/dploy-add2pxe (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
devel/dploy-dhcp/bin/dploy-add2dhcp
r36 r41 3 3 # $Id$ 4 4 # 5 # (c) Bruno Cornec, HP, provided under the GPL v2 5 # (c) Bruno Cornec, HP 6 # (c) Gallig Renaud, HP 6 7 # 8 # Provided under the GPL v2 7 9 8 10 use strict; 9 11 use File::Copy; 12 use Net::Netmask; 13 use File::Basename; 14 use Pod::Usage; 15 use Getopt::Long qw(:config auto_abbrev no_ignore_case); 16 use Data::Dumper; 10 17 11 my $dhcp="PBCONF/dhcpd.conf"; 12 # 13 # /etc/dhcpd.conf should include that line: 14 # include "/etc/dhcpd.d/dploy.org.conf"; 15 # as part of the appropriate range 16 # does not support multiple ranges yet 17 # This is done at install time by the install script 18 # Also a dploy account should exist to run that script 19 # and be referenced in sudo. Also done by install script 20 # 18 my $logfile = "/var/log/dploy.err"; 19 my $dhcpmain = "/etc/dhcpd.conf"; 20 my $dhcpsub = ""; 21 my %opts; 22 my $mac = ""; 23 my $ip = ""; 24 my $gw = ""; 25 my $dns = ""; 26 my $pxe = ""; 27 my $ntp = ""; 28 my $netmask = ""; 29 my $iprange = ""; 30 my $cidr = ""; 21 31 22 my $mac=$ARGV[0] || ""; 32 GetOptions("help|?|h" => \$opts{'h'}, 33 "mac|m=s" => \$opts{'mac'}, 34 "ip|i=s" => \$opts{'ip'}, 35 "netmask|n=s" => \$opts{'mask'}, 36 "gw|g=s" => \$opts{'gw'}, 37 "dns|d=s" => \$opts{'dns'}, 38 "pxe|p=s" => \$opts{'pxe'}, 39 "time|t=s" => \$opts{'ntp'}, 40 ) || pb_syntax(-1,0); 23 41 24 if ($mac !~ /^(?:[[:xdigit:]]{1,2}[-:]){5}[[:xdigit:]]{1,2}$/) { 42 # Mac address check 43 if (defined $opts{'mac'}) { 44 $mac = $opts{'mac'}; 45 if ($mac !~ /^(?:[[:xdigit:]]{1,2}[-:]){5}[[:xdigit:]]{1,2}$/) { 25 46 print "Syntax: dploy-add2dhcp MAC ip\n"; 26 47 exit(-1); 48 } 49 } 50 # only lowercase for homogeneity in mac address 51 $mac =~ tr/A-Z/a-z/; 52 53 # newmac replaces : separator with - and only lowercase for dhcpd 54 my $newmac = $mac; 55 $newmac =~ s/:/-/g; 56 57 # IP address check 58 if (defined $opts{'ip'}) { 59 $ip = $opts{'ip'}; 60 if ($ip !~ /^([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])$/) { 61 print "Syntax: dploy-add2dhcp mac IP\n"; 62 exit(-1); 63 } 27 64 } 28 65 29 # only lowercase for homogeneity 30 $mac =~ tr/A-Z/a-z/; 31 32 my $ip=$ARGV[1] || ""; 33 34 if ($ip !~ /^([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])$/) { 35 print "Syntax: dploy-add2dhcp mac IP\n"; 36 exit(-1); 66 # Other Parameters 67 if (defined $opts{'h'}) { 68 pb_syntax(0,1); 37 69 } 38 # 39 # Reads existing DHCP conf if any 40 # 41 42 my $foundit = 0; 43 44 open(DHCPNEW,"> $dhcp.new") || die "Unable to create $dhcp.new"; 45 if (open(DHCP,$dhcp)) { 46 while (<DHCP>) { 47 if (/host host-$mac \{/ .. /}\n/) { 48 # We need to replace the old content with the new one 49 print "Skip $_"; 50 $foundit = 1; 51 } else { 52 print "Keep $_"; 53 # Just write the existing content 54 print DHCPNEW $_; 55 } 56 } 57 close(DHCP); 58 } else { 59 $foundit = 1; 70 if (defined $opts{'mask'}) { 71 $netmask = $opts{'mask'}; 72 } 73 if (defined $opts{'gw'}) { 74 $gw = $opts{'gw'}; 75 } 76 if (defined $opts{'pxe'}) { 77 $pxe = $opts{'pxe'}; 60 78 } 61 79 62 if ($foundit == 1) { 63 # Writes new DHCP conf from scratch 64 # 65 print DHCPNEW "\n"; 66 print DHCPNEW "host host-$mac {\n"; 67 print DHCPNEW " hardware ethernet $mac;\n"; 68 print DHCPNEW " fixed-address $ip;\n"; 69 print DHCPNEW "}\n\n"; 80 # Calculate IP subnet 81 my $block = new Net::Netmask ($ip, $netmask); 82 $iprange = $block->base(); 83 $cidr = $block->bits(); 84 85 # Check if a DHCP conf file already exist for the subnet 86 $dhcpsub="/etc/dploy/dhcp-$iprange-$cidr.conf"; 87 if (! -d "/etc/dploy" ) { 88 mkdir ("/etc/dploy",0755); 70 89 } 71 close(DHCPNEW);72 move("$dhcp.new",$dhcp);73 90 74 # 91 if (! -e "$dhcpsub" ) { 92 # Create a DHCP file for the IP subnet 93 open(DHCPSUBNEW,"> $dhcpsub.new") || die "Unable to create $dhcpsub.new: $!"; 94 print DHCPSUBNEW "subnet $iprange netmask $netmask {\n"; 95 print DHCPSUBNEW " option routers $gw;\n"; 96 if (defined $opts{'dns'}) { 97 $dns = $opts{'dns'}; 98 print DHCPSUBNEW " option domain-name-servers $dns;\n"; 99 } 100 if (defined $opts{'ntp'}) { 101 $ntp = $opts{'ntp'}; 102 print DHCPSUBNEW " option ntp-servers $ntp;\n"; 103 } 104 print DHCPSUBNEW " next-server $pxe;\n"; 105 print DHCPSUBNEW " authoritative;\n"; 106 print DHCPSUBNEW " if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00000\" {\n"; 107 print DHCPSUBNEW " filename \"pxelinux.bin\";\n"; 108 print DHCPSUBNEW " } else if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00002\" {\n"; 109 print DHCPSUBNEW " filename \"elilo-ia64.efi\";\n"; 110 print DHCPSUBNEW " }\n"; 111 print DHCPSUBNEW "}\n"; 112 close DHCPSUBNEW; 113 move("$dhcpsub.new",$dhcpsub); 114 } 115 116 # Update the main DHCP configuration file 117 my $foundit = 0; 118 open(DHCPMAIN, "$dhcpmain") || die "Unable to open $dhcpmain: $!"; 119 while (<DHCPMAIN>) { 120 if (/include\s+\"$dhcpsub\";/) { 121 $foundit=1; 122 } 123 } 124 close(DHCPMAIN); 125 126 if ($foundit == 0) { 127 open(DHCPMAIN, ">>$dhcpmain") || die "Unable to open $dhcpmain: $!"; 128 print DHCPMAIN "include \"$dhcpsub\";\n"; 129 close(DHCPMAIN); 130 } 131 132 133 # Populate the subnet DHCP file with the new server entry 134 # Replace server entry if already existing 135 $foundit = 0; 136 # let's try to find the right position in the file 137 my $bracket = 0; 138 my $bracketfound = 0; 139 my $lastline = ""; 140 141 open(DHCPSUBNEW,"> $dhcpsub.new") || die "Unable to create $dhcpsub.new: $!"; 142 open(DHCPSUB, "$dhcpsub") || die "Why can't I open a file I just created: $!"; 143 while (<DHCPSUB>) { 144 if (/{/) { 145 $bracket++; 146 $bracketfound=1; 147 } 148 if (/}/) { 149 $bracket--; 150 } 151 if (/host host-$newmac {/ .. /}\n/) { 152 # We need to replace the old content with the new one 153 print "Skip $_"; 154 $foundit = 1; 155 } else { 156 print "Keep $_"; 157 # Just write the existing content 158 print DHCPSUBNEW $_ unless (($_ =~ /}/) && ($bracket == 0) && ($bracketfound == 1)); 159 } 160 $lastline = $_; 161 } 162 163 print DHCPSUBNEW " host host-$newmac {\n"; 164 print DHCPSUBNEW " hardware ethernet $mac;\n"; 165 print DHCPSUBNEW " fixed-address $ip;\n"; 166 print DHCPSUBNEW " }\n"; 167 print DHCPSUBNEW $lastline; 168 169 close DHCPSUBNEW; 170 close DHCPSUB; 171 move("$dhcpsub.new",$dhcpsub); 172 75 173 # Relaunch DHCP server 76 #77 174 system("sudo /etc/init.d/dhcpd restart"); -
devel/dploy-pxe/bin/dploy-add2pxe
r29 r41 3 3 use strict; 4 4 use File::Copy; 5 use File::Basename; 5 6 6 my $dhcp="/etc/dhcpd.d/dploy.org.conf"; 7 # 8 # /etc/dhcpd.conf should include that line: 9 # include "/etc/dhcpd.d/dploy.org.conf"; 10 # as part of the appropriate range 11 # does not support multiple ranges yet 12 # 7 # Root of the PXE setup 13 8 my $pxe="/tftpboot/pxelinux.cfg"; 14 # Where to find the LinuxCOE created ISO 9 # Where to find the LinuxCOE created ISO on the SystemDesigner server 15 10 my $linuxcoe="/var/ftp/html/scratch_monkey/systemdesigner"; 11 # Where to find the Mondorescue created ISO on the Images server 12 my $mondorescue="/var/ftp/html/mondorescue/iso"; 16 13 # temporary mountpoint 17 my $mnt="/tmp/mnt- linuxcoe";14 my $mnt="/tmp/mnt-dploy"; 18 15 19 my $mac=$ARGV[0] ;16 my $mac=$ARGV[0] || ""; 20 17 21 18 if ($mac !~ /^(?:[[:xdigit:]]{1,2}[-:]){5}[[:xdigit:]]{1,2}$/) { 22 print "Syntax: add2pxe MAC ip url-to-LinuxCOE-iso\n";23 exit(-1);19 print "Syntax: dploy-add2pxe MAC url-to-iso\n"; 20 exit(-1); 24 21 } 25 22 26 my $ip=$ARGV[1]; 27 28 if ($ip !~ /^([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])$/) { 29 print "Syntax: add2pxe mac IP url-to-LinuxCOE-iso\n"; 30 exit(-1); 31 } 32 33 my $url=$ARGV[2]; 34 35 if ($url !~ /(\w+):\/\/([^\/:]+)(:\d+)?\/(.*)/) { 36 print "Syntax: add2pxe mac ip URL-TO-LinuxCOE-ISO\n"; 37 exit(-1); 38 } 39 40 # Reads existing DHCP conf if any 41 # 23 # only lowercase for homogeneity 24 $mac =~ tr/A-Z/a-z/; 42 25 43 26 # newmac replaces : separator with - and only lowercase for pxelinux … … 46 29 $newmac =~ s/:/-/g; 47 30 48 my $ foundit = 0;31 my $url=$ARGV[1]; 49 32 50 open(DHCPNEW,"> $dhcp.new") || die "Unable to create $dhcp.new"; 51 if (open(DHCP,$dhcp)) { 52 while (<DHCP>) { 53 if (/host host-$newmac \{/ .. /}\n/) { 54 # We need to replace the old content with the new one 55 print "Skip $_"; 56 $foundit = 1; 57 } else { 58 print "Keep $_"; 59 # Just write the existing content 60 print DHCPNEW $_; 61 } 62 } 63 close(DHCP); 64 } else { 65 $foundit = 1; 33 if ($url !~ /(\w+):\/\/([^\/:]+)(:\d+)?\/(.*)/) { 34 print "Syntax: dploy-add2pxe mac URL-TO-ISO\n"; 35 exit(-1); 66 36 } 67 37 68 if ($foundit == 1) { 69 # Writes new DHCP conf from scratch 70 # 71 print DHCPNEW "\n"; 72 print DHCPNEW "host host-$newmac {\n"; 73 print DHCPNEW " hardware ethernet $mac;\n"; 74 print DHCPNEW " fixed-address $ip;\n"; 75 print DHCPNEW "}\n\n"; 76 } 77 close(DHCPNEW); 78 move("$dhcp.new",$dhcp); 79 80 # Relaunch DHCP server 81 system("sudo /etc/init.d/dhcpd restart"); 82 83 # Get the ID given by LinuxCOE to prepare kernel/initrd/ks 38 # Get the ID given by LinuxCOE/MondoRescue to prepare kernel/initrd/ks 84 39 my $iso=basename($url); 85 40 my $id=$iso; … … 114 69 copy("$mnt/isolinux/initrd.img","$pxe/../linuxcoe/$i") or die "Copy failed: $!"; 115 70 system("sudo /bin/umount $mnt"); 116 117 # Based on Version 2.4 27-Sep-1996 Charles Bailey bailey@genetics.upenn.edu118 # in Basename.pm119 120 sub basename {121 122 my($fullname) = shift;123 my($dirpath,$basename);124 ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s);125 126 return($basename);127 }128
Note: See TracChangeset
for help on using the changeset viewer.
