browser icon
You are using an insecure version of your web browser. Please update your browser!
Using an outdated browser makes your computer unsafe. For a safer, faster, more enjoyable user experience, please update your browser today or try a newer browser.

Boot disk custom image – Raspberry Pi

Posted by on July 29, 2016

Why make a custom boot image

After some experimenting and playing, i had a memory card full of unorganized mini experiments, so  i decided to start again with a fresh disk image. I had been using the full image from https://www.raspberrypi.org/downloads/raspbian/ but decided i wanted a minimal image. I also wanted my disk image to have my wifi details and ssh keys pre installed to avoid having manually edit the network setting each time i wanted to boot up a new raspberry pi.  On my first attempt and making a custom boot disk i ended up with a 32GB disk image. After some googling and retrying i figured out a process to do what i wanted . This is how I made my custom image for the raspberry pi.

How I made my boot image

  • On windows 10 I download the lite image from https://www.raspberrypi.org/downloads/raspbian/ and copied to two 32GB memory card using the tool Win32DiskImager
  • I mounted the first memory card on the windows 10 host and edited the file “cmdline.txt” to remove the init=/usr/lib/raspi-config/init_resize.sh from the end of the boot command line
  • Next I booted the pi up – it took short while the first time but eventually offered a boot prompt and i logged in with the standard pi/raspberry. I edited /etc/wpa_supplicant/wpa_supplicant.conf and added the details for my wifi
    
    country=GB
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    
    network={
    ssid="my_ssid"
    psk="my_password"
    }
    
    
    
  • I rebooted and the pi connected to my home network and i saw its IP on the screen. I checked the disk site to verify was still “small”
    pi@raspberrypi:~ $ df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/root 1.2G 838M 288M 75% /
    devtmpfs 459M 0 459M 0% /dev
    tmpfs 463M 0 463M 0% /dev/shm
    tmpfs 463M 12M 451M 3% /run
    tmpfs 5.0M 4.0K 5.0M 1% /run/lock
    tmpfs 463M 0 463M 0% /sys/fs/cgrou p
    /dev/mmcblk0p1 63M 21M 43M 33% /boot
    
  • I use cygwin on my windows pc so i was able to ssh to the pi using its new ip and scp over my public ssh key.
    dolbyn@pc $ scp .ssh/id_rsa.pub pi@192.168.1.23:
    pi@192.168.1.23's password:
    id_rsa.pub 100% 395 0.4KB/s 00:00
    
    dolbyn@pc$ ssh pi@192.168.1.23
    pi@192.168.1.23's password:
    
    
  • So that i knew things worked as i wanted ,I logged out of the ssh session and logged back .
  • Satisfied things were good, I shutdown the pi,  removed the memory card and remounted on my windows 10 pc. I edited the file “cmdline.txt” to re-add the init=/usr/lib/raspi-config/init_resize.sh from the end of the boot command line.
    dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet init=/usr/lib/raspi-config/init_resize.sh
    
  • After rebooting the image was resized . I wanted to capture the pre-expanded image so i plugged a usb SD card reasder/writer into the Pi. I plugged the second sd image into that one. I mounted the partitions and copied over the files i has working ont he current / first card. Finally i captured the first2 1.5 GB of the disk image and copied it back to my windows desktop. I now have a boot image that knows about mynetwork and public key.
    pi@raspberrypi:~# apt-get install rsync pv
    
    pi@raspberrypi:~# mount  /dev/sda1 /mnt/
    pi@raspberrypi:~# cp /boot/config.txt /mnt/config.txt
    pi@raspberrypi:~# umount /mnt
    
    pi@raspberrypi:~# mount  /dev/sda2 /mnt/
    pi@raspberrypi:~# sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /mnt/etc/wpa_supplicant/
    pi@raspberrypi:~# rsync -a /home/pi/.ssh /mnt/home/pi/
    pi@raspberrypi:~# umount /mnt
    
    pi@raspberrypi:~# sudo dd count=98304 bs=16k if=/dev/sda | pv | dd of=2016-05-27-raspbian-jessie-lite-dolbyn002.img bs=16k
    

Conclusion

Now i can make a clean disk whenever i want to start from a clean start but not have to reconfigure networking and credentials each time. I considered changing the default raspberry pi password but decided to do that at a later time. I expect as i evolve my project I will create further Bootstrapping  images , but for today I have achieved what i wanted

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.