Ubuntu 20中在LXD容器内安装ROS

19 篇文章 10 订阅

快速构建并销毁一个完整的ROS套件。
关于容器的更多细节可以参考linuxcontainer.org

配置容器

基础lxd创建工作

安装lxd

~$ sudo snap install lxd

安装后用

~$ sudo lxc info

查看信息如下:

If this is your first time running LXD on this machine, you should also run: lxd init
To start your first instance, try: lxc launch ubuntu:18.04
……
……
……

初始化lxd
根据提示运行

~$ sudo lxd init

按照默认一路回车

Would you like to use LXD clustering? (yes/no) [default=no]:
Do you want to configure a new storage pool? (yes/no) [default=yes]:
Name of the new storage pool [default=default]:
Name of the storage backend to use (btrfs, dir, lvm, zfs, ceph) [default=zfs]:
Create a new ZFS pool? (yes/no) [default=yes]:
Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]:
Size in GB of the new loop device (1GB minimum) [default=30GB]:
Would you like to connect to a MAAS server? (yes/no) [default=no]:
Would you like to create a new local network bridge? (yes/no) [default=yes]:
What should the new bridge be called? [default=lxdbr0]:
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
Would you like the LXD server to be available over the network? (yes/no) [default=no]:
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:

查看lxc的profile

~$ sudo lxc profile list
+---------+---------------------+---------+
|  NAME   |     DESCRIPTION     | USED BY |
+---------+---------------------+---------+
| default | Default LXD profile | 0       |
+---------+---------------------+---------+

创建ROS lxc配置

创建lxc的ros profile

~$ sudo lxc profile create rosp
Profile ros created

修改lxc的ros profile

~$ sudo lxc profile edit ros

修改文件至如下:

### This is a YAML representation of the profile.
### Any line starting with a '# will be ignored.
###
### A profile consists of a set of configuration items followed by a set of
### devices.
###
### An example would look like:
### name: onenic
### config:
###   raw.lxc: lxc.aa_profile=unconfined
### devices:
###   eth0:
###     nictype: bridged
###     parent: lxdbr0
###     type: nic
###
### Note that the name is shown but cannot be changed

config:
### X Window显示
  environment.DISPLAY: :0
### 从宿主机映射userid至容器
  raw.idmap: both 1001 1000
### 增加ros repositories
  user.user-data: |
    #cloud-config
    runcmd:
      - "apt-key adv --fetch-keys https://raw.githubusercontent.com/ros/rosdistro/master/ros.key"
      - "apt-add-repository http://packages.ros.org/ros2/ubuntu"
      - "apt-add-repository http://packages.ros.org/ros/ubuntu"
description: ROS
  X0:
    path: /tmp/.X11-unix/X0
    source: /tmp/.X11-unix/X0
    type: disk
### 网络设置为桥接
  eth0:
    name: eth0
    nictype: bridged
    parent: lxdbr0
    type: nic
  root:
    path: /
    pool: default
    type: disk
name: ros
used_by: []

说明

  1. 查看现有id可以用如下命令
~$ id
uid=1001(xh) gid=1001(xh) groups=1001(xh),27(sudo)
  1. repositories的地址,参照ROS官方安装手册中增加ROS 2和ROS 1的repositories
    在这里插入图片描述
  2. 使用X0设备用作XWindow显示,若有多张显卡,也可以使用其他显卡
~$ ls /tmp/.X11-unix/
X1024  X1025
  1. 查看现有网络设置可以用如下命令
~$ ifconfig
eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.46.29  netmask 255.255.255.0  broadcast 192.168.46.255
        inet6 fe80::bb30:ed6a:6ec:6bbd  prefixlen 64  scopeid 0x20<link>
        ether 48:0f:cf:37:a8:a4  txqueuelen 1000  (Ethernet)
        RX packets 26905  bytes 4373082 (4.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 15366  bytes 1836647 (1.8 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 20  memory 0xf7800000-f7820000

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 195  bytes 16883 (16.8 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 195  bytes 16883 (16.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

取可连接外网的网卡名

确认ros profile创建成功

~$ sudo lxc profile list
+---------+---------------------+---------+
|  NAME   |     DESCRIPTION     | USED BY |
+---------+---------------------+---------+
| default | Default LXD profile | 0       |
+---------+---------------------+---------+
| ros     | ROS                 | 0       |
+---------+---------------------+---------+

启动容器

使用lxc launch创建相关的ros容器
我们这里使用ubuntu 20.04的镜像

~$ sudo lxc launch -p ros ubuntu:20.04 rosgalactic
Creating rosgalactic
Starting rosgalactic

用lxc exec来启动容器内的bash shell

~$ sudo lxc exec rosgalactic -- bash
root@rosgalactic:~#

在默认情况下,shell是以root账户启动的
用exit命令可以退出该shell。

指定用户启动
我们可以用–login来指定启动的账户

~$ sudo lxc exec rosgalactic -- su --login ubuntu
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@rosgalactic:~$ id
uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),117(netdev),118(lxd)
ubuntu@rosgalactic:~$

安装ROS

ubuntu@rosgalactic:~$ sudo apt install ros-galactic-desktop

关于ROS本身安装中的问题请参照ROS2学习(一).Ubuntu 20.04安装ROS2 Galactic

启动确认

ubuntu@rosgalactic:~$ rqt

在这里插入图片描述

添加快速启动参数

~$ sudo lxc alias add ubuntu 'exec @ARGS@ --user 1000 --group 1000 --env HOME=/home/ubuntu -- /bin/bash --login'
~$ sudo lxc alias list
+--------+-----------------------------------------------------------------------------------+
| ALIAS  |                                      TARGET                                       |
+--------+-----------------------------------------------------------------------------------+
| ubuntu | exec @ARGS@ --user 1000 --group 1000 --env HOME=/home/ubuntu -- /bin/bash --login |
+--------+-----------------------------------------------------------------------------------+
~$ sudo lxc ubuntu rosgalactic
ubuntu@rosgalactic:~$ source /opt/ros/galactic/setup.bash
ubuntu@rosgalactic:~$ ros2 run demo_nodes_cpp talker

至此,大功告成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LXCLinux Containers)和LXDLinux Container Daemon)是一种轻量级虚拟化技术,常用于在Linux系统创建和管理容器。以下是一些常用的LXC/LXD配置: 1. 安装LXD:使用适合你的Linux发行版的包管理工具进行安装。 2. 初始化LXD:运行`sudo lxd init`命令来初始化LXD。在初始化过程,你可以设置默认的存储池、网络设置、镜像源等。 3. 创建容器:使用`lxc launch`命令创建一个新容器。例如,要创建一个名为"mycontainer"的容器,可以运行`lxc launch images:ubuntu/20.04 mycontainer`。 4. 启动和停止容器:使用`lxc start`命令启动容器,例如`lxc start mycontainer`。使用`lxc stop`命令停止容器,例如`lxc stop mycontainer`。 5. 进入容器:使用`lxc exec`命令可以在容器内执行命令。例如,要在"mycontainer"容器内执行`ls`命令,可以运行`lxc exec mycontainer -- ls`。 6. 管理容器配置:使用`lxc config`命令可以管理容器的配置。例如,要查看容器的IP地址,可以运行`lxc config show mycontainer | grep ipv4.address`。 7. 管理网络:使用`lxc network`命令可以管理LXD网络。例如,要列出所有网络,可以运行`lxc network list`。 8. 导出和导入容器:使用`lxc export`命令可以将容器导出到一个文件。例如,要将"mycontainer"导出到"mycontainer.tar.gz"文件,可以运行`lxc export mycontainer mycontainer.tar.gz`。使用`lxc import`命令可以从导出文件导入一个容器。 这些是LXC/LXD的一些常用配置,还有其他更高级的配置和功能可供探索。你可以使用`lxc --help`命令或参考LXD的官方文档来获取更多详细信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值