3

Background: I have several RPis which I need to re-image. I use the stock Raspbian and they get their IPs via DHCP. This makes it difficult (in my specific environment) to know which IP they received, and then to configure them.

Today this configuration is automated via Ansible but the first connection is complicated (due to the lack of knowledge of the IP). I would like to move to SaltStack, where the connection is initiated by the client (the RPi in my case).

The question: the SaltStack software still needs to be installed and I would like to "drop" this installation script somewhere on the SD with the image. One thing I do not want to do is to modify the stock Raspbian (rebuild it), just have myinstallation.sh (which would have the relevant commands and configuration) launched either as part of the first boot, or as part of any boot (I would remove it during the configuration).

Is such a functionality available? Or a similar approach?

WoJ
  • 515
  • 2
  • 6
  • 15
  • Would automatic tools such as PiBakery which customise the image be suitable (I described the usage [here](https://raspberrypi.stackexchange.com/a/77400/58316)) or is the requirement that the image can't be modified strict? – Aurora0001 Jan 27 '18 at 14:56
  • *"launched either as part of the first boot, or as part of any boot (I would remove it during the configuration)... a similar approach"* -> Yes, this is exactly what `raspi-config` does. – goldilocks Jan 27 '18 at 16:07
  • @goldilocks: my understanding is that `raspi-config` is a tool one uses interactively after having logged in. This is not at all what I am trying to do (actually I do not want to log in in the first place) – WoJ Jan 27 '18 at 22:31
  • @Aurora0001: this is an interesting solution. After going through the documentation, it looks like the number of things I can do is quite limited, though (what I would be missing is, for instance, the ability to start a bash script, or at least to edit an existing file such as `/etc/hosts` ). – WoJ Jan 27 '18 at 22:32
  • Have you looked into systemd? –  Jan 28 '18 at 04:42
  • @CMalasadas: I know systemd but, again, I would need to configure a service after logging in (and in that case I would be better off just installing the software I need). This is really a RPi question, where I want to add a file to the SD card (flashed with the ISO) and have this file executed on boot – WoJ Jan 28 '18 at 08:45
  • Yes, I should have clarified: There used to be a `raspi-config` *service* that automatically ran `raspi-config` the first time the system was booted, then disabled itself. Looking in a recent image now all that service seems to do is detect shift held down to switch cpu governors; anyway, the point is that it is a fairly simple premise: You install a boot script that disables itself after completing everything it is suppose to do. – goldilocks Jan 28 '18 at 15:04
  • @goldilocks: I am actually running after the "*install*" part: how to do that after having flashd a DS card with the raspbian ISO but without the need to plug an HDMI/ethernet/keyboard to the Rpi, boot it and work from there. Say I have 50 devices to set up - I would like to drop something on the SD card (ideally a file which would be picked up by the OS during the boot) but I believe this does not exist. This is the current approach to [enable ssh](https://raspberrypi.stackexchange.com/a/73121/6123) though - so I was hoping for something more elaborate (like a script) – WoJ Jan 28 '18 at 15:46

1 Answers1

2

As per comments, it is simple enough to create a boot service that runs only once, or runs until some criteria have been satisfied, then it simply disables itself (systemctl disable ...).

However, from the sound of things, you don't want to have to run the image in order to install and enable a new service. Technically this is not necessary; all you have to do to enable a systemd service is create symlink to the service file in an appropriate /etc/systemd/system/ subdirectory (e.g., graphical.target.wants if the system will be running a GUI or multi-user.target.wants otherwise).

If that seems like too much work, rather than adding a file you can replace one: /etc/rc.local, and start whatever from there.

goldilocks
  • 56,430
  • 17
  • 109
  • 217
  • I just added a comment on the way to enable ssh on a headless system (https://raspberrypi.stackexchange.com/a/73121/6123) which is similar to what I am looking for - but with something more elaborate than just a file which acts as a flag (to enable ssh) for the OS. – WoJ Jan 28 '18 at 15:49
  • If you want something like that you'll have to implement it yourself, which means creating a customized image. But again: If you are able to *add* a file, then you might as well *replace* one instead. `/etc/rc.local` is an empty shell script by default, and it is automatically run by init/systemd. – goldilocks Jan 28 '18 at 16:23
  • Well, the difference is that the ssh file is added to the root of the SD card (right after having flashed it, for instance as part of a script which does a dd, rhen a cp of the file (the Rpi has not booted yet)), while your proposition must be done on a running RPi (in which case I could simply install salt and be done) – WoJ Jan 28 '18 at 17:42
  • **No, it does not need to be done on a running Pi.** Read the second paragraph again where it says, *"...from the sound of things, you don't want to have to run the image in order to install and enable a new service. Technically this is not necessary..."* and then describes enabling a service by creating a symlink. You can do that to the card in another computer without ever having used it in a Pi. Even easier: **Just replace `/etc/rc.local` with your own script.** If the problem is you don't have a computer you can read the second partition on, lol -- come on. How easy is that to solve? – goldilocks Jan 29 '18 at 13:50
  • I was in the middle of a long reply to your comment when suddenly one word form your answer ("partition") switched on a light in my head (a bright one :)). I was flashing the SD cards on a Windows system so the only thing I was seeing is the FAT32 /boot partition, the one with actual data was hidden. Now I understand what you are saying and I will just mount the data partition to update (as you mention) either systemd with a service or `/etc/rc.local`. Thanks and sorry for the back-and-forth, I use Linux since 1993 but am learning everyday (especially when things are obvious). Thanks again. – WoJ Jan 29 '18 at 15:48