Set hostname from TEZI wrapup.sh

I would like to set the hostname via the wrapup.sh script after successful image installation. It’s generated from the MAC address and I have the script to generate the full hostname, but I’m not sure how to write it into /etc/hostname

Hi, I’m the Toradex AI Assistant.

I might make mistakes. Our support team will step in if needed. If I answered your question, please tick the “Solution” checkbox. For follow-up questions to me, please use @ToradexAI in your post.

Did you like my answer? :+1: :-1:


Hello! You can absolutely set the hostname using the wrapup.sh script after a successful installation with the Toradex Easy Installer.

After your product ID validation in wrapup.sh, simply append your hostname generation logic and then write the hostname directly into /etc/hostname. For example:

# Example: Generate hostname from MAC address (replace with your actual logic)
MAC=$(cat /sys/class/net/eth0/address | sed 's/://g')
HOSTNAME="mydevice-$MAC"

# Write the generated hostname to /etc/hostname
echo "$HOSTNAME" > $ROOTFS_DIR/etc/hostname

# Optionally, set the hostname immediately for the installation environment
echo "$HOSTNAME" > /proc/sys/kernel/hostname

Please note:

  • $ROOTFS_DIR points to the directory representing the target root filesystem (as used in wrapup.sh scripts).
  • If your script is more complex, just ensure the output is a single line (the desired hostname).
  • You do not need to reboot for this change to persist—the hostname will be set on the next system boot automatically.

Keep in mind: If you are using a read-only root filesystem, persistent modifications at runtime aren’t possible (details).

You can find additional context here:

Best regards, Toradex AI Assistant.

@ToradexAI unfortunately the variable $ROOTFS_DIR is empty inside the wrapup.sh script.

Thanks for your response.

Please remember to tick the “Solution” checkbox below if my response solves your question. If you have follow-up questions, just continue to tag me using @ToradexAI in your new posts.

Did you like my answer? :+1: :-1:


Thank you for the clarification!

You’re correct: within the wrapup.sh script, the $ROOTFS_DIR variable isn’t available. Since wrapup.sh runs after the image installation is completed, you need to modify the target system by mounting its root filesystem directly.

Here’s an example that:

  • Mounts the installed rootfs partition (usually /dev/mmcblk0p2 for eMMC)
  • Writes the generated hostname to /etc/hostname in the installed system
  • Unmounts the partition afterward
#!/bin/sh

# Example: Adapt to your validation logic if necessary
# case $PRODUCT_ID in
#     0027|0028|0029|0035|0082|0083|0084|0085) ;;
#     0014|0015|0016|0017|0077|0078|0079|0080) ;;
#     *) error_exit "This script is meant to be run on a supported module. Aborting...";;
# esac

# Generate hostname from MAC address as desired
MAC=$(cat /sys/class/net/eth0/address | sed 's/://g')
HOSTNAME="mydevice-$MAC"

# Mount the rootfs partition (usually the 2nd partition)
mkdir -p /tmp/target_rootfs
mount /dev/mmcblk0p2 /tmp/target_rootfs

# Set /etc/hostname in the installed system
echo "$HOSTNAME" > /tmp/target_rootfs/etc/hostname

# Unmount and cleanup
umount /tmp/target_rootfs
rmdir /tmp/target_rootfs

# Optionally, reboot
# reboot -f
exit 0

Key points:

  • Always check that /dev/mmcblk0p2 is the correct partition for your device and storage.
  • Adjust the MAC address interface (eth0) if needed.
  • The partition scheme might differ if you use SD cards or NAND.

For more information, see:

Best regards, Toradex AI Assistant.

@ToradexAI I mounted my root partition /dev/mmcblk0p1 to /var/volatile/_temp.
I found the hostname file - in my case it’s /var/volatile/_temp/ostree/deploy/torizon/deploy/1831fbeea5efb994cd9c532168cb6abb80997e2789b11a710d12f7075b4b00b6.0/etc/hostname

unfortunately there is a random directory name involved. In my case that’s 1831fbeea5efb994cd9c532168cb6abb80997e2789b11a710d12f7075b4b00b6.0. How can I know the name of this random directory?

Thanks for your response.

Please remember to tick the “Solution” checkbox below if my response solves your question. If you have follow-up questions, just continue to tag me using @ToradexAI in your new posts.

Did you like my answer? :+1: :-1:


Great question! That "random" directory name is actually the OSTree commit hash, representing the specific deployment of your Torizon OS image. Each new deployment gets a unique, cryptographically generated hash (OSTree details).

To handle this in your wrapup.sh script, you can dynamically find the correct deployment directory with a wildcard/globbing pattern. Here’s an example:

#!/bin/sh

Mount the rootfs partition

mkdir -p /var/volatile/_temp
mount /dev/mmcblk0p1 /var/volatile/_temp

Generate hostname from MAC address

MAC=$(cat /sys/class/net/eth0/address | sed ‘s/://g’)
HOSTNAME=“mydevice-$MAC”

Find the deployment directory dynamically

DEPLOY_DIR=$(ls -d /var/volatile/_temp/ostree/deploy/torizon/deploy/*.0 | head -1)

if [ -n “$DEPLOY_DIR” ]; then
# Write hostname to the deployment’s /etc/hostname
echo “$HOSTNAME” > “$DEPLOY_DIR/etc/hostname”
echo “Hostname set to: $HOSTNAME”
else
echo “Error: Could not find deployment directory”
fi

Unmount and cleanup

umount /var/volatile/_temp
rmdir /var/volatile/_temp

exit 0

This method will always target the correct deployed rootfs, regardless of the OSTree commit hash, making your script reliable after every installation/update cycle.

Best regards, Toradex AI Assistant.

Hi @tw.apex

Can you elaborate on what is your objective here?

Since the MAC address is already derived from the SN :sweat_smile:
which in turn is part of the hostname

Hi @allan.tx, we have our own MAC address range which we want to use. Also we want to use a specific prefix to the hostname, followed by a random 4-digit value based on the mac address (like this: gcu-ABCD.local). the default hostname does not suit us. It would be nice to have a hook when assigning the MAC address (TEZI is currently always replacing it with a default one) and generating the hostname - especially because we also use avahi services to announce our device on the network and there I need a proper hostname. best, Tilli

@tw.apex

The default hostname set is a one-shot service; you can try a similar approach.

set-hostname.service
[Unit]
ConditionFileNotEmpty=|!/etc/hostname
Wants=network-pre.target
Before=network-pre.target
After=local-fs.target
After=sys-subsystem-net-devices-eth0.device

[Service]
Type=oneshot
ExecStart=/usr/bin/sethostname

[Install]
WantedBy=network.target
sethostname
/usr/bin/hostnamectl set-hostname ${hostname}

I actually did exactly that - I just thought it was overkill and wanted to do it in the wrapup script instead :smiley: but now I’m actually running my service before your set-hostname.service :slight_smile: