In this post you will learn how to make some (tiny) changes in the Raspbian SD card image before writing it on the micro SD card.
Assumptions
- You downloaded a Raspbian image (i.e. 2015-11-21-raspbian-jessie.img)
- You are using a computer running Ubuntu (or other linux distribution)
-
Mount Raspbian partition
You need to mount the Raspbian partition of the image.
-
Create a new mountpoint
Creating a mountpoint is as easy as creating a new directory
mkdir mountpoint
-
Find the Raspbian partition in the disk image
Raspbian is at the second partition in the disk image. So you need to find the offset of this partition to be able to mount it.
Execute the command:
file 2015-11-21-raspbian-jessie.img
…in the output there is a line like this:
2015-11-21-raspbian-jessie.img: DOS/MBR boot sector; partition 1 : ID=0xc, start-CHS (0x0,130,3), end-CHS (0x8,40,32), startsector 8192, 122880 sectors; partition 2 : ID=0x83, start-CHS (0x8,40,33), end-CHS (0x1de,79,49), startsector 131072, 7553024 sectors
Find here the start sector of the second partition (131072). To calculate the offset of this partition you have to multiply this by 512 bytes.
-
Mount the partition
sudo mount 2015-11-21-raspbian-jessie.img -o offset=$[512*131072] mountpoint
Remark 1: Calculation of the offset is done inline.
Remark 2: When you see this error: “mount: wrong fs type, bad option, bad superblock on /dev/loop0” probably you specified the wrong offset, did you multiply the start sector with 512 bytes?
-
-
Make changes in the Raspbian image
Now that you mounted the Raspbian partition at ‘mountpoint’, you can make changes.
-
Possible change 1: Configure WLAN-settings
Pre configure for example the WiFi/WLAN-settings. For this you need to have the service set identifier (SSID) of your wireless network and the pre-shared key (PSK). Remember also to grant new WiFi-devices at your wireless router when booting up the Raspberry Pi!
sudo vi mountpoint/etc/wpa_supplicant/wpa_supplicant.conf
Add these lines:
network={ ssid="the_ssid_of_your_network" psk="the_psk_of_your_network" key_mgmt=WPA-PSK }
Of course replace the ‘the_ssid_of_your_network‘ and ‘the_psk_of_your_network‘-values with those of your network. Replace ‘update_config=1’ with ‘update_config=0’.
-
Possible change 2: Enable SSH-server
As of the November 2016 release, Raspbian has the SSH server disabled by default. It can be enabled by placing a file named ‘ssh’, without any extension, onto the boot partition of the SD card. When the Pi boots, it looks for the ssh file. If it is found, SSH is enabled, and the file is deleted. For this change you have to mount the boot partition instead of the second partition. In this example the boot partition starts at: 8192. Use this value in step 3 (Mount the partition).
touch mountpoint/ssh
-
-
Unmount the disk image
When you are ready making your modifications, you can unmount the image again.
sudo umount mountpoint
-
Ready to write image
Now you are ready to write the image in the usual way to the SD card.
Use for example:sudo dd bs=4M if=2015-11-21-raspbian-jessie.img of=/dev/sdb
…when you like to write disk image 2015-11-21-raspbian-jessie.img to the SD card which is known by: /dev/sdb
Caution: All data will be lost at device: /dev/sdb -
Flush the write cache
Before removing the SD card, issue the command:
sync
and wait for the prompt. Maybe your computer detected the new partitions at the SD card and mounted them automatically, please unmount them again. Now it’s safe to remove the SD card from the computer.