Introduction
In this tutorial, we will learn how to flash an image from one HMI device to multiple devices. We can see an overview of copying the image from one device to another using a USB flash drive. We can investigate an example code of how to increment the IP and change the MAC address in all the devices automatically using the script.
Overview
Use any disk imaging tool to extract the image from the master HMI device.
Create an installation script using the extracted image from the master HMI device and store it in a bootable USB flash drive.
Insert the USB flash drive into the destination HMI device and run the script along with the IP increment and MAC address change script.
Requirements
The master device (HMI) with the changes
Host machine PC
A bootable USB flash drive.
Destination device
Step 1: Creating an Image of the Source HMI Device
The first step is to create an image of the source HMI device. This image will be used to flash the destination HMI device. To create the image, follow these steps:
Insert the USB flash drive into the master HMI device.
Connect to the master device using the serial console
Open the terminal of the master device in the PC using any terminal program with a speed of 115200 eg:-MobaXterm
Mount the USB flash drive in the master HMI device. (If automount is not enabled)
# Mount the USB drive or SD card mount /dev/sdb1 /mnt
5. Use the dd command to create an image of the source HMI device on the USB flash drive
dd if=/dev/mmcblk0 of=/mnt/Nallino_image.img bs=1M
In this command, if stands for "input file", and of stands for "output file". The bs option specifies the block size.
Step 2: Add a script to change the default IP address of the Destination HMI Device.
The IP address should not be the same in all the cloned devices so we are about to write a script to change the IP address for all the devices. We have a counter variable called $count and an IP address stored in the variable $CURRENT_IP, you can increment the IP address according to the counter value in a shell script like this:
#!/bin/bash # Switch off the dhcp to obtain static IP sconfig dhcp false # Define the counter variable count=0 # Get the current IP address CURRENT_IP=$192.168.1.1 # Check if the counter file exists if [ -f counter.txt ]; then # Read the current count from the file count=$(cat counter.txt) fi # Increment the counter count=$((count+1)) # Extract the IP address octets IFS='.' read -r -a OCTETS<<< "$CURRENT_IP" # Increment the last octet based on the counter value OCTETS[3]=$(( ${ OCTETS[3]} + $count )) # Check if the last octet exceeds 255 if [ "${ OCTETS[3]}" -gt "255" ]; then # Increment the third octet OCTETS[2]=$(( ${ OCTETS[2]} + 1 )) # Increment the third octet OCTETS[2]=$(( ${ OCTETS[2]} + 1 )) # Reset the last octet to 1 OCTETS[3]=1 fi # Reassemble the IP address NEW_IP="${OCTETS[0]}.${OCTETS[1]}.${OCTETS[2]}.${OCTETS[3]}" # Modify the network configuration file sed -i "s/$CURRENT_IP/$NEW_IP/g" /etc/NetworkManager/system-connections/Wired connection eth0.nmcconnection # Restart the network service systemctl restart NetworkManager.service echo "IP address updated from $CURRENT_IP to $NEW_IP"
When the third octet reaches 255 and needs to roll over to 0, and the case where the second or first octet reaches 255 and needs to increment the next octet and reset themselves and any lower octets to 0.
This script assumes that the HMI device is using a static IP address and that the network configuration file is located at /etc/NetworkManager/system-connections/Wired connection eth0.nmcconnection.
To use this script, save it to a file (e.g. increment_ip_address.sh), make it executable using chmod +x increment_ip_address.sh, and then run it as root or with sudo privileges.
Step 3: Add script to replace hardware mac Address with the cloned IP address of the Destination HMI Device
The MAC address should be unique for all the cloned devices so we need to replace the hardware MAC address with the cloned MAC address of the previous device as the MAC address should be unique. We can make an example script to change the cloned MAC address
#!/bin/bash # Get the current MAC address of the interface hw_mac=$(ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}') # Set the cloned MAC address (replace with your own cloned MAC address) cloned_mac="00:11:22:33:44:55" # Use nmcli to modify the MAC address to the hardware MAC address nmcli con mod "Wired connection eth0" ethernet.mac-address $hw_mac echo "MAC address successfully reset to hardware MAC address"
This script retrieves the current hardware MAC address of the eth0 interface, stores it in the hw_mac variable, and sets the cloned_mac variable to the cloned MAC address that needs to be replaced. Then, the script uses nmcli to modify the MAC address of the connection named "Wired connection eth0" to the hardware MAC address stored in hw_mac. Finally, the script prints a message to indicate that the MAC address has been successfully reset to the hardware MAC address.
To use this script, save it as a file (e.g. change_mac.sh), make it executable (chmod +x change_mac.sh), and run it with root privileges (sudo ./change_mac.sh).
Step 4: Add script to replace the Image and run the script on the Destination HMI Device
Create the installation script which mounts the USB flash drive, changes the directory, flashes the cloned image, changes the IP address and MAC address.
#!/bin/bash # Mount the USB drive or SD card mount /dev/sdb1 /mnt # Change to the directory containing the image file cd /mnt # Flash the image to the HMI device dd if=Nallino_image.img of=/dev/mmcblk0 bs=1M # Change the IP address ./increment_ip_address.sh # Change the MAC address ./change_mac.sh # Unmount the USB drive or SD card umount /mnt
To use this script, save it to a file (e.g. install_image.sh), make it executable using chmod +x install_image.sh, and then run it as root or with sudo privileges.
The next step is to transfer the image from the USB flash drive to the destination HMI device. To do this, follow these steps:
Transfer all the scripts and files to the root directory in the USB flash drive.
Remove the USB flash drive from the master HMI device.
Insert the USB flash drive into the destination HMI device
Connect to the destination device using the serial console.
Open the terminal of the destination device in the PC using any terminal program with a speed 115200 eg:-MobaXterm
Run the installation script in the destination HMI device(install_image.sh).
Step 5: Verifying the Image on the Destination HMI Device
The final step is to verify that the image was successfully transferred to the destination HMI device. To do this, follow these steps:
Remove the USB flash drive from the Destination HMI device.
Reboot the destination HMI device.
Verify that the image is running correctly on the destination HMI device by looking in to the Network config file by using the following command.
root@localhost:/# cat /etc/NetworkManager/system-connections/Wired\ connection\ eth0.nmconnection [connection] id=Wired connection eth0 uuid=84f37846-e9ee-45aa-86e5-09876a1020b5 type=ethernet autoconnect-priority=-100 interface-name=eth0 permissions= [ethernet] cloned-mac-address=00:07:8E:32:7E:8A [ipv4] method=manual address1=192.168.1.143/24,192.168.1.10
The default IP address should be changed by using the above script.
Conclusion
In this tutorial, we discussed how to flash an image from one HMI device to another in general and looked into the example using the dd command. This method can be useful for deploying the same image across multiple HMI devices and changing the IP address and MAC address. We can install the customer setup on multiple devices by repeating the steps on multiple devices one by one.
We can get more information about the dd command in the following link
We can use other imaging tools such as Clonezilla or Mondo Rescue tool to create images of the HMI devices.