Here is some background information and methodology on how to store SSH keys on persistent storage, and modify init scripts such that the petalinux image does not generate a new key on each boot:
This sequence of steps describes how to use the flash MTD partition for persistent storage. The use case being considered is to load a previously generated RSA
key and prevent dropbear
from generating a new one (the dropbear init script needs to be modified for doing this).
The Dropbear SSH server is included by default in Petalinux, and is built automatically into the rootfs. Upon boot up, the dropbear init script (named dropbear) is placed and executes from the /etc/init.d
directory:
- This script has a function
gen_keys()
, which is responsible for generating theRSA
key. - If a key already exists at
/etc/dropbear
, thegen_keys()
function first deletes this key, and then generates a new one.
Although not recommended by Xilinx officially, there might be some use cases where this functionality is not desired. For example, when a user wants to avoid having new keys generated upon reboot, and instead loads them from persistent storage.
In such cases, a custom recipe
named dropbear
can be added to the Petalinux project
, with a custom init script that suppresses the call to gen_keys()
. When the image is built and run from the board, the new dropbear init script will run and not create new keys at /etc/dropbear
. The user can then copy over a previously generated key into this location from persistent storage.
This methodology can be dvivded into two parts:
- Generate a
RSA
key (on the board or laptop) and store it in the persistent storage (QSPI Flash) on the FPGA. - Boot up with the modified dropbear init script, mount the QSPI Flash device on a
JFFS
partition, and copy over the previously stored key into/etc/dropbear
.
Part 1:
- Build a normal Petalinux project and use those images to boot into the board. Default kernel config settings should include ZynqMP GQSPI as well as MTD block device support.
- From the Linux terminal, issue
dmesg | grep spi
and verify that four MTD partitions were created. Follow this bycat /proc/mtd
to view the partitions. We will use the ‘spare’ MTD partition/dev/mtd3
for persistent storage. - Erase
/dev/mtd3
partition usingflash_eraseall -j /dev/mtd3
. - Create a mount point for the flash partition:
mkdir /mnt/flash_mtd3
. - Mount flash partition as JFFS2 file system:
mount -t jffs2 /dev/mtdblock3 /mnt/flash_mtd3
. (Note that /dev/mtdblock3 is used rather than /dev/mtd3) - Copy Dropbear RSA key to flash partition:
cp /etc/dropbear/dropbear_rsa_host_key /mnt/flash_mtd3
- Verify the copied key exists on the flash partition:
ls /mnt/flash_mtd3
. - Unmount flash partition:
umount /mnt/flash_mtd3
. - The Dropbear
RSA
key now resides in the persistent flash storage and can be accessed on successive boots - The existing Dropbear init script can be modified and used in Part 2. Copy the script (/etc/init.d/dropbear) over to your host machine.
Part 2:
- Go back to your Petalinux project, and add a folder named ‘dropbear’ to
<plnx_proj_root>/project-spec/meta-user/recipes-core/
. Within this folder, build the following structure:
1 | ├── dropbear |
- The dropbear init script in the ‘files’ folder above is the one you have copied over from Part 1, and modified by modifying the gen_keys().
1 | gen_keys() { |
- Create
dropbear_%.bbappend
as follows:
1 | # The dropbear_%.bbappend looks like this: |
- Add the line
SIGGEN_UNLOCKED_RECIPES_append = "dropbear"
to<plnx_proj_root>/project-spec/meta-user/conf/petalinuxbsp.conf
. - Clean the Petalinux project and re-build:
petalinux-build -x mrproper
andpetalinux-build
. - Boot with the generated images. Observe that the key generation message is not displayed, and no key will exist at
/etc/dropbear
. - Mount flash partition containing original key:
mount -t jffs2 /dev/mtdblock3 /mnt/flash_mtd3
- Copy key into default location:
cp /mnt/flash_mtd3/dropbear_rsa_host_key /etc/dropbear