Preparing a customized Raspbian image

I wanted to build a customized Raspbian image (.img-File) so I just have to dd it onto a sdcard and the finished os is booting. Stuff I wanted to customize:

Problems:

Here is how I’ve created my customized imgage file using vagrant. Prerequieries is a working vagrant environment.

Preparing the workspace

Create a temporary folder to work in.

:-$ mkdir /tmp/raspi
:-$ cd /tmp/raspi

Then create a Vagrantfile containing Archlinux and start it.

:-$ vagrant init archlinux/archlinux
:-$ vagrant up

While the Archlinux VM boots download Raspbian. Choose the version you like. I go with the headless lite version. After the download finished copy the img file into our workspace and unzip it.

:-$ cp ~/Downloads/2018-06-27-raspbian-stretch-lite.zip .
:-$ unzip 2018-06-27-raspbian-stretch-lite.zip

Afterwards there should be a file ending with .img. Now we leave the Mac terminal and ssh into the Archlinux VM.

:-$ vagrant ssh

Now the real fun begins. Aquire root privileges.

:-$ su -

The root password should be vagrant. The command promt changes now.

Mount the imgage

Copy the imgage file into roots folder and create a directory.

[root@archlinux ~]# cd
[root@archlinux ~]# cp /vagrant/2018-06-27-raspbian-stretch-lite.img .
[root@archlinux ~]# mkdir loop

The image file contains two partitions. The first partition contains the bootlaoder. We want the root filesystem. First we have to calculate the offset so we can mount it. Run fdisk to extract the partition info.

[root@archlinux ~]# fdisk -lu 2018-06-27-raspbian-stretch-lite.img
Disk 2018-06-27-raspbian-stretch-lite.img: 1.8 GiB, 1862270976 bytes, 3637248 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: 0x4d3ee428

Device                                Boot Start     End Sectors  Size Id Type
2018-06-27-raspbian-stretch-lite.img1       8192   96663   88472 43.2M  c W95 FAT32 (LBA)
2018-06-27-raspbian-stretch-lite.img2      98304 3637247 3538944  1.7G 83 Linux

In my example the root filesystem partition starts at 98304 and the sector size is 512 bytes. Mount needs an offset and that’s the product of 98304 * 512. You can calc that on the terminal.

[root@archlinux ~]# expr 98304 \* 512
50331648

Now mount the partition to loop.

[root@archlinux ~]# mount -o loop,offset=50331648 2018-06-27-raspbian-stretch-lite.img loop/

When mount doesn’t display an error everything sould be okay. You can check with ls.

[root@archlinux ~]# cd loop/
[root@archlinux loop]# ls
bin  boot  debootstrap	dev  etc  home	lib  lost+found  media	mnt  opt  proc	root  run  sbin  srv  sys  tmp	usr  var

Perfect! Now we can do the customization.

Customize the image

Note: Chroot won’t work because Archlinux is x86 based and the image is ARM. When you try to chroot you won’t be able to start bash.

[root@archlinux ~]# chroot loop/
chroot: failed to run command ‘/bin/bash’: Exec format error

Enable ssh at start

According to the docs you can start ssh on boot if a file called “ssh” is present. Link: https://www.raspberrypi.org/documentation/remote-access/ssh/README.md

[root@archlinux loop]# touch boot/ssh

Remove the pi user

First go back on the root level.

[root@archlinux system]# cd /root/loop/
[root@archlinux loop]#

Then delete the pi user.

[root@archlinux loop]# grep pi etc/passwd
pi:x:100:1000:,,,:/home/pi:/bin/bash
[root@archlinux loop]# sed -i "/pi/d" etc/passwd
[root@archlinux loop]# grep pi etc/passwd
[root@archlinux loop]#

Then add your own user and a group.

[root@archlinux loop]# echo "veloc1ty:x:1001:1001:,,,:/home/veloc1ty:/bin/bash" >> etc/passwd
[root@archlinux loop]# echo "veloc1ty:x:1001:" >> etc/group

Create a home directory and set the right permissions.

[root@archlinux loop]# mkdir home/veloc1ty
[root@archlinux loop]# chown -R 1001:1001 home/veloc1ty/

Then create a .ssh directory in your home directory and add an authorized keys file.

[root@archlinux loop]# mkdir home/veloc1ty/.ssh
[root@archlinux loop]# echo "yourpublickey" >> home/veloc1ty/.ssh/authorized_keys

Then set the correct permissions.

[root@archlinux loop]# chown -R 1001:1001 home/veloc1ty/.ssh
[root@archlinux loop]# chmod 0700 home/veloc1ty/.ssh

Done with ssh.

Continue with your own customizations.

Unmount the image

After you are done with your customizations unmount the image.

[root@archlinux loop]# cd
[root@archlinux ~]# umount loop

Now copy the image file back to /vagrant so you can access it from Mac.

[root@archlinux ~]# cp 2018-06-27-raspbian-stretch-lite.img /vagrant/customized-raspi.img
[root@archlinux ~]# shutdown -h now
Connection to 127.0.0.1 closed by remote host.
Connection to 127.0.0.1 closed.

Now you have a local customized image file. You can now write it onto a sdcard to test it.


Du hast einen Kommentar, einen Wunsch oder eine Verbeserung? Schreib mir doch eine E-Mail! Die Infos dazu stehen hier.

🖇️ = Link zu anderer Webseite
🔐 = Webseite nutzt HTTPS (verschlüsselter Transportweg)
Zurück