Skip to content

Instantly share code, notes, and snippets.

@reubenmiller
Last active February 16, 2023 19:46
Show Gist options
  • Save reubenmiller/298936e5b171f477ed71d9aa323160b7 to your computer and use it in GitHub Desktop.
Save reubenmiller/298936e5b171f477ed71d9aa323160b7 to your computer and use it in GitHub Desktop.
thin-edge.io: changing the log output folder

Changing thin-edge.io log output folder

By default thin-edge.io persists logs under the /var/log folder. On some devices the /var folder is volatile, which means that the folder is newly created after each device reboot.

Normally, the folder structure under /var/log is created by thin-edge.io during the installation process, but since the /var/log folder is deleted, some thin-edge.io services do not function as expected.

There are two main ways to solve this problem, they are listed below.

Option 1: Create a initialization service which runs before tedge-agent service

This solution involves creating a simple systemd service which runs before the tedge-agent service starts. The service runs the tedge-agent initialization again (something which is normally only required once on installation).

  1. Create a service file, .e.g called /etc/systemd/system/custom-tedge-init.service

    [Unit]
    Description=tedge init service
    
    # This is important because it tells systemd that this service
    # should run BEFORE tedge-agent.service!
    Before=tedge-agent.service
    
    [Service]
    Type=simple
    
    # Run the initialization of tedge-agent, so it can re-create all of the required folders
    ExecStart=/bin/sh -c "tedge_agent --init"
    
    [Install]
    WantedBy=multi-user.target
  2. Enable the service so it starts on boot

    systemctl enable custom-tedge-init.service
  3. Test out the service by rebooting the device

    shutdown -r now
  4. After the device starts up, check if the custom service ran

    systemctl status custom-tedge-init.service
    

Notes

  • The above instructions are only for systemd. However the concept is the same for other service managers, so please consult the docs for your service manager for the specifics on service dependencies.

Option 2: Change log location to a persistent folder

You can change the location of the log folder to a persistent folder which is not deleted after each reboot. The following example changes the default logs.path setting to /opt/logs.

  1. Stop the tedge-agent service

    sudo systemctl stop tedge-agent
  2. Change the tedge path used by the default logs using tedge config set

    sudo tedge config set logs.path /opt/logs
    sudo mkdir -p /opt/logs/
  3. Inititalize the tedge-agent folders

    # Removing lock due to bug: https://github.com/thin-edge/thin-edge.io/issues/1726
    sudo rm -f /run/lock/tedge*agent.lock
    
    sudo tedge_agent --init
  4. Start the tedge-agent service

    sudo systemctl start tedge-agent

Notes

  • You will also have to update the /etc/tedge/c8y/c8y-log-plugin.toml file, so that any entries that refer to /var/log to /opt/logs (e.g. the path that you changed it to).

Limitations

The following are some of the limitations of the current handlings of the files.

  • Configuration management for child devices relies on the /var/tedge/ folder for caching the files coming from the child devices and when sending to devices. So option 2 will only partially work.

Moving forward the thin-edge.io team would like to make it more configurable to be able to custom location of all files and folders.

@reubenmiller
Copy link
Author

Switched to using tedge_agent --init which is supported in both 0.9.0 and 0.8.x tedge versions.

@janhumble
Copy link

Some comments:

  • /opt/log/ should be /opt/log/tedge? Otherwise the 'tedge' folder won't be created.
  • Also, need to set 'tedge' user permission on the folder as part of the instructions
  • We need to resolve how to accommodate the limitation of child device logs. Will it still work and just delete the logs on 'var' upon reboot or will they also fail to start?

@reubenmiller
Copy link
Author

reubenmiller commented Feb 16, 2023

Some comments:

  • /opt/log/ should be /opt/log/tedge? Otherwise the 'tedge' folder won't be created.
  • Also, need to set 'tedge' user permission on the folder as part of the instructions

No, but yes I did leave out the call to sudo tedge-agent --init which does create the tedge subfolder. I have updated the instructions to suit.

  • We need to resolve how to accommodate the limitation of child device logs. Will it still work and just delete the logs on 'var' upon reboot or will they also fail to start?

This is the perfect opportunity to create a ticket, to detail what your desired functionality is ;) As you don't always just have to accommodate things, we can bring in new features. And I'm guess by "child device logs" you mean the child configuration storage right? Or are you talking about a completely new feature now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment