Read/Write (RTC)Hardware Clock from within Container

Hi Toradex Community,

I’ve been reading a few past posts as well as the article but having a bit of trouble applying the information to my case.

I am running a TorizonCore v6.1.0 Build 1 image on my Apalis iMX6 (Ixora Carrier Board).

My Qt GUI App is in a container, and since my prototype is to be used a medical device environment, we do not have access to the network.

This means that if my application tracks and sets the date and time, it must edit both the System Time and RTC manually.

In my application, I try to perform the following functions at different times:

  1. Read Hardware Clock upon bootup to verify the system time is sync with it: hwclock -r and date
  2. Set System Time after user sets a new time in my Time screen: date -s "YYYYMMDD HH:mm"
  3. Synchronize Hardware Clock with System Time: hwclock -w

What I’ve done so far is expose my device to the container: /dev/rtc0.

This is required since a medical device will be shut down after each procedure.

However, since my container is running as Torizon user, it seems like both of these commands require root privilege.

I read on the dedicated RTC in Linux article of this website that timedatectl may be used when the system is offline. However, when calling this command within my app container, I get:

torizon@1544cd09d22a:/eclipse_qml$ timedatectl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

Is timedatectl the right thing to do for my specific case? If so, how can I allow usage within my app container?

Thanks in advance!
Anthony

Greetings @anthonyabboud,

Just to make sure I understand correctly. Basically you want to access and update/change the RTC from within a container more or less?

As for your use-case it sounds like the workflow is that while your application/system is running a user can change the system time dynamically at any time. Then of course in case of power loss the RTC will then retain this value. Did I understand everything correctly somewhat?

Is timedatectl the right thing to do for my specific case? If so, how can I allow usage within my app container?

Well as you saw timedatectl requires systemd running to work properly since it interacts with various services and daemons. Now there is systemd in TorizonCore but it’s outside of the container on the host. It might be possible to get this going inside a container but it can make things more complex than needed. You should be able to modify and work with the RTC using just the hwclock command unless you have special requirements or such that need timedatectl specifically.

Now with regards to how to work with RTC inside a container. Have you taken a look at this similar thread here: Update RTC on Apalis imx6

Best Regards,
Jeremias

1 Like

Hi @jeremias.tx ,

Thanks for the reply!

Yes, this is the use-case I am trying to accomplish.

Following this other topic you linked, option 2 worked for me. I am now able to set the Hardware clock manually from the container.

As mentioned, the following was added on my Torizon extension in VSCode:
image

However this method requires to be root inside the container. It’s not ideal, but I guess this is acceptable since I am using the safer method, security-wise.

PS: Quick note, in command line “cap-add” and “security-opt” arguments use dashes, however in the Torizon extension, they only get recognized if I replace the dashes with underscores (i.e. cap_add).

Best,
Anthony

Glad I was able to help.

However this method requires to be root inside the container. It’s not ideal, but I guess this is acceptable since I am using the safer method, security-wise.

Yeah unfortunately I don’t think there’s a good way around this. The RTC is often considered a critical element in a system. Which is why it requires root privileges to interact with for security reasons. Trying to workaround or break this probably would introduce more problems than it solves.

PS: Quick note, in command line “cap-add” and “security-opt” arguments use dashes, however in the Torizon extension, they only get recognized if I replace the dashes with underscores (i.e. cap_add).

This has to do with how the extension and underlying python library works we have a small note about it here: Torizon Best Practices Guide | Toradex Developer Center

Best Regards,
Jeremias

1 Like