Changeset 7 for devel/dploy


Ignore:
Timestamp:
05/10/07 19:22:32 (5 years ago)
Author:
bruno
Message:

Updated version of add2pxe working in very limited conf, but working

File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/dploy/bin/add2pxe

    r6 r7  
    55 
    66my $dhcp="/etc/dhcpd.d/linuxcoe.conf"; 
     7# 
     8# /etc/dhcpd.conf should include that line: 
     9# include "/etc/dhcpd.d/linuxcoe.conf"; 
     10# as part of the appropriate range 
     11# does not support multiple ranges yet 
     12# 
    713my $pxe="/tftpboot/pxelinux.cfg"; 
     14# Where to find the LinuxCOE created ISO 
    815my $linuxcoe="/var/ftp/html/scratch_monkey/systemdesigner"; 
     16# temporary mountpoint 
    917my $mnt="/tmp/mnt-linuxcoe"; 
    10 my $dhcpcnt; 
    1118 
    1219my $mac=$ARGV[0]; 
    1320 
    14 if ($mac !~ /:/) { 
    15     print "Syntax: add2pxe mac ip URL-to-LinuxCOE-ISO\n"; 
     21if ($mac !~ /^(?:[[:xdigit:]]{1,2}[-:]){5}[[:xdigit:]]{1,2}$/) { 
     22    print "Syntax: add2pxe MAC ip url-to-LinuxCOE-iso\n"; 
    1623    exit(-1); 
    1724} 
     
    1926my $ip=$ARGV[1]; 
    2027 
    21 if ($ip !~ /\./) { 
    22     print "Syntax: add2pxe mac ip URL-to-LinuxCOE-ISO\n"; 
     28if ($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"; 
    2330    exit(-1); 
    2431} 
     
    2633my $url=$ARGV[2]; 
    2734 
    28 if ($url !~ /:/) { 
    29     print "Syntax: add2pxe mac ip URL-to-LinuxCOE-ISO\n"; 
     35if ($url !~ /(\w+):\/\/([^\/:]+)(:\d+)?\/(.*)/) { 
     36    print "Syntax: add2pxe mac ip URL-TO-LinuxCOE-ISO\n"; 
    3037    exit(-1); 
    3138} 
     
    3340# Reads existing DHCP conf if any 
    3441# 
    35 if (! open(DHCP,$dhcp)) { 
    36     # Doesn't exist yet => no content 
    37     $dhcpcnt = ""; 
    38 } else { 
    39     # reads dhcp content 
    40     $/ = '\0'; 
    41     $dhcpcnt = <DHCP>; 
    42     $/ = '\n'; 
    43 } 
    44 close(DHCP); 
    4542 
    46 # Writes new DHCP conf 
    47 # 
     43# newmac replaces : separator with - and only lowercase for pxelinux 
    4844my $newmac = $mac; 
    4945$newmac =~ tr/A-Z/a-z/; 
    5046$newmac =~ s/:/-/g; 
    51 open(DHCP, "> $dhcp") || die "Unable to open $dhcp for writing"; 
    52 print DHCP "$dhcpcnt"; 
    53 print DHCP "host host-$newmac {\n"; 
    54 print DHCP "    hardware ethernet $mac;\n"; 
    55 print DHCP "    fixed-address $ip;\n"; 
    56 print DHCP "}\n"; 
    57 close(DHCP); 
     47 
     48my $foundit = 0; 
     49 
     50open(DHCPNEW,"> $dhcp.new") || die "Unable to create $dhcp.new"; 
     51if (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; 
     66} 
     67 
     68if ($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} 
     77close(DHCPNEW); 
     78move("$dhcp.new",$dhcp); 
    5879 
    5980# Relaunch DHCP server 
    6081system("sudo /etc/init.d/dhcpd restart"); 
    6182 
    62 # mount the ISO to get kernel and initrd 
     83# Get the ID given by LinuxCOE to prepare kernel/initrd/ks 
    6384my $iso=basename($url); 
    6485my $id=$iso; 
     
    6889my $ks=$url; 
    6990$ks =~ s/img([0-9]+)\.iso/ks$1.cfg/; 
    70 mkdir "$mnt",0755; 
    71 system("sudo /bin/mount -o loop $linuxcoe/$iso $mnt"); 
    72 copy("$mnt/isolinux/vmlinuz","$pxe/../linuxcoe/$k") or die "Copy failed: $!"; 
    73 copy("$mnt/isolinux/initrd.img","$pxe/../linuxcoe/$i") or die "Copy failed: $!"; 
    74 system("sudo /bin/umount $mnt"); 
    7591 
    7692# Updates PXElinux 
     
    92108close(PXE); 
    93109 
     110# mount the ISO to get kernel and initrd 
     111mkdir "$mnt",0755; 
     112system("sudo /bin/mount -o loop $linuxcoe/$iso $mnt"); 
     113copy("$mnt/isolinux/vmlinuz","$pxe/../linuxcoe/$k") or die "Copy failed: $!"; 
     114copy("$mnt/isolinux/initrd.img","$pxe/../linuxcoe/$i") or die "Copy failed: $!"; 
     115system("sudo /bin/umount $mnt"); 
    94116 
    95117# Based on Version 2.4  27-Sep-1996  Charles Bailey  bailey@genetics.upenn.edu 
Note: See TracChangeset for help on using the changeset viewer.