Skip to content

Instantly share code, notes, and snippets.

@AndreaGhizzoni
Last active October 24, 2017 09:26
Show Gist options
  • Save AndreaGhizzoni/a54b817dcf407fe36ccffe4c638bdb8e to your computer and use it in GitHub Desktop.
Save AndreaGhizzoni/a54b817dcf407fe36ccffe4c638bdb8e to your computer and use it in GitHub Desktop.
customizing-raspbian.md

Customizing Raspbian image

Notes while learning how to custom Raspbian image.

For simplicity I will refer to downloaded image as raspbian.img.

How to mount .img as loopback

The problem is that the .img files are not images of a partition, but of a whole disk. That means they start with a bootloader and a partition table. You have to find out the offset of the partition and mount it with the offset option of mount. If you do a:

cd /path/to/image
fdisk -l raspbian.img

it will show you the block-size and the start-block of the partition. You can use that to calculate the offset. For example, the output of the fdisk command is:

Disk raspbian.img: 1.7 GiB, 1854590976 bytes, 3622248 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: 0x11eccc69

Device        Boot Start     End Sectors  Size Id Type
raspbian.img1       8192   93813   85622 41.8M  c W95 FAT32 (LBA)
raspbian.img2      94208 3622247 3528040  1.7G 83 Linux

.img1 is the boot partition and .img2 is main volume.

To mount each image you need to calculate the offset in block size: in this case the block-size is 512 bytes, so the offset for .img1 is 512 * 8192 = 4194304 and for img2 is 512 * 94208 = 48234496.

Warning: You can not mount the two images at the same time.

Now the mount commands would be:

sudo mount -t auto -o loop,offset=4194304 raspbian.img /mnt/boot
sudo mount -t auto -o loop,offset=48234496 raspbian.img /mnt/data

Enabling SSH

Now you have to mount the boot partition of image with:

sudo mount -t auto -o loop,offset=4194304 raspbian.img /mnt/boot

Then navigate into mounting point:

cd /mnt/boot

To enable ssh service just create an empty file named ssh into the image mount point:

sudo touch ssh

Important: Unmount the boot partition with:

cd /mnt
sudo umount boot

Network configuration

Now you have to mount the data partition of image with:

sudo mount -t auto -o loop,offset=48234496 raspbian.img /mnt/data

Then navigate into mounting point:

cd /mnt/data

Important: Unmount the data partition once done with the following sections:

cd /mnt
sudo umount data

Enabling wlan0

Navigate in:

cd etc/network

And edit interface file by adding something similar to:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Configure wireless network

To configure wlan0 to connect to your wireless network navigate through:

cd etc/wpa_supplicant/

and add to wpa_supplicant.conf the following lines:

# home wifi network settings
network={
    id_str="home"
    ssid="<your-network-ssid-name>"
    scan_ssid=1
    psk="<your-network-password>"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP
    auth_alg=OPEN
}

The wpa_supplicant.conf file can have multiple network={ entries too, I used to take my pi to work... plug it in and voila, it connected automagically there too, work's configuration was a bit more convoluted though. Included here as an example, add/replace the following in the wpa_supplicant.conf file:

network={
    ssid="THE_OFFICE"
    scan_ssid=1
    key_mgmt=WPA-EAP
    eap=PEAP
    identity="WORK_USERNAME"
    password="WORK_PASSWORD"
    phase1="peaplabel=0"
    phase2="auth=MSCHAPV2"
    id_str="SOME_DESCRIPTIVE_NAME"
}

Write .img to SD card

With the following command you can find out which device is associated with the SD card:

lsblk

Assuming that the device is /dev/sdc you can now write the image into the SD card:

cd /path/to/image
sudo dd bs=4M if=raspbian.img of=/dev/sdc

Warning: Device can change in /dev/sdcN or something similar depending of your system. I recommend to check the connected device again with lsblk.

Access to Raspbian

Now after a minute or two of raspberrypi booting you should log into rasbian via ssh:

ssh pi@some-ip

Where some-ip is the ip address given to raspberry by DHCP. To figure that out just run nmap to list all IP for device running with port 22 open:

sudo nmap -p22 -sV 192.168.1.0/24

First Configuration

Once logged in just run sudo raspi-config to:

  • change password for default user
  • change time zone
  • change hostname
  • expand file system Then restart with sudo restart

Run sudo rpi-update to update the firmware the restart.

The update some software:

sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade

If fail to resolve some mirrors

$ nano /etc/resolv.conf

and add:

nameserver 8.8.8.8

Every time you reboot, that file will be reset, so make it immutable:

$ chattr +i /etc/resolv.conf

Sources

Main guide

headless Raspberry Pi

wifi configuration

X11 Forwarding:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment