2

I have a base config that I am happy with. The distro is Raspbian.

I would like to create 10s of this same SD card with some subtle differences to each card image.

For example, I would like to set the hostname of each device in sequence. For example: pi-1, pi-2, pi-3 etc..

Is there a way I can script this stuff? Ideally I want a script that runs and will update whatever variables I want. It will then write to the SD card inserted, prompt me to replace the SD card with a new fresh one, press return to continue.

Is this possible or should I take the pain. Dupe a bunch of cards and manually edit the hostname?

Thanks

2

I get your point but if the changes are not really complicated: Why don't you just write one image to all cards and script the changes?

Here would be some example Python code for the automated hostname changes:

import os

number_cards = 9001

for card_id in number_cards:
    # Write to SD card
    os.sys("dd bs=4M if='/home/yourname/masterimg.img' of='/dev/sdXX'")
    # Mount SD card
    os.sys("mount /dev/sdXX /media/temp")
    # Do changes...
    hostname=open("/media/temp/etc/hostname","w")
    hostname.write("pi-"+str(card_id))
    hostname.close()
    # Unmount SD card
    os.sys("umount /dev/sdXX")
    # Prompt for next card
    ignore = raw_input("Card "+str(card_id)+" modified succesfully. Please insert next card and get another coffee.")

Of course you would not have an image of every card afterwards but if the changes are minimal you can save gigs of space for tens of cards.

  • This will only do half the job. If you do not also set the name in /etc/hosts (which raspi-config does) you will get strange "Not Found" error messages when attempting some networking. – Milliways Aug 24 '16 at 0:58
  • @Milliways Good to know. I did it my way (though manual instead of automated) and did not encounter problems so far. I will change the hosts file in my applications. Anyway, my code is just a basic example that should provide the general idea of such an automated mechanism. – Domme Aug 24 '16 at 11:45
2

I use a bash script to set hostname on the Pi itself on 1st boot. This can be read from a file, or generated.

NOTE The other answer may work (I haven't tried it) BUT only does half the job. If you do not also set the name in /etc/hosts (which raspi-config does) you will get strange "Not Found" error messages when attempting some networking.

#!/bin/bash
# script to set Pi hostname based on MAC (or Serial number)
# 2017-08-18
# This script should be run as root (or with sudo) to change names
# If run by a user it will report changes, but will NOT implement them
# Works for PiB (all models), Pi2, Pi3, PiZeroW with on board networking
# PiA models will set a unique Name based on Serial number

PDIR="$(dirname "$0")"  # directory containing script
CURRENT_HOSTNAME=$(cat /etc/hostname)
# Find MAC of eth0, or if not exist wlan0
if [ -e /sys/class/net/eth0 ]; then
    MAC=$(cat /sys/class/net/eth0/address)
elif [ -e /sys/class/net/enx* ]; then
    MAC=$(cat /sys/class/net/enx*/address)
else
    MAC=$(cat /sys/class/net/wlan0/address)
fi

# NOTE the last 6 bytes of MAC and CPUID are identical
CPUID=$(awk '/Serial/ {print $3}' /proc/cpuinfo | sed 's/^0*//')
echo "Current Name" $CURRENT_HOSTNAME
echo "MAC" $MAC
# If you want to specify hostnames create a file PiNames.txt with MAC hostname list e.g.
# b8:27:eb:01:02:03 MyPi
# If not found a unique Name based on Serial number will be set
NEW_HOSTNAME=$(awk /$MAC/' {print $2}' $PDIR"/PiNames.txt")
echo "Name found" $NEW_HOSTNAME
if [ $NEW_HOSTNAME == "" ]; then
    NEW_HOSTNAME="pi"$CPUID
fi

if [ $NEW_HOSTNAME = $CURRENT_HOSTNAME ]; then
    echo "Name already set"
else
    echo "Setting Name" $NEW_HOSTNAME
    echo $NEW_HOSTNAME > /etc/hostname
    sed -i "/127.0.1.1/s/$CURRENT_HOSTNAME/$NEW_HOSTNAME/" /etc/hosts
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.