Hummus Ethernet DIP: static IP via DHCP

From www.chip-community.org
Jump to: navigation, search

Update 14 October 2016: This post is now obsolete as JKW has added this and some other code to his Hummus code. Thanks, JKW!

If you cloned his archive after roughly 20:00 or 21:00 UTC 14 October 2016, the code in this post is already incorporated.

This post is now archival.

For the details, see https://bbs.nextthing.co/t/how-do-you-run-a-script-as-root/10232/24

Hummus DIP: How to obtain a static IP via dhcp

Situation

Hummus DIP's don't have static MAC addresses. Each time networking is restarted, at bootup for example, a new MAC is assigned dynamically to the ethernet interface.

If you are trying to get a static IP address via DHCP, this will not work. DHCP requires a static MAC in order to identify the machine making the request.

Solution

To assign a static IP address to the DIP the following command needs to be run as root:

   # /bin/ip link set dev eth0 address xx:xx:xx:xx:xx:xx

where xx:xx:xx:xx:xx:xx is the MAC address you want to give to your DIP.

After this, dhclient must be re-run:

   # /sbin/dhclient eth0

This can be cumbersome to do each time networking is restarted.

Automating the process

An existing script can be modified to include these two commands. /opt/CHIP_HUMUS/overlay/load.sh is where the driver for the DIP get's loaded. This is run at every boot and reboot.

The above commands can be added the load.sh after the call to sleep. Here is the script before changes were made:

   DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
   sudo rmdir /sys/kernel/config/device-tree/overlays/JKW_HUMUS/ >/dev/null 2>&1;
   sudo mkdir /sys/kernel/config/device-tree/overlays/JKW_HUMUS
   dmesg >/tmp/pre_JKW_HUMUS_load
   su -c 'cat '$DIR'/JKW_humus.dtbo > /sys/kernel/config/device-tree/overlays/JKW_HUMUS/dtbo'
   sleep 3
   dmesg >/tmp/post_JKW_HUMUS_load
   diff /tmp/pre_JKW_HUMUS_load /tmp/post_JKW_HUMUS_load
   rm /tmp/pre_JKW_HUMUS_load /tmp/post_JKW_HUMUS_load

The lines beginning with "dmesg", "diff" and "rm" are used by JKW for debugging output and can safely be removed. Here is what load.sh would look like after removing the debugging info and adding the two lines of code:

   DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
   
   sudo rmdir /sys/kernel/config/device-tree/overlays/JKW_HUMUS/ >/dev/null 2>&1;
   sudo mkdir /sys/kernel/config/device-tree/overlays/JKW_HUMUS
   
   su -c 'cat '$DIR'/JKW_humus.dtbo > /sys/kernel/config/device-tree/overlays/JKW_HUMUS/dtbo'
   
   sleep 3
   
   /bin/ip link set dev eth0 address xx:xx:xx:xx:xx:xx
   /sbin/dhclient eth0

Choosing a MAC address

It is preferable to choose unique, valid values for the MAC address. One of the criteria to be met is avoiding using Organisational Unique Identifiers (OUI, the first 3 xx pairs) that have been assigned. In my experimentation, I've not found any of the randomly assigned MAC addresses to conflict with an already assigned OUI. Therefore, I would suggest using a MAC randomly assigned by the system as your static one. Before trying out the above commands, run

   $ ip link show dev eth0 | grep ether

This will yield output such as

   link/ether 4c:7d:88:f2:27:93 brd ff:ff:ff:ff:ff:ff

Take the value shown for "link/ether" and use that in your load.sh file.