LXD for runing different ROS2 versions
Tags: lxd, ros2, containerapps, foxy
Published: 2022-11-09 18:01:01

This post gives brief details on how to user LXD to run multiple ROS/ROS2 versions in a Ubuntu machine.

> LXD is a next generation system container and virtual machine manager. It offers a unified user experience around full Linux systems running inside containers or virtual machines.

In my use case I am running ROS2 Humble in my LXD-host machine (Ubuntu 22.04). I want to run ROS2 Foxy (needs Ubuntu 20.04) in a LXD container in order to use `realsense-ros` [package](https://github.com/IntelRealSense/realsense-ros).

## Install LXD
`snap install lxd`

## First time configuration
`lxd init` # defaults are fine

## Launch the desired Ubuntu version
`lxc launch ubuntu:20.04 ros-foxy`

## Run a login shell as the default ubuntu user:
`lxc exec ros-foxy -- sudo -iu ubuntu bash`

## Do a `sudo apt update`
If the packages fail to download and you get a bunch of "W: Failed to fetch ..." warnings, there is no internet access inside the container. 

If you have Docker installed, then most probably the docker daemon is causing this problem. 
> Yep, it is. Docker made your default FORWARD policy to be DROP which will eat all traffic that’s not meant for Docker itself…

> iptables -P FORWARD ACCEPT should temporarily fix that

So run: `iptables -P FORWARD ACCEPT` to fix this issue and try doing a `sudo apt update`.

## Run RViz and other GUI applications
1. `lxc profile create gui`
Note your display variable by running `echo $DISPLAY` in your terminal. My terminal returned `:1`.
2. `lxc profile edit gui`
Paste the following config and change the display variable (in places where "#change here" is mentioned):
```
config:
  environment.DISPLAY: :1 #change here
  raw.idmap: both 1000 1000
  user.user-data: |
    #cloud-config
    runcmd:
      - 'sed -i "s/; enable-shm = yes/enable-shm = no/g" /etc/pulse/client.conf'
      - 'echo export PULSE_SERVER=unix:/tmp/.pulse-native | tee --append /home/ubuntu/.profile'
    packages:
      - x11-apps
      - mesa-utils
      - pulseaudio
description: GUI LXD profile
devices:
  PASocket:
    path: /tmp/.pulse-native
    source: /run/user/1000/pulse/native
    type: disk
  X0:
    path: /tmp/.X11-unix/X1 #change here
    source: /tmp/.X11-unix/X1 # change here
    type: disk
  mygpu:
    type: gpu
name: gui
used_by:
```
## Apply this profile to the existing container and restart using:
`lxc profile add ros-foxy gui`
`lxc restart ros-foxy`

Launch the `ros-foxy` container and `lxc exec` into it using `lxc exec ros-foxy -- sudo -iu ubuntu bash`. Now all the GUI will be X11 forwarded.

## Unsupported version 0 of Verneed record
If this error shows up when trying to `lxc exec`, try restarting your device. This should temporarily fix it. More information can be found [here](https://forum.snapcraft.io/t/unsupported-version-0-of-verneed-record-linux-6-0/32160).

## References
[ROS Development with LXD](https://ubuntu.com/blog/ros-development-with-lxd)
[No internet access inside container but container is able to ping to host](https://discuss.linuxcontainers.org/t/no-internet-access-inside-container-but-container-is-able-to-ping-to-host/13168)