Template:Yocto Create Bootable SD
Create a bootable SD card
SD card structure
This is the structure of our Recovery/Extended SD card:
The SD card is divided into 3 sections as shown in the picture above:
- The first unallocated 4MiB are saved space for U-Boot. It can be replaced using the dd command as described in the Yocto Build U-Boot section.
- The first partition is a fat16 partition used for the device tree files and the kernel image file. You can copy them as described in the Yocto Build Linux section.
- The second partition is an ext4 partition that contains the complete root filesystem (including the kernel modules).
Note:
The last unallocated area is not used. It is there so that the rootfs will fit on any 4GB SD card, as not all 4GB SD cards are really the same size. If you want, you can use a program such as GParted to resize the roofs partition and make it end at the end of your specific SD card (of course, you can also use SD cards with much bigger capacity than 4GB, and then it makes more sense to resize the partition).
Also, if you create the extended SD card yourself by following the Create an extended SD card section below, and you use the '-a' option, the rootfs partition will end at the end of your specific SD card automatically.
Yocto pre-built bootable SD card
The Yocto build products contains many files as explained in the Build Results section. For example, fsl-image-gui-., depending on your build. This is a complete image to be flashed directly to an SD card.
Example usage:
$ sudo umount /dev/sdX*
# For GUI-X11 & Qt-X11 $ cd / Or # For Qt-FB $ cd / # For fsl-image-gui image (GUI-X11) $ sudo dd if=tmp/deploy/images//fsl-image-gui-. of=/dev/sdX bs=1M && sync Or # For fsl-image-qt image (Qt-X11 & Qt-FB) $ sudo dd if=tmp/deploy/images//fsl-image-qt-. of=/dev/sdX bs=1M && sync
Replace sdX with the right device name. This can be obtained by "dmesg" command on your host Linux PC, after the SD card reader is inserted.
Yocto Recovery Image
Beginning in Yocto Langdale, Variscite has released a new recovery Yocto image recipe called “var-recovery-image.” This image is used to create a bootable SD card that contains another target image to be programmed to the eMMC. See the Yocto Recovery SD article on specifics of installing a recovery image.
Usage:
To create a recovery image, simply run the following from your Yocto environment:
$ bitbake var-recovery-image
This will produce an output relative to the build folder of "./tmp/deploy/images//var-recovery-image-." that can be flashed to the SD card. By default, this image boots fsl-image-gui and contains a fsl-image-gui target image to be programmed to eMMC.
You can also specify a different target eMMC image to be embedded in the recovery image by setting VAR_RECOVERY_TARGET_ROOTFS via the command line during build:
i.e.
$ VAR_RECOVERY_TARGET_ROOTFS="<desired-emmc-image-recipe>" bitbake var-recovery-image
Or alternatively, by setting this variable in local.conf:
VAR_RECOVERY_TARGET_ROOTFS = "<desired-emmc-image-recipe>"
Additionally, the file name of the recovery image output can be modified by setting something like the following in local.conf:
VAR_RECOVERY_SD_NAME = “my-recovery-image”
Would produce a recovery image relative to the build folder of "./tmp/deploy/images//my-recovery-image."
Extending the SD Card Size
Flashing the default . images from Yocto results in a rootfs that does not utilize the entire SD card if the card is > 8 GB. This section explains how the SD card can be extended on the build host after flashing.
Procedure:
Start by running fdisk as below and typing "p" and enter to print current partitions. You should see a layout similar to below but numbers may differ depending on card sizes. Note that the partition starts at 16384 (bytes offset 16384*512) which is to account for the boot content explained above.
$ sudo fdisk /dev/sdX Welcome to fdisk (util-linux 2.37.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/sdX: 59.48 GiB, 63864569856 bytes, 124735488 sectors Disk model: MassStorageClass 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: 0x5ebf1617 Device Boot Start End Sectors Size Id Type /dev/sdX1 16384 15165439 15149056 7.2G 83 Linux
Next we will run the following sequence of commands followed by enter with each step explained with a "-" to the right. Each command/input should be followed by enter:
- d - Delete current partition (1).
- n - Create a new parition.
- - Empty, hit enter only which will leave default response p for primary partition.
- - Empty, hit enter only which will leave default response 1 for first partition.
- 16384 - Enter 16384 to begin partition past bootloader section.
- - Empty, hit enter only which will leave last sector as default which should choose ending size of the SD card.
- N - Answers no to not remove the ext4 signature on the disk
- p - Print output and verify before we write. We should see the starting offset the same and the end expanded to fill the SD card.
- w - Write output to disk
See the full log of the above sequence below:
Command (m for help): d Selected partition 1 Partition 1 has been deleted. Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): Using default response p. Partition number (1-4, default 1): First sector (2048-124735487, default 2048): 16384 Last sector, +/-sectors or +/-size{K,M,G,T,P} (16384-124735487, default 124735487): Created a new partition 1 of type 'Linux' and of size 59.5 GiB. Partition #1 contains a ext4 signature. Do you want to remove the signature? [Y]es/[N]o: N Command (m for help): p Disk /dev/sdX: 59.48 GiB, 63864569856 bytes, 124735488 sectors Disk model: MassStorageClass 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 identifier: 0x5ebf1617 Device Boot Start End Sectors Size Id Type /dev/sdX1 16384 124735487 124719104 59.5G 83 Linux Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
Finally, we will need to resize the partitions on the disk. Do that as follows:
$ sudo e2fsck -f /dev/sdX1 e2fsck 1.46.5 (30-Dec-2021) root: recovering journal Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information root: 43155/947488 files (0.1% non-contiguous), 674097/1893632 blocks $ sudo resize2fs /dev/sdX1 resize2fs 1.46.5 (30-Dec-2021) Resizing the filesystem on /dev/sdX1 to 15589888 (4k) blocks. The filesystem on /dev/sdX1 is now 15589888 (4k) blocks long.
Again, numbers and output should differ slightly depending on your card.
Variscite provides the var-create-yocto-sdcard.sh script which creates our recovery SD card - an SD card based on the fsl-image-gui filesystem, which also contain the scripts and relevant binaries for installation to the internal storage of the SOM.
Later, you will be able to follow either the more automatic Yocto Recovery SD card guide or the more manual Installing Yocto to the SOM's internal storage guide.
Note:
This is essentially the same as our pre-built Recovery SD image, with the following main differences:
- The pre-built image's rootfs partition size is 3700MiB, which is also the default size when using the script, but the script also has an option to set the rootfs partition size to fill the whole free space of the used SD card. Anyway, you can always resize the partition later with an external tool such as gparted.
Naturally, the pre-built image is more straight forward and easier to use, while the script method is easier to customize.
Usage:
- Follow the Setup and build Yocto guide, and bitbake fsl-image-gui.
- Plug-in the SD card to your Linux Host PC, run dmesg and see which device is added (i.e. /dev/sdX or /dev/mmcblkX)
$ cd $ sudo MACHINE= sources//scripts/var_mk_yocto_sdcard/var-create-yocto-sdcard.sh <options> /dev/sdX (Replace /dev/sdX with your actual device)
options: -h Display help message -s Only show partition sizes to be written, without actually write them -a Automatically set the rootfs partition size to fill the SD card -r Select alternative rootfs for recovery images (default: /tmp/deploy/images//fsl-image-gui-.*)
If you don't use the '-a' option, a default rootfs size of 3700MiB will be used The '-r' option allows you to create a bootable SD card with an alternative image for the installation to NAND flash or eMMC. Example: "-r tmp/deploy/images//fsl-image-qt-" -- selected the "Qt image with X11" recovery image
Create an extended SD card image using a loop device
It is also possible to use the var-create-yocto-sdcard.sh script to create an extended SD card image, while using a loop device instead of attaching a real SD card.
Create an empty file using the following command:
$ dd if=/dev/zero of=-extended-sd.img bs=1M count=7420
The above command creates a 7420MiB file representing the SD card.
Attach the first available loop device to this file:
$ sudo losetup -Pf -extended-sd.img
To find the actual loop device being used, run:
$ losetup -a | grep -extended-sd.img
Write the content to the loop device to generate the SD card image:
$ sudo MACHINE= sources//scripts/var_mk_yocto_sdcard/var-create-yocto-sdcard.sh <options> /dev/loopX
(Replace /dev/loopX with your actual loop device, e.g. /dev/loop0)
Detach the loop device from the file:
$ sudo losetup -d /dev/loopX
To compress the SD card image file use the following command:
$ gzip -9 -extended-sd.img
To write the SD card image to a real SD card device use the following command:
$ zcat -extended-sd.img.gz | sudo dd of=/dev/sdX bs=1M && sync
(Replace /dev/sdX with your actual SD device, e.g. /dev/sdb)