Change balena device name at initialization

Is it possible to change BALENA_DEVICE_NAME_AT_INIT variable?
I believe it is the first name that is set after it connects balena API for the first time. Is this a random name? Do I have a control over it while flashing the BalenaOS image?

Thanks in advance.

Hello, BALENA_DEVICE_NAME_AT_INIT is the name of the device on first initialization, so it’s not supposed to change. If you just want to get the current name, you can use supervisor API to get that: Interacting with the balena Supervisor - Balena Documentation

If you want to change the device name, you can use one of our SDKs, like this in Python: Balena Python SDK - Balena Documentation

What is your use case? Perhaps we can suggest some other options.

@alanb128 This is weird. We always use BALENA_DEVICE_NAME_AT_INIT to get the latest device name as configured through the dashboard. But I see that it’s not consistent on my latest devices, some services have the updated name, some the original one.

I read in your docs that it takes this from when the container is created. Was this always the case?

Could it be that previously the containers were created from scratch on every boot but not anymore now? I’m 99% sure that normally changes inside the container did not persist over a service restart, but now they do.

Hello @ada as far as I know, BALENA_DEVICE_NAME_AT_INIT always was the name at first initialization.
Changes inside a container will persist device reboots. In that case the container is stopped and started, but not removed and recreated. (same as hitting the stop and then start buttons on the dashboard) A container is removed and recreated when you restart the service, such as from the dashboard button or the Supervisor API, or by pushing a new release.

@alanb128 thanks for confirming. Most likely this effect was not clear to us because of regular updates to most services.

@berkayg So not sure if this helps, but our procedure is the following:

  • flash the device (no preloading)
  • once it’s online, change the device name on the dashboard. This will be used for BALENA_DEVICE_NAME_AT_INIT later
  • trigger an update to the latest release, which installs all services the first time

But I guess the proper way would be to use $ curl "$BALENA_SUPERVISOR_ADDRESS/v2/device/name?apikey=$BALENA_SUPERVISOR_API_KEY", epsecially for preloaded images.

1 Like