[meta-fsl-arm][PATCH 2/3] image_types_fsl: Align SD partition and optimize final SD image

102 views
Skip to first unread message

Andrei Gherzan

unread,
Oct 21, 2012, 5:42:16 PM10/21/12
to meta-f...@googlegroups.com, Andrei Gherzan
SD image file was 4G in size but actually the rootfs partition was only
60MB (in some images).
The image file size is not fixed anymore but calculated inside this
class. In this way we will setup the partition for rootfs to fit the
ext file used as SDIMG_ROOTFS. By default the free space is calculated
with IMAGE_OVERHEAD_FACTOR which is by default 1.3. If more free space
is needed this can be overwritten.
IMAGE_ROOTFS_ALIGNMENT set at 4096. This should be the best value for a
SD card.

Signed-off-by: Andrei Gherzan <and...@gherzan.ro>
---
classes/image_types_fsl.bbclass | 95 +++++++++++++++++++++++++++++----------
1 file changed, 72 insertions(+), 23 deletions(-)

diff --git a/classes/image_types_fsl.bbclass b/classes/image_types_fsl.bbclass
index 1b58df8..4724705 100644
--- a/classes/image_types_fsl.bbclass
+++ b/classes/image_types_fsl.bbclass
@@ -43,14 +43,14 @@ IMAGE_DEPENDS_uboot.mxsboot-sdcard = "u-boot-mxsboot-native u-boot"
IMAGE_CMD_uboot.mxsboot-sdcard = "mxsboot sd ${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.${UBOOT_SUFFIX} \
${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.${UBOOT_SUFFIX_SDCARD}"

-# Default to 3.4GiB images
-SDCARD_SIZE ?= "3400"
-
# Boot partition volume id
BOOTDD_VOLUME_ID ?= "Boot ${MACHINE}"

-# Addional space for boot partition
-BOOT_SPACE ?= "5MiB"
+# Boot partition size [in KiB]
+BOOT_SPACE ?= "8192"
+
+# Set alignment to 4MB [in KiB]
+IMAGE_ROOTFS_ALIGNMENT = "4096"

IMAGE_DEPENDS_sdcard = "parted-native dosfstools-native mtools-native \
virtual/kernel ${IMAGE_BOOTLOADER}"
@@ -71,17 +71,29 @@ SDCARD_GENERATION_COMMAND_mx6 = "generate_imx_sdcard"
#
# The disk layout used is:
#
-# 0 - 1M - reserved to bootloader (not partitioned)
-# 1M - BOOT_SPACE - kernel
-# BOOT_SPACE - SDCARD_SIZE - rootfs
+# 0 -> IMAGE_ROOTFS_ALIGNMENT - reserved to bootloader (not partitioned)
+# IMAGE_ROOTFS_ALIGNMENT -> BOOT_SPACE - kernel and other data
+# BOOT_SPACE -> SDIMG_SIZE - rootfs
#
+# Default Free space = 1.3x
+# Use IMAGE_OVERHEAD_FACTOR to add more space
+# <--------->
+# 4KiB 8MiB SDIMG_ROOTFS 4KiB
+# <-----------------------> <----------> <----------------------> <------------------------------>
+# ------------------------ ------------ ------------------------ -------------------------------
+# | IMAGE_ROOTFS_ALIGNMENT | BOOT_SPACE | ROOTFS_SIZE | IMAGE_ROOTFS_ALIGNMENT |
+# ------------------------ ------------ ------------------------ -------------------------------
+# ^ ^ ^ ^ ^
+# | | | | |
+# 0 4096 4KiB + 8MiB 4KiB + 8Mib + SDIMG_ROOTFS 4KiB + 8MiB + SDIMG_ROOTFS + 4KiB
generate_imx_sdcard () {
# Create partition table
parted -s ${SDCARD} mklabel msdos
- parted -s ${SDCARD} mkpart primary 1MiB ${BOOT_SPACE}
- parted -s ${SDCARD} mkpart primary ${BOOT_SPACE} 100%
+ parted -s ${SDCARD} unit KiB mkpart primary ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED})
+ parted -s ${SDCARD} unit KiB mkpart primary $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED}) $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED} \+ $ROOTFS_SIZE)
parted ${SDCARD} print

+ # Burn bootloader
case "${IMAGE_BOOTLOADER}" in
imx-bootlets)
bberror "The imx-bootlets is not supported for i.MX based machines"
@@ -100,6 +112,7 @@ generate_imx_sdcard () {
;;
esac

+ # Create boot partition image
BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDCARD} unit b print \
| awk '/ 1 / { print substr($4, 1, length($4 -1)) / 1024 }')
mkfs.vfat -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS
@@ -112,8 +125,9 @@ generate_imx_sdcard () {
fi
fi

- dd if=${WORKDIR}/boot.img of=${SDCARD} conv=notrunc seek=1 bs=1M
- dd if=${SDCARD_ROOTFS} of=${SDCARD} conv=notrunc seek=1 bs=${BOOT_SPACE}
+ # Burn Partition
+ dd if=${WORKDIR}/boot.img of=${SDCARD} conv=notrunc seek=1 bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync
+ dd if=${SDCARD_ROOTFS} of=${SDCARD} conv=notrunc seek=1 bs=$(expr ${BOOT_SPACE_ALIGNED} \* 1024 + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync
}

#
@@ -132,11 +146,23 @@ generate_mxs_sdcard () {
imx-bootlets)
# The disk layout used is:
#
- # 1M - BOOT_SPACE - kernel
- # BOOT_SPACE - SDCARD_SIZE - rootfs
+ # 0 -> IMAGE_ROOTFS_ALIGNMENT - reserved for bootstream (not partitioned)
+ # IMAGE_ROOTFS_ALIGNMENT -> BOOT_SPACE - kernel and other data
+ # BOOT_SPACE -> SDIMG_SIZE - rootfs
#
- parted -s ${SDCARD} mkpart primary 1MiB ${BOOT_SPACE}
- parted -s ${SDCARD} mkpart primary ${BOOT_SPACE} 100%
+ # Default Free space = 1.3x
+ # Use IMAGE_OVERHEAD_FACTOR to add more space
+ # <--------->
+ # 4KiB 8MiB SDIMG_ROOTFS 4KiB
+ # <-----------------------> <----------> <----------------------> <------------------------------>
+ # ------------------------ ------------ ------------------------ -------------------------------
+ # | IMAGE_ROOTFS_ALIGNMENT | BOOT_SPACE | ROOTFS_SIZE | IMAGE_ROOTFS_ALIGNMENT |
+ # ------------------------ ------------ ------------------------ -------------------------------
+ # ^ ^ ^ ^ ^
+ # | | | | |
+ # 0 4096 4KiB + 8MiB 4KiB + 8Mib + SDIMG_ROOTFS 4KiB + 8MiB + SDIMG_ROOTFS + 4KiB
+ parted -s ${SDCARD} unit KiB mkpart primary ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED})
+ parted -s ${SDCARD} unit KiB mkpart primary $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED}) $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED} \+ $ROOTFS_SIZE)

# Empty 4 bytes from boot partition
dd if=/dev/zero of=${SDCARD} conv=notrunc seek=2048 count=4
@@ -151,11 +177,29 @@ generate_mxs_sdcard () {
# 2M - BOOT_SPACE - kernel
# BOOT_SPACE - SDCARD_SIZE - rootfs
#
- parted -s ${SDCARD} mkpart primary 1MiB 2MiB
- parted -s ${SDCARD} mkpart primary 2MiB ${BOOT_SPACE}
- parted -s ${SDCARD} mkpart primary ${BOOT_SPACE} 100%
+ # The disk layout used is:
+ #
+ # 1M -> 2M - reserved to bootloader and other data
+ # 2M -> BOOT_SPACE - kernel and other data
+ # BOOT_SPACE -> SDIMG_SIZE - rootfs
+ #
+ # Default Free space = 1.3x
+ # Use IMAGE_OVERHEAD_FACTOR to add more space
+ # <--------->
+ # BOOT_SPACE SDIMG_ROOTFS 4KiB
+ # <-------------------------------> <----------------------> <------------------------------>
+ # ------- ------------ ----------- ------------------------ -------------------------------
+ # | | bootloader | kernel |ROOTFS_SIZE | IMAGE_ROOTFS_ALIGNMENT |
+ # ------- ------------ ----------- ------------------------ -------------------------------
+ # ^ ^ ^ ^ ^ ^
+ # | | | | | |
+ # 0 1M 2M BOOTSPACE BOOTSPACE + SDIMG_ROOTFS BOOTSPACE + SDIMG_ROOTFS + 4KiB
+ #
+ parted -s ${SDCARD} unit KiB mkpart primary 1024 2048
+ parted -s ${SDCARD} unit KiB mkpart primary 2048 ${BOOT_SPACE_ALIGNED}
+ parted -s ${SDCARD} unit KiB mkpart primary ${BOOT_SPACE_ALIGNED} $(expr ${BOOT_SPACE_ALIGNED} \+ $ROOTFS_SIZE)

- dd if=${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.${UBOOT_SUFFIX_SDCARD} of=${SDCARD} conv=notrunc seek=1 skip=${UBOOT_PADDING} bs=1MiB
+ dd if=${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.${UBOOT_SUFFIX_SDCARD} of=${SDCARD} conv=notrunc seek=1 skip=${UBOOT_PADDING} bs=$(expr 1024 \* 1024)
BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDCARD} unit b print \
| awk '/ 2 / { print substr($4, 1, length($4 -1)) / 1024 }')

@@ -169,7 +213,7 @@ generate_mxs_sdcard () {
fi
fi

- dd if=${WORKDIR}/boot.img of=${SDCARD} conv=notrunc seek=2 bs=1MiB
+ dd if=${WORKDIR}/boot.img of=${SDCARD} conv=notrunc seek=2 bs=$(expr 1024 \* 1024)
;;
*)
bberror "Unkown IMAGE_BOOTLOADER value"
@@ -183,7 +227,7 @@ generate_mxs_sdcard () {

parted ${SDCARD} print

- dd if=${SDCARD_ROOTFS} of=${SDCARD} conv=notrunc seek=1 bs=${BOOT_SPACE}
+ dd if=${SDCARD_ROOTFS} of=${SDCARD} conv=notrunc seek=1 bs=$(expr ${BOOT_SPACE_ALIGNED} \* 1024 + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync
}

IMAGE_CMD_sdcard () {
@@ -192,8 +236,13 @@ IMAGE_CMD_sdcard () {
exit 1
fi

+ # Align boot partition and calculate total SD card image size
+ BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE} + ${IMAGE_ROOTFS_ALIGNMENT} - 1)
+ BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE_ALIGNED} - ${BOOT_SPACE_ALIGNED} % ${IMAGE_ROOTFS_ALIGNMENT})
+ SDCARD_SIZE=$(expr ${IMAGE_ROOTFS_ALIGNMENT} + ${BOOT_SPACE_ALIGNED} + $ROOTFS_SIZE + ${IMAGE_ROOTFS_ALIGNMENT})
+
# Initialize a sparse file
- dd if=/dev/zero of=${SDCARD} bs=1 count=0 seek=$(expr 1000 \* 1000 \* ${SDCARD_SIZE})
+ dd if=/dev/zero of=${SDCARD} bs=1 count=0 seek=$(expr 1024 \* ${SDCARD_SIZE})

${SDCARD_GENERATION_COMMAND}
}
--
1.7.9.5

Otavio Salvador

unread,
Oct 21, 2012, 5:47:41 PM10/21/12
to meta-f...@googlegroups.com, Andrei Gherzan
On Sun, Oct 21, 2012 at 7:42 PM, Andrei Gherzan <and...@gherzan.ro> wrote:
> SD image file was 4G in size but actually the rootfs partition was only
> 60MB (in some images).
> The image file size is not fixed anymore but calculated inside this
> class. In this way we will setup the partition for rootfs to fit the
> ext file used as SDIMG_ROOTFS. By default the free space is calculated
> with IMAGE_OVERHEAD_FACTOR which is by default 1.3. If more free space
> is needed this can be overwritten.
> IMAGE_ROOTFS_ALIGNMENT set at 4096. This should be the best value for a
> SD card.
>
> Signed-off-by: Andrei Gherzan <and...@gherzan.ro>

I really like the idea.

I will let it rest for some time for people to comment on it.

--
Otavio Salvador O.S. Systems
E-mail: ota...@ossystems.com.br http://www.ossystems.com.br
Mobile: +55 53 9981-7854 http://projetos.ossystems.com.br

Daiane Angolini

unread,
Oct 22, 2012, 8:09:03 AM10/22/12
to meta-f...@googlegroups.com
Where did you defined IMAGE_OVERHEAD_FACTOR?
De default value is 1.3. 1.3M?


I like the idea too



Daiane

Daiane Angolini

unread,
Oct 22, 2012, 1:15:39 PM10/22/12
to meta-f...@googlegroups.com
I tested this patch for imx53qsb using image fsl-image-test and
core-image-base.

Patch is OK to me.

Daiane

Otavio Salvador

unread,
Oct 22, 2012, 1:17:17 PM10/22/12
to meta-f...@googlegroups.com
Before pushing it I want to test it in mxs (mx23 or mx28) so we
confirm it works.

Daiane Angolini

unread,
Oct 23, 2012, 11:27:49 AM10/23/12
to meta-f...@googlegroups.com
Andrei. It looks like your patch broke imx28evk image.

Could you, please, take a look on this? In case you create a V2 for
this patch I can re-test on imx28evk.

Please, the following error.

Your patch is really beautiful, and I'm looking forward to include it.
But we cannot break mxs tree.

Many thanks in advance.

mxs-pxp mxs-pxp.0: initialized
WARNING : No battery connected !
Aborting power driver initialization
mxs-battery: probe of mxs-battery.0 failed with error 1
mxs watchdog: initialized, heartbeat 19 sec
mxs-mmc: MXS SSP Controller MMC Interface driver
mxs-mmc mxs-mmc.0: mmc0: MXS SSP MMC DMAIRQ 82 ERRIRQ 96
dcp dcp.0: DCP crypto enabled.!
usbcore: registered new interface driver hiddev
usbcore: registered new interface driver usbhid
usbhid: USB HID core driver
mmc0: new SDHC card at address 9c76
mmcblk0: mmc0:9c76 SD04G 3.69 GiB
mmcblk0: p1 p2 p3
sgtl5000_hw_read: read reg error : Reg 0x00
Device with ID register 0 is not a SGTL5000
mxs-spdif mxs-spdif.0: MXS SPDIF Audio Transmitter
No device for codec mxs spdif
No device for DAI mxs spdif
No device for DAI mxs-saif
No device for DAI mxs-spdif
asoc: mxs spdif <-> mxs-spdif mapping ok
ALSA device list:
#0: mxs-evk (mxs spdif)
TCP cubic registered
NET: Registered protocol family 17
can: controller area network core (rev 20090105 abi 8)
NET: Registered protocol family 29
can: raw protocol (rev 20090105)
mxs-rtc mxs-rtc.0: setting system clock to 1970-01-01 00:00:09 UTC (9)
List of all partitions:
b300 3872256 mmcblk0 driver: mmcblk
b301 1024 mmcblk0p1
b302 6144 mmcblk0p2
b303 65536 mmcblk0p3
No filesystem could mount root, tried: ext3
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(179,3)
Backtrace:
[<c0034110>] (dump_backtrace+0x0/0x10c) from [<c03ce4cc>] (dump_stack+0x18/0x1c)
r6:c0029a14 r5:c7459005 r4:c0543018 r3:00000000
[<c03ce4b4>] (dump_stack+0x0/0x1c) from [<c03ce6b4>] (panic+0x74/0xf0)
[<c03ce640>] (panic+0x0/0xf0) from [<c0008f14>] (mount_block_root+0x1d4/0x218)
r3:00000000 r2:c049494c r1:c7c27f48 r0:c0487de4
[<c0008d40>] (mount_block_root+0x0/0x218) from [<c0009130>]
(mount_root+0xe0/0x124)
[<c0009050>] (mount_root+0x0/0x124) from [<c00092e0>]
(prepare_namespace+0x16c/0x1cc)
r5:c0029a29 r4:c0542418
[<c0009174>] (prepare_namespace+0x0/0x1cc) from [<c0008b14>]
(kernel_init+0x12c/0x16c)
r5:c0028214 r4:c0028214
[<c00089e8>] (kernel_init+0x0/0x16c) from [<c004e490>] (do_exit+0x0/0x6c0)
r5:c00089e8 r4:00000000

Andrei Gherzan

unread,
Oct 23, 2012, 4:07:52 PM10/23/12
to meta-f...@googlegroups.com
On Tue, Oct 23, 2012 at 6:27 PM, Daiane Angolini <daian...@gmail.com> wrote:
Andrei. It looks like your patch broke imx28evk image.

Possible. I am able (due to hw) to test it on imx53 and imx6. So the rest of the work was done blindly. :) 
 
Could you, please, take a look on this? In case you create a V2 for
this patch I can re-test on imx28evk.

 
Sure. I will take a look and resend it. No problem.
 
Please, the following error.

Your patch is really beautiful, and I'm looking forward to include it.

Hey. You're way too kind...
 
But we cannot break mxs tree.

Obvious is obvious. :) I will have it done soon.

Now about:
Where did you defined IMAGE_OVERHEAD_FACTOR?
De default value is 1.3. 1.3M?

In image_types.bbclass line 86 you can see how ROOTFS_SIZE is generated. This includes a variable named IMAGE_OVERHEAD_FACTOR which is a percentage. This is the amount of free space to be included in the generated image. By default, the image will contain 30% free space. Above that you can have defined IMAGE_ROOTFS_EXTRA_SPACE which adds extra free space - in KiB. 

Now, i use ROOTFS_SIZE variable while calculating the final sd image size. So this includes the above described variables - users can overwrite these variables in local.conf.

ag
 

Andrei Gherzan

unread,
Oct 23, 2012, 5:10:05 PM10/23/12
to meta-f...@googlegroups.com
On Tue, Oct 23, 2012 at 11:07 PM, Andrei Gherzan <and...@gherzan.ro> wrote:
On Tue, Oct 23, 2012 at 6:27 PM, Daiane Angolini <daian...@gmail.com> wrote:
Andrei. It looks like your patch broke imx28evk image.

Possible. I am able (due to hw) to test it on imx53 and imx6. So the rest of the work was done blindly. :) 
 
Could you, please, take a look on this? In case you create a V2 for
this patch I can re-test on imx28evk.

 
Sure. I will take a look and resend it. No problem.

Found the issue. Need to sort it out in an elegant way.  

ag

Otavio Salvador

unread,
Oct 23, 2012, 5:26:28 PM10/23/12
to meta-f...@googlegroups.com
Cool; looking forward to it.
Reply all
Reply to author
Forward
0 new messages