Creating Custom hostname default for fog.man.reg


  • Moderator

    Work in progress. This code has not been fully tested yet.

    Some FOG Admins have requested the ability to automatically calculate the target host name during both manual and auto registration. FOG (at the time) had a patch (hack) to supported this function in the 0.3.x days. That patch never made it into the mainstream code and was lost over the years. This tutorial will show you how to add a similar function to the current release of FOG (v1.5.8 at the time of this tutorial).

    This code depends on functions withing the FOG environment today to allow this patch to function. First a little background. The developers already added the ability to “hook” external programs into the imaging process. The “hook” we are going to use is called postinitialization scripts. The intent of these postinit scripts are to allow the FOG admin to run programs needed to bring up specific hardware before the imaging process starts. We are going to leverage this postinit hook to “patch” or replace files in the FOS Linux OS before imaging (or in this case registration) starts. In this tutorial we are going to replace the FOS Linux version of fog.man.reg and (fog.auto.reg) with our patched version as well as copy over our custom program for created a default host name that can be used during the registration process. I requested, for the 1.6.x series, to have the code added to the official fog.man.reg and (fog.auto.reg) to support this default system naming hook. So hopefully in the future you will not have to change any code in the fog.man.reg files to support the fog.customhostname script. But for now (in the 1.4.x and 1.5.x series) we will have to manually patch the FOS Linux core code.

    In this first step we’ll add the code necessary to support our custom naming script. To start with download the fog.man.reg file from the fogproject github site. (NOTE: If you want to do these edits using a windows computer, use notepad++ for editing and not any windows built in text editors like notepad.exe) https://github.com/FOGProject/fos/blob/master/Buildroot/board/FOG/FOS/rootfs_overlay/bin/fog.man.reg

    fog.man.reg (hacks)
    In the fog.man.reg file replace lines # 157-171 with the below code. I’ve left plenty of the original and untouched code so you can see where the patched code goes.

    keyEnter=""
    blDoAD=""
    res=""
    host_default_name=""
    
    if [[ -f "/bin/fog.customhostname" ]]; then
        . /bin/fog.customhostname
    fi
    
    while [[ $res != "#!ok" ]]; do
        if [[ $host_default_name != "" ]]; then
            read -p " * Enter hostname for this computer [$host_default_name]: " host
        else
            read -p " * Enter hostname for this computer: " host
        fi
        host=${host:-$host_default_name}
        if [[ ${#host} -gt 15 ]]; then
            host=${host:0:15}
            echo " | Truncated to 15 characters: $host"
            usleep 2000000
        fi
        host=$(echo $host | base64)
        res=$(curl -Lks --data "host=$host" ${web}service/hostnameloop.php 2>/dev/null)
        [[ $res != "#!ok" ]] && echo "$res"
    done
    

    The change/inserted lines into the original fog.man.reg code are as follows.

    keyEnter=""
    blDoAD=""
    res=""
    >> host_default_name=""
    
    >> if [[ -f "/bin/fog.customhostname" ]]; then
    >>     . /bin/fog.customhostname
    >> fi
    
    while [[ $res != "#!ok" ]]; do
    >>    if [[ $host_default_name != "" ]]; then
    >>        read -p " * Enter hostname for this computer [$host_default_name]: " host
    >>     else
    >/         read -p " * Enter hostname for this computer: " host
    >>     fi
    >>     host=${host:-$host_default_name}
        if [[ ${#host} -gt 15 ]]; then
            host=${host:0:15}
            echo " | Truncated to 15 characters: $host"
            usleep 2000000
        fi
        host=$(echo $host | base64)
        res=$(curl -Lks --data "host=$host" ${web}service/hostnameloop.php 2>/dev/null)
        [[ $res != "#!ok" ]] && echo "$res"
    done
    

    The heart of change is the following code. In this code we will determine the site the target computer is running at via its IP address. Then determine what type of computer it is (desktop, laptop, tablet) and then finally read the computer’s serial number from the SMBIOS. The composite host name will be the SiteCode(3)+HW(1)+Serial This format works well with Dell based computers. You may need to put limits on the serial number collected from other hardware manufacturers. (NOTE: I’ve added a “z” in front of the variable names to ensure there isn’t any variable name clashes with the original FOG programs.) At the end of the fog.customhostname script the variable host_default_name is used to return the default host name to the fog.man.reg and (fog.auto.reg) scripts. If this variable is blank the main fog code will use its default naming actions.

    fog.customhostname

    #!/bin/bash
    
    zmyip=`ip route get 8.8.8.8 | awk 'NR==1 {print $NF}' | cut -d "." -f1-2`;
    
    case "${zmyip}" in
         10.1)
             zsitecode="NYC";
             ;;
         10.2)
             zsitecode="LA";
             ;;
         *)
             # Default code for the unknowns
             zsitecode="CRP";
             ;;
    esac
    
    zchassis=`dmidecode -s chassis-type`;
    zchassis="${zchassis%"${zchassis##*[![:space:]]}"}";  #Remove training space
    zchassis="${zchassis,,}"; # Convert string to lower
    
    if [ "$zchassis" == "laptop" ]; then
        zchtype="P";
    elif [ "$zchassis" == "tablet" ]; then
        zchtype="T";
    else
        # Everything else is a desktop
        zchtype="D";
    fi
    
    zserialno=`dmidecode -s chassis-serial-number`;
    zserialno="${zserialno%"${zserialno##*[![:space:]]}"}";  # Remove trailing whitespace
    
    # get the serial no from the baseboard if chassis doesn't give it up
    if [ "$zserialno" == "" ]; then
        zserialno=`dmidecode -s baseboard-serial-number`;
        zserialno="${zserialno%"${zserialno##*[![:space:]]}"}";
    fi
    
    LEN=$(echo ${#zserialno});
    if [ "$LEN" -gt 11 ]; then
        # get the right most characters of the serial number (usually the most
        # unique characters). Logic for 11 is Site(3)+HW(1)+Serial(11)=15 characters
        zserialno=${zserialno:(-11)};
    fi
    
    # default host name is returned to the fog.man.reg script
    host_default_name="$zsitecode$zchtype$zserialno";
    

    I feel I need to explain this bit of magic code.

    zmyip=`ip route get 8.8.8.8 | awk 'NR==1 {print $NF}' | cut -d "." -f1-2`;
    

    This code gets the target computer’s IP address. In the switch case below we are looking at the first 2 octets of the IP address [10.1, 10.2]. If in your network you use the first 3 octets of the IP address [10.1.50, 10.1.60, 10.1.70] to define the sites then replace the zmyip line with this code to extract the first 3 octets.

    zmyip=`ip route get 8.8.8.8 | awk 'NR==1 {print $NF}' | cut -d "." -f1-3`;
    

    Postinit Patch script. In this section we need to tell the postinitscripts what we need. In this case we need to copy the fixed fog.man.reg and (fog.auto.reg) file(s) and our fog.customhostname to the FOS Linux engine every time it boot. So create the following file fog.patch.customhostname in the /images/dev/postinitscripts directory on the fog server. In that file add the following lines.

    fog.patch.customhostname

    #!/bin/bash
    echo "";
    echo "Installing CustomeHostName Patch"
    cp -f ${postinitpath}fog.man.reg.fix /bin/fog.man.reg
    cp -f ${postinitpath}fog.customhostname /bin/fog.customhostname
    echo "Done Patching"
    

    Finally add the call to fog.patch.customhostname to the end of the fog.postinit master file.
    fog.postinit

    . ${postinitpath}/fog.patch.customhostname
    

    Now pxe boot the target computer and enter the Full Registration and Inventory menu. When the regristration process runs the system should pause at the set host name. You should see the calculated host name in the square braces at the end of the line. If you press the enter key here (without typing any text) the system will take the default host name in the square braces. If you key in any text for the host name, that value will override the default host name provided by our customhostname script.

    ref: https://forums.fogproject.org/topic/7740/the-magical-mystical-fog-post-download-script/15
    ref: https://forums.fogproject.org/topic/11126/using-fog-postinstall-scripts-for-windows-driver-injection-2017-ed



  • Works like a charm! Thanks george1421!


Log in to reply
 

339
Online

7.1k
Users

14.3k
Topics

135.1k
Posts