Read hostname from config file


53 posts   Page 2 of 3   1, 2, 3
by simplesi » Sun Nov 04, 2012 5:10 pm
Just in case I haven't made the situation plain :)
I have a set of 6 RPi that I'm going to be taking around 7 schools for after school/clubs ICT Lessons.

And that's ignoring going to RaspberryJams and testing at home :)

So I really would like to avoid 7xsetting up DHCP servers :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by rurwin » Sun Nov 04, 2012 8:16 pm
No DHCP servers in my solution, just a config file in each RaspPi and a text file to copy to any machine that needs to talk to them.
User avatar
Forum Moderator
Forum Moderator
Posts: 2913
Joined: Mon Jan 09, 2012 3:16 pm
by simplesi » Sun Nov 04, 2012 9:43 pm
Reserve 16 bits of address space for the RaspPis, say 10.255.x.x, then give each Pi a static IP address either using a /boot config file or by making a 16-bit CRC of the serial number. Then provide a hosts file with all the 65536 pi addresses in and distribute that to all the machines that need to access them.


Surely I'm going to have to do this 7x over? - unless I'm missing something ?
Each school has a different 10.x.y.0/24 address space so statics are completely out :(


Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by rurwin » Sun Nov 04, 2012 10:44 pm
OK you need to do this another way.

1. Get the hostname stuff working and then use Reverse Address Resolution Protocol (RARP) to get the IP address. You'll need a RARP server sufficiently intelligent to work out what the IP addresses should be. There's some programming there, but it would be on your RaspPi or laptop. It's not trivial to write.

or

2. Use DHCP on the RaspPis to get a normal IP allocation and then broadcast the address and an identifier later in the boot process. A process on listening machines -- your RaspPi or laptop -- receives those broadcasts and constructs a host file with an entry of each RaspPi. That's also programming, but not as complex. Note that the identifier does not have to be the hostname. If you can get to email from the Raspis there's a quick and dirty halfway house here, but it doesn't do the laptop half and probably makes it more complex.

There's also the very easy bodge,

3. At any point during boot, copy the hostname to /etc/hostname, then reboot. Use SAMBA to propagate the IP address. It may work well enough, and it wouldn't need any R&D.
User avatar
Forum Moderator
Forum Moderator
Posts: 2913
Joined: Mon Jan 09, 2012 3:16 pm
by milhouse » Sun Nov 04, 2012 11:06 pm
Wouldn't running dnsmasq (on another, "master", Pi server) solve this problem?

Just configure dnsmasq with the hostname/MAC/IP addresses of the various Pis in the classroom.

Once you have a "server" in the classroom running dnsmasq, you're bound to find other services that could also run on it (eg. small relational database, web server, file server, etc.)
Posts: 555
Joined: Mon Jan 16, 2012 12:59 pm
by simplesi » Mon Nov 05, 2012 8:59 am
Wouldn't running dnsmasq (on another, "master", Pi server) solve this problem?


Every network the RPis would be connected to would need to have one setup for it :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by rurwin » Mon Nov 05, 2012 10:24 am
I assume you have a Pi or a laptop or something. That could run dnsmasq and implement a private network, say 192.168.x.x.

If your machine had two network connections you could route the private network to the school network. That might be useful if you wanted to monitor the traffic for debugging student work.

There is of course, an even simpler solution; get the RaspPis to announce their IP address in big letters, ask the kids what the numbers are and hand-edit your hosts file. They will probably keep the same IP address for days, weeks or months, so that only needs to be done once.
User avatar
Forum Moderator
Forum Moderator
Posts: 2913
Joined: Mon Jan 09, 2012 3:16 pm
by milhouse » Mon Nov 05, 2012 2:04 pm
simplesi wrote:Every network the RPis would be connected to would need to have one setup for it :)


That isn't the problem the OP is trying to solve - his problem seems to relate solely to his classroom situation. What hostname the Pi has outside of the classroom is irrelevant, he's not going to be VNC'ing into the Pi's when they are outside of the classroom.
Posts: 555
Joined: Mon Jan 16, 2012 12:59 pm
by simplesi » Mon Nov 05, 2012 3:09 pm
My original problem (and title of the thread :) is to read hostname from a config file held in /boot so that cards can be imaged and pre-seeded with a known hostname from a windows PC before being inserted into an RPi :)

Part 2 (not that part 1 has gone well :) ) was put all my RPi serialnums in the txt file in /boot and do the same trick of setting the hostname automatically following card imaging.

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by milhouse » Mon Nov 05, 2012 3:24 pm
simplesi wrote:My original problem (and title of the thread :) is to read hostname from a config file held in /boot so that cards can be imaged and pre-seeded with a known hostname from a windows PC before being inserted into an RPi :)

Part 2 (not that part 1 has gone well :) ) was put all my RPi serialnums in the txt file in /boot and do the same trick of setting the hostname automatically following card imaging.

Simon


Right, but it's not possible (or likely) with current firmware, possibly you could add a start-up script to each device that reads a config file, but this is reinventing the wheel when other solutions being offered should work automatically.

So my question is this: Is #1 dependent on the devices being on a particular network? The justification you initially gave (now that I realise you are the OP...) for wanting a known hostname is that you would be accessing these devices over your classroom network using VNC - is this still the case, or do you want the hostname to be fixed for other reasons, and thus network independent ? Would the child need to know or care about their hostname when booting the Pi outside of the classroom environment?

If you have these devices connecting to a classroom network you're going to want to issue them with IP addresses (unless you give them static IP address which will cause problems when booting those devices outside of the classroom, but this is unlikely if you are cloning images), so it seems obvious to me that you would want to run a DHCP server of some kind, and since you could also combine this with a DNS server in the form of dnsmasq, it would solve your initial problem of allocating hostnames to specific devices based on MAC address.
Posts: 555
Joined: Mon Jan 16, 2012 12:59 pm
by simplesi » Mon Nov 05, 2012 10:17 pm
Eureka (I think)
Code: Select all
#! /bin/sh
### BEGIN INIT INFO
# Provides:          cy_hostname
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before     hostname
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Set hostname based on /boot/cy_hostname.txt
# Description:       Read the machines hostname from /boot/cy_hostname.txt, and
#                    updates /etc/hostname
### END INIT INFO

. /lib/lsb/init-functions

log_daemon_msg "Starting cy_hostname"

if [ -f /boot/cy_hostname.txt ]
        then
          OLDHOST=`cat /etc/hostname`
          NEWHOST=`cat /boot/cy_hostname.txt`
          echo "Changing $OLDHOST to $NEWHOST..."
          echo $NEWHOST > /etc/hostname
          sed -i "s/127.0.1.1.*/127.0.1.1\t$NEWHOST/g" /etc/hosts
          #rm /boot/newhost
          log_daemon_msg "cy_hostname complete"
        fi
exit 0

This code - saved as /etc/init.d/cy_hostname.sh and then followed by
Code: Select all
sudo insserv cy_hostname.sh

seems to do the job :)

It reads in the text from the /boot/cy_hostname.txt file and sticks it into /etc/hostname and /etc/hosts BEFORE hostname.sh is run :)
(unless its just a fluke but it seems to repeatably work fine)
So the answer to my original question seems to be solved:)

But before I move onto the "Why on earth are you trying to do it this way - are you mad???" discussions :) I've come across a little issue that was obviously clouding my earlier tests
If I change the contents of my /boot/cy_hostname.txt file and then issue a re-boot or shutdown comand - everthing is fine.

But if I edit the file do a cat /boot/cy_hostname.txt to make sure the edit is good and then just pull the power off - my edit is lost!

The contents of /boot/cy_hostname.txt are the same as before the edit!

So it looks something like the /boot folder is being cached and if you just pull the power - its not re-written back to the card (or something like that anyway)

regards

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by simplesi » Mon Nov 05, 2012 10:25 pm
So my question is this: Is #1 dependent on the devices being on a particular network? The justification you initially gave (now that I realise you are the OP...) for wanting a known hostname is that you would be accessing these devices over your classroom network using VNC - is this still the case


I want my RPis to keep the same name so I can mark each PI with a number 1- 6 and know that they will respond to say pi1, pi2,pi3 etc no matter which school I take them to or whether they are on my desk back at home.

If you have these devices connecting to a classroom network you're going to want to issue them with IP addresses (unless you give them static IP address which will cause problems when booting those devices outside of the classroom, but this is unlikely if you are cloning images), so it seems obvious to me that you would want to run a DHCP server of some kind, and since you could also combine this with a DNS server in the form of dnsmasq, it would solve your initial problem of allocating hostnames to specific devices based on MAC address.

I'm the network manager of these schools - we have DHCP servers - I just want them to pick up any IP address the DHCP server wishes to allocate to them - I don't want to bother knowing what these are - I just want to use pi1,pi2,pi3 etc.

I could configure each school and add DNS records etc etc etc but I like the easy life.

Since the serial numbers of my RPi's won't change - it seemed liek a good idea to just have a simple lookup config file in /boot with all 6 serial numbers in it and have each RPi effectively auto-allocate their own name even if I re-image the SD card :)
(plus I thought it was a good script writing exercise :) )

regards
Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by milhouse » Mon Nov 05, 2012 10:56 pm
simplesi wrote:seems to do the job :)


So with that script, if Pi #6 is booted with the SD card from Pi #5, Pi #6 will take on the Pi5 hostname - presumably this isn't what you want?

In which case /boot/cy_hostname.txt needs to contain all of the possible hostnames, keyed against something unique to each Pi device, such as serial number (MAC address would also work as this is based on the serial number, but serial number is probably easier to obtain than the MAC address before the network is up).

Selecting the correct hostname based on serial number should be trivial:

Code: Select all
SERIAL=`cat /proc/cpuinfo | grep Serial | awk '{ print $3 }'`
NEWHOST=`grep $SERIAL /boot/cy_hostname.txt | awk '{ print $2 }'`
if [ ! -z "$NEWHOST" ]; then
... change hostname ...
fi


where /boot/cy_hostname.txt looks like:

Code: Select all
00000000de13eab1 pi1
00000000de12eac2 pi2
00000000de10ef9e pi3
00000000de119945 pi4
etc.


And you can see the Serial number for each Pi by typing "cat /proc/cpuinfo" (don't rely on the serial number printed on the board).

simplesi wrote:So it looks something like the /boot folder is being cached and if you just pull the power - its not re-written back to the card (or something like that anyway)


Correct. Execute "sync" to flush the file buffers otherwise Linux will only do so at its leisure, which is obviously not happening before you pull the power.
Posts: 555
Joined: Mon Jan 16, 2012 12:59 pm
by milhouse » Tue Nov 06, 2012 12:30 am
One more thing, you probably don't want to call insserv directly, instead use "update-rc.d sickbeard-daemon defaults" - the details are in "man insserv".
Posts: 555
Joined: Mon Jan 16, 2012 12:59 pm
by rurwin » Tue Nov 06, 2012 8:30 am
Well done on solving the hostname problem.

The difficulty you will have now is that just because the RaspPi knows its own name, it doesn't mean that your computer will know it. SAMBA should solve that, if you have it installed on the RaspPis, but it can take a while to propagate the names.
User avatar
Forum Moderator
Forum Moderator
Posts: 2913
Joined: Mon Jan 09, 2012 3:16 pm
by simplesi » Tue Nov 06, 2012 12:30 pm
you probably don't want to call insserv directly, instead use "update-rc.d sickbeard-daemon defaults" - the details are in "man insserv".


I'd really like to have a Raspbian/Debian guru tell me which of the many contradicting pieces of info on this subject are correct :)

The difficulty you will have now is that just because the RaspPi knows its own name, it doesn't mean that your computer will know it. SAMBA should solve that, if you have it installed on the RaspPis, but it can take a while to propagate the names.

I do use Samba
Code: Select all
http://cymplecy.wordpress.com/2012/08/09/auto-install-a-simple-samba-setup/
:)

Once, I've got @millhouse's code merged into your code, I can test it out in one of my schools and see what happens in practice.

Thanks for everyone sticking with this thread BTW - its been full of good and interesting ideas :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by PeterO » Tue Nov 06, 2012 12:38 pm
See my post here viewtopic.php?f=36&t=21791&p=209950#p209950 for how to get a dhcp server to set the hostname and ip address.

PeterO
User avatar
Posts: 956
Joined: Sun Jul 22, 2012 4:14 pm
by Aydan » Tue Nov 06, 2012 12:49 pm
simplesi wrote:But if I edit the file do a cat /boot/cy_hostname.txt to make sure the edit is good and then just pull the power off - my edit is lost!

The contents of /boot/cy_hostname.txt are the same as before the edit!

So it looks something like the /boot folder is being cached and if you just pull the power - its not re-written back to the card (or something like that anyway)

regards

Simon

Why on earth would you just pull the power?
If you do that shortly after editing a file the file hasn't been synced to the SD card yet. So either do "sync; sync" or power down properly.

Regards
Aydan
Posts: 241
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, Lake Constance
by simplesi » Tue Nov 06, 2012 2:09 pm
@petero - all my schools have w2k3 or w2k8 - i don't have a technical issue (at least I think I don't :) ) in configuring them to hand out names - I just don't want to do that 7x over :)

And I don't have a standalone DHCP server at home - just the one built into my O2 router (which cannot even hand out the same IP to the same MAC address never mind the same name :) )
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by simplesi » Tue Nov 06, 2012 2:16 pm
Why on earth would you just pull the power?


MY RPi doesn't have a nice integrated button that initiates a clean shutdown and its a lot quicker than typing sudo shutdown :)

Seriously though, if I'm sitting at a plain desktop with nothing open then I just switch it off and on again at the mains as its the quickest method to reboot. ) Along with 100,000s of others I'd suspect :) )

I hadn't realised how long the RPi takes to finally write its cache to disc - it must be going for a world record - certainly exceeds my attention span anyway :)

But now I know, I will be better in future :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by bredman » Tue Nov 06, 2012 2:21 pm
simplesi wrote:I hadn't realised how long the RPi takes to finally write its cache to disc

Normally the write to the filesystem is fairly quick. But it is very slow to the /boot directory because this uses a FAT filesystem.
Posts: 1413
Joined: Tue Jan 17, 2012 2:38 pm
by rurwin » Tue Nov 06, 2012 4:27 pm
Linux is Unix. Theoretically Unix used to cache its disk forever. After all, you never know when the cache might be obsoleted by data you overwrite it with. In practise, there was generally a process that sync'd the filesystems every minute. I'm not sure how Linux works, but it's probably something similar. Most Linux filesystems have journals that allow unfinished updates to be completed if the system crashes. However FAT does not have a journal.

Windows has to cope with users who pull the plug without shutting down properly. So it will flush the cache to disk more frequently.
User avatar
Forum Moderator
Forum Moderator
Posts: 2913
Joined: Mon Jan 09, 2012 3:16 pm
by PeterO » Tue Nov 06, 2012 4:39 pm
simplesi wrote:
Why on earth would you just pull the power?


MY RPi doesn't have a nice integrated button that initiates a clean shutdown and its a lot quicker than typing sudo shutdown :)
Simon

At the very least run "sync" a couple of times before pulling the power lead. That should ensure all dirty disk cache entries are flushed back to the SDcard.
PeterO
User avatar
Posts: 956
Joined: Sun Jul 22, 2012 4:14 pm
by simplesi » Tue Nov 06, 2012 9:44 pm
Well thanks to everyones help :) - this seems to be a working init.d script

Code: Select all
#! /bin/sh
### BEGIN INIT INFO
# Provides:          serial_hostname
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before     hostname
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Set hostname based on /boot/serial_hostname.txt
# Description:       Read the machines hostname from /boot/serial_hostname.txt,
#                    based on machine serial number
### END INIT INFO

. /lib/lsb/init-functions

log_daemon_msg "Starting serial_hostname"

if [ -f /boot/serial_hostname.txt ]
        then
          SERIAL=`cat /proc/cpuinfo | grep Serial | awk '{ print $3 }'`
          NEWHOST=`grep $SERIAL /boot/serial_hostname.txt | awk '{ print $2 }'`
          if [ -z "$NEWHOST" ]; then
              sed -i "$ a${SERIAL} ${SERIAL}" /boot/serial_hostname.txt
              NEWHOST=$SERIAL
          fi
          if [ ! -z "$NEWHOST" ]; then
             OLDHOST=`cat /etc/hostname`
             echo "Changing $OLDHOST to $NEWHOST..."
             echo $NEWHOST > /etc/hostname
             sed -i "s/127.0.1.1.*/127.0.1.1\t$NEWHOST/g" /etc/hosts
             #rm /boot/newhost
             log_daemon_msg "serial_hostname complete"
          fi
        fi
exit 0



I've added in a bit extra so that if the machines serial number is not in the serial_hostname.txt file - it will be automatically added (and its serial number used as the name) so that I can semi-automatically harvest my RPi serial numbers and all I'll have to do is edit the file once its been in all machines.

regards
Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
by simplesi » Sun Nov 11, 2012 4:31 pm
Still not fully there yet :(
Came to use the setup in proper use - swapped my working SD card from one Rpi (where name was pi01) to another (that should have changed it to pi02 based on serial number) but its didn't change :(

When I VNCed into the RPi, (Using pi01 name) /etc/hostname was showing pi02 so the script had run but seemingly not before Samab had used the old hostname.

I rebooted again and it appeared as pi02 on the network so at least its half working :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter
Posts: 2071
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK