Page 1 of 1

How to run a bash script on the very first boot

Posted: Thu Mar 15, 2018 10:56 am
by bilalachahbar
Hello

I created a basic image with some configuration i need for setting up a kubernetes cluster .
But when i burn this image the machine-id from /etc/machine-id stays the same on every node of my raspberry pi cluster.
I have to delete this and create a new one with these commands

Code: Select all

sudo rm /etc/machine-id
sudo rm /var/lib/dbus/machine-id
sudo dbus-uuidgen --ensure=/etc/machine-id
But i want this included in my basic image so every time i burn my basic image on a sd card this code will run once at first boot. So the machine id will be recreated.

Until now i had the idea to create a line in /etc/rc.local to call a bash script in my home folder where i run these commands , delete /etc/rc.local and copy the /etc/rc.local.bak to make a fresh /etc/rc.local and delete its self and reboot , so that on the next boot there is a fresh /etc/rc.local and the machine id was not the one from the cloned image. But that didn't worked out to well and i thought there have to be a better way to do this. After some unsuccesfully google searches i was hoping someone of the raspberry pi community will help me in the right direction to solve this problem.

Re: How to run a bash script on the very first boot

Posted: Thu Mar 15, 2018 12:08 pm
by topguy
You could extract the serialnumber of the Pi and store it in parallell with the UUID. Then on every boot you could you can check if the serial has changed and create a new UUID if it has. This will also create a new UUID if the SD-card is moved from one Pi to another, but I'm not sure if that is a hinder for you.

Re: How to run a bash script on the very first boot

Posted: Thu Mar 15, 2018 12:23 pm
by thagrol
Mod your script to test if the existing machine id is the default one and exit rather than performing those commands if not.

Call it from /etc/rc.local at ecery boot

Though a more flexible approach would be to compare the existing machine ID with the proposed new one and update if different. That way you could move SD cards between machines and the ID would update as neccessary.

If it's of any use I have a script on github that sets hostname and MAC address (for USB gdaget mode ethernet) at boot time from the Pi's serial number. You could probably mod that.

https://github.com/thagrol/usb-gadget