SETUP - Here is what I have done on the Pi. Download the Raspbian.zip:
wget -O Raspbian.zip https://downloads.raspberrypi.org/raspbian_latest
and unzip it:
unzip Raspbian.zip
.
I renamed the image file, to save on typing:
mv 2017-04-10-raspbian-jessie.img jessie.img
Run the command sudo fdisk -l jessie.img
to see the sector size 512
and start sector for each image file, 8192
and 92160
.
Make directories mkdir /mnt/d1 /mnt/d2
to mount the image files in loopback mode:
sudo mount -o loop,offset="$((512 * 8192))" jessie.img /mnt/d1
and
sudo mount -o loop,offset="$((512 * 92160))" jessie.img /mnt/d2
.
With these two image file mounted, there is access to the boot and root filesystems. If you wish ssh
to be enabled (in Raspbian), sudo touch /mnt/d1/ssh
and that will make ssh
available (https://www.raspberrypi.org/documentation/remote-access/ssh/).
EDIT: It appears now that mounting two loop objects from the same image does not work. Therefore, the script will not work as written. A work around would be to loop mount the first (boot) image, create the ssh file, copy to temp file, and then umount the image file. Delete the first loop mount instruction and replace the tmp file name inplace of /mnt/d1. Also change /dev/loop1 to loop0.
Here is a draft of bash script.
#!/bin/bash
# Shell script to modify image file and write the new image to /dev/sda.
# If /dev/sda is not the target, change the script, or /dev/sda will be
# overwritten, all current contents of /dev/sda will be lost.
disk="/dev/sda"
num=15
many="$(($num + 40))"
info=$(sudo fdisk -l *.img | grep -E '.img|Sector size' || exit 1)
details=$(echo "$info" | awk '{
if ( NR == 1 ) one_line = $2 # jessie.img
if ( NR == 2 ) one_line = one_line " " $7 # 512
if ( NR == 3 || NR == 4 ) one_line = one_line " " $2
} END{ print one_line }' | tr -d ':')
image=$(echo "$details" | cut -d ' ' -f1) # jessie.img
size=$(echo "$details" | cut -d ' ' -f2) # 512
img1=$(echo "$details" | cut -d ' ' -f3) # 8192
img2=$(echo "$details" | cut -d ' ' -f4) # 92160
sudo mkdir /mnt/d1 /mnt/d2 2> /dev/null
sudo umount /mnt/d[12] 2> /dev/null
sudo mount -o loop,offset="$(( $size * $img1 ))" "$image" /mnt/d1
sudo mount -o loop,offset="$(( $size * $img2 ))" "$image" /mnt/d2
sudo tune2fs -L root /dev/loop1 # write root label
sudo touch /mnt/d1/ssh # enable ssh
for x in $(seq "$num" "$many");
do
echo -n "Insert sd card #$x, press Enter "
read input
lsblk "$disk"
echo -n "Clone $disk? (y/n) "
read input
[[ "$input" == "y" ]] || exit
ls "$disk" > /dev/null || exit 1
sudo umount "$disk"?* 2> /dev/null
new_hostname=$(awk -v x="$x" '{
if ( $1 == "127.0.1.1" ) $2 = " customhost" x
print $0
}' /mnt/d2/etc/hosts)
sudo echo "$new_hostname" | sudo tee /mnt/d2/etc/hosts > /dev/null
sudo echo "customhost$x" | sudo tee /mnt/d2/etc/hostname > /dev/null
cat << EOF | sudo tee /mnt/d2/etc/dhcpcd.conf > /dev/null
interface eth0
static ip_address=10.0.0.$x/24
static routers=10.0.0.1
static domain_name_servers=10.0.0.1
EOF
echo "It takes about 30 minutes to write images."
sudo dd bs=4M if="$image" of="$disk"
sudo sync; sync
echo "Remove sd card #$x."
done