Setting hostname through preload

Hi,

I am using ‘balena preload ~/balena.img --app AppName’ to provision new devices. I would like to provide a custom hostname to each img file I create. It appears there isn’t any --hostname variable, I am wondering what would be an alternative approach for this that avoids having to do the change manually?

On a separate note but one that will help my understanding of this issue, does ‘balena preload’ do the same things as ‘balena os configure’? At the moment I am running both because I want to configure the device to connect to my OpenBalena server, and preload the packages.

Hi @maggie0002, first let me point you to the advanced CLI masterclass with regards to preloading as there is a comprehensive guide there to preloading https://github.com/balena-io/balena-cli-advanced-masterclass#5-preloading-and-preregistering. Preloading injects the images onto the device whereas os-configure just does the configuration of the config.json part (so the images would not be on the device and would need to be downloaded when the device boots).

As for the hostname, you can set this in the config.json file to configure the image by setting e.g. “hostname”: “my-host-name”. See here https://github.com/balena-io/debugging-masterclass#7-working-with-the-configjson-file or https://github.com/balena-os/meta-balena#hostname for more.

Thanks for the links. I had come across the config.json configuration, it was the injecting a hostname into the config, ideally though something like --config-hostname myhostname at some stage of the process rather than doing it manually I was hoping to achieve. Or perhaps as there are many and it seems a growing number of variables that config.json supports, a --config-add any-var-you-like would provide more flexibility.

For anyone asking a similar question, here is what I have gone with for now, a .sh script for creating my images:

#!/usr/bin/env bash

####set varilables####

app="appname"

version="2.46.1+rev1"

#####################

#generate key

key="$(hexdump -n 32 -e '"%0x"' /dev/random | head -c 32 && printf '\n')"

#register new device with generated key

balena device register $app --uuid $key

#create config file with key

balena config generate --app $app --version $version --device $key --network ethernet --appUpdatePollInterval 10 --output config.json

#write the hostname to the new config file

configfile=$(cat config.json)

echo {\"hostname\":\"CHOSENHOSTNAMEHERE\",${configfile#"{"} > config.json

#inject the config file

balena os configure ~/filelocation/balena.img --config config.json --config-network=ethernet --device $key

#room here for expansion
#initialise the card by writing to SD card could go here.
 
#clean up by deleting image file and initiate a loop here for multiple images

There is a lot of room here to customise, you could pull the first 5 digits from $key for example and use it as your hostname, or as in one instance, use the first two digits as a unique identifier that goes on the end of your hostname.

I then use this new hostname as my wifi SSID, which is particularly useful if you end up using a unique identifier in your hostname. Insert this into your wifi-connect start.sh, replacing “./wifi-connect” (https://github.com/balena-io/wifi-connect/blob/master/scripts/start.sh):

#gets the hostname of the device
    ssid="$(hostname)"

#starts wifi-connect with that hostname. 
    ./wifi-connect -s $ssid

Thanks as always for all the input on these processes.