January 10, 2022

Raspberry, edit config or add files before boot

The actual use case was that I have a Raspberry Pi(RPi) sourcing Vesselfinder with AIS data from nearby. I wanted to change/test a new configuration but on the other hand I didn't want to take the RPi down, during a longer period, since it's an online service.

So the solution was to make a bootable clone of the actual application and test with a second RPi. The major obstacle was that I then would have 2 hosts, with the same hostname, which will mess up the local network.

So how to change the host name on the cloned SD card before I'm using it in the second RPi ?

Another post on how to read and write Linux drives with Windows/MacOS

The idea
I did some Googling, didn't got any direct hit, but found an old blog post, by Jeff Geerling, on how to use a Mac to "mount" a RPi OS SD card, and that gave me the idea.

Steps
  • After the clone, on the first RPi, "mount" the USB adapter with the cloned SD card
  • Edit and change the hostname in the configuration files 
  • Remove the SD card from the first RPi and insert it into the second RPi
  • Boot up and check
This method could not only used for a cloned RPi OS SD card, it could also be any SD card/boot media inserted in one of the RPi:s USB ports that you want to change.

How to
USB media name
Check which name the USB media is assigned

$ sudo fdisk -l | grep sd
Disk /dev/sda: 29.7 GiB, 31914983424 bytes, 62333952 sectors
/dev/sda1 8192 532479 524288 256M c W95 FAT32 (LBA)
/dev/sda2 532480 62333951 61801472 29.5G 83 Linux

which gave that the linux file system is available at "/dev/sda2"

Another command could be "lsblk -f" which will show file system type. For a RPi it will be ext4.

Mount media
Create the directory into which the media will be mounted

$ mkdir /tmp/drive


Here I use the "/tmp" directory which will make that the "/drive" directory will disappear at next boot. Then mount the media with 

$ sudo mount /dev/sda2 /tmp/drive


check with

$ ls -la /tmp/drive
total 84
drwxr-xr-x 18 root root 4096 Oct 30 13:29 .
drwxrwxrwt 8 root root 4096 Jan 17 01:58 ..
lrwxrwxrwx 1 root root 7 Oct 30 13:09 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Jan 1 1970 boot
drwxr-xr-x 2 root root 4096 Nov 15 10:52 dev
drwxr-xr-x 79 root root 4096 Jan 15 20:15 etc
drwxr-xr-x 3 root root 4096 Oct 30 13:12 home
....................

which gives you the directories on the mounted media.

Change hostname
If you have a RPi up and running and you want to change the hostname manually, in the usual way, you have to change 2 files, "hostname" and "hosts". Both placed in directory "/etc". 

So in this case edit the first file, on the mounted media, with 

$ sudo nano /tmp/drive/etc/hostname


and change the existing name to the new one, and then the second file

$ sudo nano /tmp/drive/etc/hosts

and at the line starting with 127.0.1.1 change the existing name to the new one.

Test
Remove the SD card from the first RPi and insert it into the second RPi, boot and check that all works as intended with the new hostname.

Wrap up
Other configuration/files
As said before, this method can of course be used to change any RPi configuration manually or add, change other files on an "offline" mounted media connected to the RPi USB.

Configuration examples
For example to edit the WiFi configuration

$ sudo nano /tmp/drive/etc/wpa_supplicant/wpa_supplicant.conf

or change something in "config.txt"

$ sudo nano /tmp/drive/boot/config.txt

Installing new OS
If you are installing a new RPi OS, using the RPi imager, you can edit some settings direkt via the "Advanced Options" menu with command "Ctrl-Shift-X".

No comments:

Post a Comment

Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !