Script to replace default config with your config



  • I use the embedded image and find it a pain when reflashing to a new version to have to setup the interfaces, ip address, and spoof the mac address for my ip330 box just to restore my config. What I did was create a small script that runs on freebsd. You give it your config file and the location to the image and it will overwrite the default config. That way you just flash the image, stick the cf card in and your done.

    I attached the file as well. In testing I found that if the code is copied into notepad, etc on windows and saved you have to run dos2unix to make it work on freebsd. To save you the trouble you can just download the attachement rename from .txt to .sh and chmod +x to give it execute permissions.

    
    #!/bin/sh
    # The script replaces the default config file in the
    # image with your specified config file.
    # Usage {configfile} {imagefile}
    # Example ./replace_conf.sh myconfig.xml pfSense.img
    
    NEWCONFIG="$1"
    IMGFILE="$2"
    WORKDIR=`pwd`
    echo "Working Directory: $WORKDIR"
    echo "Image File: $IMGFILE"
    MD=`mdconfig -a -t vnode -f $WORKDIR/$IMGFILE`
    echo "Image File Device: $MD"
    mkdir $WORKDIR/d
    mount /dev/${MD}d $WORKDIR/d
    mv $WORKDIR/$NEWCONFIG $WORKDIR/d/conf/config.xml
    chmod 640 $WORKDIR/d/conf/config.xml
    echo "Repalced Config With $NEWCONFIG"
    umount $WORKDIR/d
    rm -rf $WORKDIR/d
    mdconfig -d -u ${MD}
    
    ```[replace_conf.txt](/public/_imported_attachments_/1/replace_conf.txt)


  • Based on the builder code, to use this with a nanobsd image simply replace /dev/${MD}d with /dev/${MD}s3 in the mount command to reference the proper slice for the configuration.  These scripts will only work for a full image, not an upgrade image.  Note that you must use gunzip to decompress the image before using any of these scripts.

    Modified script for nanobsd (also modified some parts to be less potentially destructive and work with absolute paths):

    
    #!/bin/sh
    # This script replaces the config file in the
    # image with your specified config file.
    # Usage {configfile} {imagefile}
    # Example ./replace_conf.sh myconfig.xml pfSense.img
    
    NEWCONFIG="$1"
    IMGFILE="$2"
    echo "Image file: $IMGFILE"
    MD=`mdconfig -a -t vnode -f $IMGFILE`
    echo "Image file device: $MD"
    mkdir ${IMGFILE}.mnt
    mount /dev/${MD}s3 ${IMGFILE}.mnt
    cp $NEWCONFIG ${IMGFILE}.mnt/conf/config.xml
    chmod 644 ${IMGFILE}.mnt/conf/config.xml
    echo "Replaced config with $NEWCONFIG"
    umount ${IMGFILE}.mnt
    rmdir ${IMGFILE}.mnt
    mdconfig -d -u ${MD}
    
    

    If the new config should be the default when you reset to defaults, the config file should be placed at /conf.default/config.xml in the OS slices rather than /conf/config.xml in the configuration slice of the image:

    
    #!/bin/sh
    # This script replaces the default config file in the
    # image with your specified config file.
    # Usage {configfile} {imagefile}
    # Example ./replace_def_conf.sh myconfig.xml pfSense.img
    
    NEWCONFIG="$1"
    IMGFILE="$2"
    echo "Image file: $IMGFILE"
    MD=`mdconfig -a -t vnode -f $IMGFILE`
    echo "Image file device: $MD"
    mkdir ${IMGFILE}.mnt
    mount /dev/${MD}s1a ${IMGFILE}.mnt
    cp $NEWCONFIG ${IMGFILE}.mnt/conf.default/config.xml
    chmod 644 ${IMGFILE}.mnt/conf.default/config.xml
    umount ${IMGFILE}.mnt
    mount /dev/${MD}s2a ${IMGFILE}.mnt
    cp $NEWCONFIG ${IMGFILE}.mnt/conf.default/config.xml
    chmod 644 ${IMGFILE}.mnt/conf.default/config.xml
    umount ${IMGFILE}.mnt
    echo "Replaced default config with $NEWCONFIG"
    rmdir ${IMGFILE}.mnt
    mdconfig -d -u ${MD}
    
    

    To make an update image from the first OS slice of a full image:

    
    #!/bin/sh
    # This script creates an update image from a full image.
    # Usage {imagefile} {updatefile}
    # Example ./make_update.sh pfSense.img pfSense-update.img
    
    IMGFILE="$1"
    IMGUPDATE="$2"
    echo "Image file: $IMGFILE"
    MD=`mdconfig -a -t vnode -f $IMGFILE`
    echo "Image file device: $MD"
    dd if=/dev/${MD}s1 of=$IMGUPDATE bs=64k
    gzip $IMGUPDATE
    echo "Update file created: ${IMGUPDATE}.gz"
    mdconfig -d -u ${MD}
    
    

    Additional information about some of the steps used in these scripts is on the documentation site at
    http://doc.pfsense.org/index.php/Modifying_Embedded


Locked