And what if it could be part of the Raspbian image so you could just write your SD card and boot?
Sure you could boot up, manually configure everything, then make one of those huge SD card images with dd, and then try and figure out how to shrink that to fit on smaller cards. If only there was an easier way...
So this is how you can make your own custom burn-n-boot image, the easy way, and you can do it before you write the image to the card!
Cool! Can I do this in Windows?
Nope, you need a Linux computer, but luckily you have a Raspberry Pi, which is a Linux computer. You could also boot a live Linux on your x86 Windows or Mac PC from USB or optical media. This would be a good option: Debian with Raspberry Pi Desktop.
Anyone still here?
Good, let's get started.
What you need to know about the image:
First, extract the Raspbian image from the .zip archive, and then we need to find out what's in the image file.
Code: Select all
fdisk -lu 2018-11-13-raspbian-stretch-lite.img
Code: Select all
Disk 2018-11-13-raspbian-stretch-lite.img: 1.8 GiB, 1866465280 bytes, 3645440 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7ee80803
Device Boot Start End Sectors Size Id Type
2018-11-13-raspbian-stretch-lite.img1 8192 98045 89854 43.9M c W95 FAT32 (LBA)
2018-11-13-raspbian-stretch-lite.img2 98304 3645439 3547136 1.7G 83 Linux
Mounting disk partitions from an image:
You mount an image partition as a loop device (a block device within a file) and mount needs to know where the partition starts with offset={Start X SectorSize}. For the first "boot" partition the start sector is 8192, and you can let the command do the math.
Code: Select all
sudo mkdir /mnt/disk
sudo mount -t vfat -o loop,offset=$((8192*512)) 2018-11-13-raspbian-stretch-lite.img /mnt/disk
Mounting disk images:
If you want to mount the entire Raspbian disk image, mount the 2nd partition first and put the first partition in the mount folder/boot. When mounting another partition into an already mounted one, you must also tell Linux how big the partition is (sizelimit=Sectors X SectorSize).
Code: Select all
sudo mkdir /mnt/disk
sudo mount -t ext4 -o loop,offset=$((98304*512)) 2018-11-13-raspbian-stretch-lite.img /mnt/disk
sudo mount -t vfat -o loop,offset=$((8192*512)),sizelimit=$((89854*512)) 2018-11-13-raspbian-stretch-lite.img /mnt/disk/boot
So you've made your changes, now what?
After making your changes you can un-mount the partitions and your custom image is ready (re-zip the image to save space).
Code: Select all
sudo umount /mnt/disk/boot
sudo umount /mnt/disk
So what can you do with this?
You can add a file named "ssh" (or ssh.txt) to enable SSH logins on first boot. You can add a pre-configured wpa_supplicant.conf file to connect to your wireless network. You can edit config.txt or cmdline.txt, and that's only the first partition. There's even more you can do on the second partition (that's the one Windows and Mac OS can't see). Mounting the entire image (both partitions) allows you to edit other system files that control way more stuff than I can cover here. With this simple technique you can't install software, run updates or add new users, but you could add scripts for those and other complex operations.
The ssh and wpa_supplicant.conf files are what you typically need for a totally headless boot on a wireless network (you can then SSH into the system to enable VNC or make any other changes needed).
Your pre-configured wpa_supplicant.conf should look like this.
Code: Select all
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB
network={
ssid="Your network SSID"
psk="Your WPA/WPA2 security key"
key_mgmt=WPA-PSK
}
Who will benefit from this?
If you only have a couple of Pi computers, it may not help you much. If you have several Pi computers, or manage a classroom full of them, having a custom burn-n-boot image should be a real time saver.
Are we there yet?
Yup, that's it. That's how you make changes to a Raspbian image before it's written to a card, for your very own custom burn-n-boot image.
One final note:
The Raspbian Desktop images contain proprietary software that RPF is licenced to distribute, but you are not. So if you want to make custom images for yourself, that's fine, but you probably shouldn't share them without proper licencing and permissions.