When working with embedded Linux systems using Yocto, configuring timezone information is crucial for accurate system time and logging. This guide provides a detailed, step-by-step approach to adding timezone information to the root filesystem using Yocto, complete with practical examples and tips to ensure successful integration.
Why Add Timezone Information?
- Accurate Timekeeping: Ensures that the system clock reflects the correct local time.
- Logging and Debugging: Provides accurate timestamps in logs for effective troubleshooting.
- User Experience: Enhances usability by displaying correct local time for applications and services.
1. Understanding Yocto and Timezone Integration
Yocto is a powerful build system used to create custom Linux distributions for embedded systems. Adding timezone information involves updating the root filesystem with the necessary timezone data and configuring the system to use it.
2. Configuring Timezone in Yocto
To add timezone information to the root filesystem, follow these steps:
- Add Timezone Data Package Yocto provides a package for timezone data that you can include in your build. Add the
tzdata
package to your image by modifying theIMAGE_INSTALL
variable in your image recipe. Edit your image recipe file, usually located inmeta-yourlayer/recipes-core/images/your-image.bb
, and add:
IMAGE_INSTALL_append = " tzdata"
This line ensures that the tzdata
package is included in the root filesystem.
- Configure Default Timezone Set the default timezone for your system by configuring the
tzdata
package in your Yocto build. You can do this by setting theTZ
environment variable in your local configuration or by creating a configuration file. To set the default timezone globally, add the following line to yourlocal.conf
file in the build directory:
DISTRO_FEATURES_append = " timezone"
Then, specify the default timezone by creating a localtime
symlink in your root filesystem:
echo "America/New_York" > ${D}/etc/timezone
ln -sf /usr/share/zoneinfo/America/New_York ${D}/etc/localtime
Replace America/New_York
with your desired timezone.
- Build Your Image After configuring the timezone settings, rebuild your Yocto image to include the updated timezone information.
bitbake your-image
This command compiles your image with the new timezone settings included.
3. Verifying Timezone Configuration
- Deploy and Boot Deploy your newly built image to your embedded system and boot it.
- Check Timezone Settings Verify that the timezone has been correctly configured by checking the system time and timezone information:
date
cat /etc/timezone
Ensure that the output reflects the correct timezone.
4. Troubleshooting Common Issues
- Timezone Not Applying: Ensure that the
tzdata
package is included and that the timezone configuration inlocal.conf
is correct. - Incorrect Timezone: Verify that the correct timezone data is used and that the symlink in
/etc/localtime
points to the right file.
5. Best Practices
- Regular Updates: Keep the
tzdata
package updated to ensure accurate timezone information. - Testing: Test your timezone configuration in different environments to ensure consistency.
6. Additional Resources
- Yocto Project Documentation: Refer to the Yocto Project documentation for more details on image customization and package management.
- Timezone Database: Visit the IANA Time Zone Database for information on timezone data.
Debugging
Recently while debugging some issues we found that we were missing timezone related files into Yocto root filesystem, the debugging with strace shown the following error,
open("/usr/share/zoneinfo/UTC", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
The poky has a recipe for compiling the relevant files at poky/meta/recipes-extended/tzdata/ hence you can use below command to compile it,
$ bitbake tzdata
Once you have it compiled, you can add this files to the root filesystem by adding into IMAGE_INSTALL as,
IMAGE_INSTALL += "tzdata"
Now, after you boot this newly created root filesystem, we will get following messages at same place where it was failed previously in strace,
open("/etc/localtime", O_RDONLY|O_CLOEXEC) = 16
fstat64(16, {st_mode=S_IFREG|0644, st_size=127, ...}) = 0
fstat64(16, {st_mode=S_IFREG|0644, st_size=127, ...}) = 0
read(16, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1\0\0\0\1\0\0\0\0"..., 4096) = 127
_llseek(16, -6, [121], SEEK_CUR) = 0
read(16, "\nUTC0\n", 4096) = 6
close(16) = 0
Adding timezone information to the root filesystem using Yocto ensures that your embedded Linux system maintains accurate timekeeping and provides a better user experience. By following the steps outlined in this guide, you can successfully integrate timezone data into your Yocto build and configure your system to use the correct local time.
Hi,
Please have a look at the tzdata.bb file available at https://git.yoctoproject.org/cgit.cgi/poky/plain/meta/recipes-extended/tzdata/tzdata_2018e.bb . tzdata seems to be providing sub-packages like tzdata-australia, tzdata-europe, tzdata-asia, etc. Add one of them according to your needs.