Skip to content

Process For Creating LOKI Control Host Virtual Environments

Joseph Nobes edited this page Jan 28, 2025 · 10 revisions

LOKI - Process for Creating Control Host Python Virtual Environments

When debugging LOKI systems, or correcting issues where there it not time to produce a new image, it can sometimes be necessary to alter the Python code that is running on the system. Because the adapters are installed with pip as part of the image generation process, this is not as straightforward as just symlinking another directory. Changes also do not persist reboots without scripts to re-perform changes.

There are two general ways to have updated code run from a clone of the control repository at runtime before:

  • Manually symlinking individual source files into the /usr/lib/ installed location in the image. This is quicker but requires very manual choice of files, and can only really be performed with a script
  • Creating a virtual environment in the control mount. This is the most 'evolved' solution as it will install all files automatically. In addition, the means to use the environment are already in the image (a config file switch). However, creating the environment in a way where it both works and is editable by users on the control host without the LOKI system locking the files is surprisingly complex and error prone.

The virtual environment method is the better one, but is can be more difficult to configure correctly than one might expect- hence this document.

Access Issues

Virtual environments cannot simply be read-only directories; the system utilising it actually had to be able to write to the area, which means they also don't play well if being used by more than one system.

Steps

  1. Create the virtual environment from the connected LOKI board, in a detector-specific directory. Do this as the loki user so that it is able to source the environment (this may mean you need to update permissions of the detector specific directory)
    • --system-site-packages means that any modules not replaced will still be used from the image's installed resources
    petalinux-custom:/controlhost/babyd/loki_rev2_ser6$ python3 -m venv --system-site-packages dev-env
  2. Source the environment from LOKI
    petalinux-custom:/controlhost/babyd/loki_rev2_ser6$ source dev-env/bin/activate
    (dev-env) petalinux-custom:/controlhost/babyd/loki_rev2_ser6$
  3. Install the adapter as a relative path
    • This can be shared between detectors if desired
    • You will not be able to use the -e editable path if the directory is not be writeable by loki. To make sure it is, change to the src directory and run chmod a+w ., so that the install can create files on install. This may have unpredictable results when used with multiple systems.
      • Changes therefore may not take effect until you restart the service completely
    • You will need to specify --no-deps. Make sure --no-deps comes first.
    • You can repeat this for other modules you want in your virtual environment if you have cloned them to the control host mountpoint, such as odin-devices if you are using a branch that is not build in.
(dev-env) petalinux-custom:/controlhost/babyd/loki_rev2_ser6$ pip3 install --no-deps -e ../sequences-clone/babyd-embedded/control/
Processing /controlhost/babyd/sequences-clone/babyd-embedded/control
Installing collected packages: babyd
  Running setup.py install for babyd ... done
Successfully installed babyd-0.0.0
WARNING: You are using pip version 19.2.3, however version 24.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command	
  1. Alter the detector config file (in the control host mount) so that it will use this environment when running the detector from boot. Remember that the path is from LOKI's point of view.

    #loki-config.conf
    
    # Use a virtual environment hosted on this control host for this specific detector
    conf_ODIN_DETECTOR_PYVENV_ENABLE=1
    conf_ODIN_DETECTOR_PYVENV_PATH='/controlhost/babyd/loki_rev2_ser6/dev-env'
    ``
    
    
  2. Set the correct static resources path.

  3. Reboot the detector. After reboot you should see the boot log confirming the use of the external environment:

    Wed Apr 24 05:11:22 2024: Using Python virtual environment at /controlhost/babyd/loki_rev2_ser6/dev-env

General Rules

  • Ideally create separate environments for each LOKI device, to avoid any conflicts
  • Don't create the venv as root, or the default user won't be able to use it