nlecaude
Posts: 4
Joined: Fri Jun 07, 2013 2:33 pm

Set hostname from the /boot partition ?

Sun May 21, 2017 8:17 pm

Hello,

I've been deploying a lot of Raspberry pis lately. I really enjoy you can now setup wifi by having a wpa_supplicant.conf file in the /boot partition. However I think it would be extremely useful if we could perhaps put a hostname file in the /boot partition that would set the hostname on first boot. If I can set this just after imaging I can then pop cards in all of my pis and script the rest of the post-imaging process easily.
Any thoughts ?

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: Set hostname from the /boot partition ?

Sun May 21, 2017 8:40 pm

My first thought is "Yes, this is a necessary functionality". One way or another, it should be required that the hostname be changed before the machine can be used.

My second thought is that we have an ever-growing list of things that would be nice to be able to hack via the "throw a file in /boot" hack. Sooner or later, I think we're going to decide to replace it all with a "run an arbitrary script" type approach - which will allow you to do whatever you want.

This has been discussed a few times already - and I think TPTB are in general agreement with me - just a question of when (and eventuality).
If this post appears in the wrong forums category, my apologies.

nlecaude
Posts: 4
Joined: Fri Jun 07, 2013 2:33 pm

Re: Set hostname from the /boot partition ?

Sun May 21, 2017 8:57 pm

Thanks, I'd be even more happy with a script that runs where I can do what I want :)

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Set hostname from the /boot partition ?

Sun May 21, 2017 9:30 pm

This has been discussed a few times already - and I think TPTB are in general agreement with me - just a question of when (and eventuality).
I don't know what TPTB means but my interpretation of RPi attitude to this involves probability, water, Hell and sharp reduction in temperature :)

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Set hostname from the /boot partition ?

Sun May 21, 2017 9:31 pm

In answer to OP
The practical solution is to use PiBakery for deployments

nlecaude
Posts: 4
Joined: Fri Jun 07, 2013 2:33 pm

Re: Set hostname from the /boot partition ?

Sun May 21, 2017 9:35 pm

I've got nothing against piBakery, it works well but for large deployments I'd like to be able to script this without any GUI involved...

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Set hostname from the /boot partition ?

Sun May 21, 2017 10:02 pm

Then you'll need to make a custom image with a script in one of the usual auto-run places that changes the name, deletes itself and reboots

Hard to imagine this being easier or quicker than using PiBakery

MrEngman
Posts: 3849
Joined: Fri Feb 03, 2012 2:17 pm
Location: Southampton, UK

Re: Set hostname from the /boot partition ?

Sun May 21, 2017 10:06 pm

mattmiller wrote:
This has been discussed a few times already - and I think TPTB are in general agreement with me - just a question of when (and eventuality).
I don't know what TPTB means but my interpretation of RPi attitude to this involves probability, water, Hell and sharp reduction in temperature :)
TPTB - Google suggests - The Powers That Be.



MrEngman
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra

Please post ALL technical questions on the forum. Please Do Not send private messages.

S0litaire
Posts: 216
Joined: Thu Dec 29, 2011 4:24 pm
Location: Ayrshire, Scotland
Contact: ICQ Skype Twitter

Re: Set hostname from the /boot partition ?

Sun May 21, 2017 10:39 pm

Just wondering if this is a possibilty... (or is it a stupid idea...?)

Instead of having a multitude of flags and touched files. how about a raspi-conf.conf file located in /boot.
All the options in raspi-conf can then be configured in this file and read from (and then removed!) during first boot. Similar to a Windows installer "answer" file.
--
Laters

Bill "Solitaire" C

Anáil nathrach, ortha bhas betha, do cheol déanta

User avatar
DougieLawson
Posts: 36105
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Set hostname from the /boot partition ?

Sun May 21, 2017 10:43 pm

S0litaire wrote:Just wondering if this is a possibilty... (or is it a stupid idea...?)

Instead of having a multitude of flags and touched files. how about a raspi-conf.conf file located in /boot.
All the options in raspi-conf can then be configured in this file and read from (and then removed!) during first boot. Similar to a Windows installer "answer" file.
Good idea.

Or if the system finds "firstboot.sh" in /boot then it runs it before deleting it. That shell script could do the same stuff as your "answers" file.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Set hostname from the /boot partition ?

Mon May 22, 2017 6:15 am

Instead of having a multitude of flags and touched files. how about a raspi-conf.conf file located in /boot.
All the options in raspi-conf can then be configured in this file and read from (and then removed!) during first boot. Similar to a Windows installer "answer" file.
Its a great idea but I'd wager a reasonable larg % of my total net worth that RPi will never do this

it took years to persuade them to let us pre-configure the wifi and when I last asked about pre-configure/enable vnc server on boot (the last peg in easy headless setup) I was told no chance ever :(

Ernst
Posts: 1231
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: Set hostname from the /boot partition ?

Mon May 22, 2017 6:29 am

DougieLawson wrote:
S0litaire wrote:Just wondering if this is a possibilty... (or is it a stupid idea...?)

Instead of having a multitude of flags and touched files. how about a raspi-conf.conf file located in /boot.
All the options in raspi-conf can then be configured in this file and read from (and then removed!) during first boot. Similar to a Windows installer "answer" file.
Good idea.

Or if the system finds "firstboot.sh" in /boot then it runs it before deleting it. That shell script could do the same stuff as your "answers" file.
I prefer the "firstboot.sh" option because it would be more versatile.
The road to insanity is paved with static ip addresses

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Set hostname from the /boot partition ?

Mon May 22, 2017 10:33 am

firstboot.sh is best way for us geeks to go as it give us total control but the raspi-config answer file concept would let the less geeky (such as say a school teacher) be able to pre-configure stuff for lessons

I believe discussing either is mute as RPi have said no to either

I don't know what it is they are afraid of but they are afraid (or JBM of course) :)

User avatar
PiBakery
Posts: 24
Joined: Sat May 21, 2016 4:55 pm
Location: Edinburgh
Contact: Website

Re: Set hostname from the /boot partition ?

Thu May 25, 2017 3:42 pm

The firstBoot.sh script is actually exactly what PiBakery does under the hood - the interface just generates the scripts and sticks them in the /boot/PiBakery/firstBoot.sh and /boot/PiBakery/everyBoot.sh files.
PiBakery - The easiest way to setup a Raspberry Pi
www.PiBakery.org
@PiBakery

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: Set hostname from the /boot partition ?

Thu May 25, 2017 5:09 pm

PiBakery wrote:The firstBoot.sh script is actually exactly what PiBakery does under the hood - the interface just generates the scripts and sticks them in the /boot/PiBakery/firstBoot.sh and /boot/PiBakery/everyBoot.sh files.
Thanks. I'd been wondering for some time now how "PiBakery" works.

But how did you get the underlying OS (presumably Raspbian) to execute those files? Did it require a meeting of the minds between yourself and the Raspbian maintainers?
If this post appears in the wrong forums category, my apologies.

papadeltasierra
Posts: 31
Joined: Tue Sep 10, 2013 4:26 pm

Re: Set hostname from the /boot partition ?

Wed Feb 28, 2018 8:01 pm

Disclaimer: I have not tried this yet, but I will be going to...

Setting the hostname from the /boot directory should be possible providing you are happy to create a custom install image. The steps I expect that you need are:
  1. Create an SDcard in the usual manner
  2. Open the card on a Linux machine (or virtualbox VM say if using windows, or there is freely available extX device driver for Windows)
  3. Look at the file

    Code: Select all

    /etc/systemd/system/multi-user.target.wants/raspberrypi-net-mods.service
    This is the file that copies the /boot/wpa_supplicant.conf to /etc/wpa_supplicant/wpa_supplicant.conf
  4. Create a similarly file named, say, raspberrypi-hostname.service, in the same directory and update it to copy /boot/hostname to /etc/hostname
  5. Set the file permissions to be the same as the ...net-mods service
  6. Do similar for any other files that you want to copy.
  7. Now archive this image as your new 'master'
When you want an image, use the master, and you can create files on /boot, even on Windows, and they should get copied over.

Improvements? Perhaps a single service that looks for files below /boot/bootstrap (or deploy, or some other sensible name) and then copies every file found to a suitable place so

Code: Select all

/boot/bootstrap/directory/hostname
is copied to

Code: Select all

/directory/filename
If the general scheme works, I'll be trying this too!

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Set hostname from the /boot partition ?

Wed Feb 28, 2018 8:18 pm

Improvements? Perhaps a single service that looks for files below /boot/bootstrap (or deploy, or some other sensible name) and then copies every file found to a suitable place so
What is really needed - and where everyone eventually ends up when they discuss this issue - is a "runme" type script. That is, rather than the model of "Put out various files in the /boot area and then they get copied - provided they have pre-determined significant names", the real model - where we all wish we'd end up at - is "Let me write a script and have it get executed by the system on first boot" - then I can do whatever I want to, instead of just those things that TPTB have thought of and approved of.

Therefore, you should be able to put a script (with a known, predetermined name) such as /boot/runme
and have it get run by the system on first boot. This script would do whatever you program it to do, copy whatever files you want, etc, etc.

Clearly where we all want to be. And it is just a question of when.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Set hostname from the /boot partition ?

Wed Feb 28, 2018 9:25 pm

Clearly where we all want to be. And it is just a question of when.
RPi have been VERY reluctant to do this - it took years before we got wpa_supplicant facility

So I wouldn't hold any breath :(

papadeltasierra
Posts: 31
Joined: Tue Sep 10, 2013 4:26 pm

Re: Set hostname from the /boot partition ?

Thu Mar 01, 2018 8:52 am

n67, the systemd script is sort of the runme script but I appreciate that perhaps you might want something that cannot be 'preloaded' as a configuration file. This should be possible using something like the following scheme:
  • systemd script/service as described earlier
  • two shell scripts, /boot/deploy/runonce.sh and /boot/deploy/runalways.sh
  • the systemd script/service looks for these and runs them (the environment will be limited but I imagine that adding a new user etc might be possible)
  • the runonce.sh script is renamed to runonce.sh.done after being run to avoid being run again
In a few days I'll be playing with a Pi Zero W and I would like a headless deployment so will see if I can make this work.

mattmiller, this might have to be a 'build the initla image yourself' solution but if it works and we present a completed solution, perhaps they'll be happier to officially adopt it - we can dream!

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Set hostname from the /boot partition ?

Thu Mar 01, 2018 9:51 am

we present a completed solution, perhaps they'll be happier to officially adopt it - we can dream!
Dream on :)

They could adopt PiBakery but they haven't :(

User avatar
thagrol
Posts: 1834
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Set hostname from the /boot partition ?

Thu Mar 01, 2018 2:10 pm

papadeltasierra wrote:
Thu Mar 01, 2018 8:52 am

In a few days I'll be playing with a Pi Zero W and I would like a headless deployment so will see if I can make this work.
I threw together a couple of scripts to generate fixed MAC addresses and hostname from the Pi's serial number. primarily for use with zeros in ethernet gadget mode but usable elsewhere.

You can find them here: https://github.com/thagrol/usb-gadget
This space unintentionally left blank.

User avatar
PiBakery
Posts: 24
Joined: Sat May 21, 2016 4:55 pm
Location: Edinburgh
Contact: Website

Re: Set hostname from the /boot partition ?

Thu Mar 01, 2018 2:22 pm

Martin Frezman wrote:
Thu May 25, 2017 5:09 pm
Thanks. I'd been wondering for some time now how "PiBakery" works.

But how did you get the underlying OS (presumably Raspbian) to execute those files? Did it require a meeting of the minds between yourself and the Raspbian maintainers?
Sorry for not replying to this earlier - my email alert script seemed to miss your reply!
It didn't require any cooperation between me and Raspbian/Raspberry Pi, PiBakery uses a modified Raspbian image, which I modify on a Linux machine and then distribute with PiBakery.

However, the next major version of PiBakery (due to be released fairly soon) will do away with that, and allow any standard Raspbian image or SD card to be used without any pre-modification. I'll go into many more details about how this is done when the new version os released, but essentially it's using the init= parameter of cmdline.txt.

This isn't a special PiBakery thing either. It'll be possible for people who aren't using PiBakery to take any SD card that has Raspbian on it, drop a couple of files (including a script) onto the /boot partition, and have that script execute the next time the Pi boots.
PiBakery - The easiest way to setup a Raspberry Pi
www.PiBakery.org
@PiBakery

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Set hostname from the /boot partition ?

Thu Mar 01, 2018 9:00 pm

However, the next major version of PiBakery (due to be released fairly soon) will do away with that, and allow any standard Raspbian image or SD card to be used without any pre-modification. I'll go into many more details about how this is done when the new version os released, but essentially it's using the init= parameter of cmdline.txt.

This isn't a special PiBakery thing either. It'll be possible for people who aren't using PiBakery to take any SD card that has Raspbian on it, drop a couple of files (including a script) onto the /boot partition, and have that script execute the next time the Pi boots.
Yipee!!!!!! :)

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Set hostname from the /boot partition ?

Fri Mar 02, 2018 12:54 am

Yes, I had forgotten about the init= trick. That's another back door into the thing.

However, my vague recollection (I've never tested it personally, but I believe this was the consensus when this topic came up a long time ago) is that it won't work - because of two interacting limitations:

1) The thing after init= has to be a simple executable - not a command line. I.e., it can't be something like /bin/sh /boot/myscript.

2) For some reason, you can't directly execute things stored in /boot. I.e., often these things get mounted "noexec" and/or do not support regular Unix permission bits (because it is formatted FAT32, not a regular Unix fs).

You can see how these two things interact. But, the funny thing is, it looks to me like /boot is not mounted noexec and everything there seems to be permed 755 (so executable).

P.S. Also, is /boot even going to be mounted when you short-circuit the booting process with the init= trick? My recollection is that when you do the usual init=/bin/bash, you end up in a shell, but with pretty much nothing mounted other than /.

So, all in all, I'd be a little surprised if this thing could actually be made to work...
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
RichardBronosky
Posts: 82
Joined: Thu Feb 14, 2013 5:38 pm
Location: Atlanta, GA, USA

Re: Set hostname from the /boot partition ?

Sun Mar 04, 2018 8:02 pm

PiBakery wrote:
Thu Mar 01, 2018 2:22 pm
I'll go into many more details about how this is done when the new version os released, but essentially it's using the init= parameter of cmdline.txt.
Ha ha! It sounds like you are going to beat me to it! This weekend I started doing the same technique in cmdline.txt setting init=<wrapper that also calls the usual /usr/lib/raspi-config/init_resize.sh script>

The difference to my approach is that I am using the standard Linux run-parts utility to run *.sh in /boot/per-boot.d/ and /boot/per-once.d/ (each script in the latter getting renamed after execution) You can see the basic concept here: https://gist.github.com/RichardBronosky ... n-parts-sh

In other places I attempting to get the entire Raspbian project to switch over to using cloud-init. It is a Standard for doing this kind of thing. There are dozens of ways we could do "execute on boot" and names we could give scripts. But, if we teach people to do it the cloud-init way, they are prepared to use AWS/EC2, Google Cloud, Digital Ocean, Azure, etc. But, that is quite an advanced objective. First I have to gather info on everything that should be moved into cloud-init.
mattmiller wrote:
Wed Feb 28, 2018 9:25 pm
So I wouldn't hold any breath :(
I've been trying to get this implemented since 2013! So, no, don't hold breath. But, back then PlugWash was supportive. I just didn't have the understanding or time until now.
-- Bruno Bronosky
1x RPi 1 (B)
1x RPi 1+ (B)
2x RPi 2
11x RPi 3
4x RPi Zero W
2x RPi camera module
3x AIY Voice kits
https://github.com/RichardBronosky/pi-gen-extender
https://github.com/RichardBronosky/raspbian-boot-setup

Return to “Raspbian”