0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ros2仮想環境の作成

Last updated at Posted at 2025-11-11

目的

qemu同士を接続し、ros2通信の仮想環境を構築する

確認環境

node os ip
host fedora 42 192.168.7.1
qemu A x86_64のubuntu(*1) 192.168.7.3
qemu B yoctoでビルドしたlinux 192.168.7.4

(*1)
ホストPCがしょぼいので2台armは厳しかった
それなりのマシンパワーと容量あるなら2台armいけるはず

とりあえず1台最低50Gは必要なので、余裕をみて200Gくらい空きあれば
PCIe接続のM.2 SSDでないと結構重いです

接続図

image

接続図のコード
digraph{
rankdir=TB;
bgcolor="white";
node[shape=box,style="filled,rounded",fillcolor="#f0f0f0"];
subgraph cluster_host{
label="HostPC(Fedora)";
style=solid;
color="black";
subgraph cluster_qemu{
label="QEMULayer";
style=dashed;
qemuA[label="QEMUA\nUbuntux86_64\n192.168.7.3"];
qemuB[label="QEMUB\nYoctoARM\n192.168.7.4"];
}veth0[label="veth0\n192.168.7.1"];
br0[label="br0\nLinuxBridge",shape=ellipse,style=filled,fillcolor="#d0f0d0"];
qemuA->br0[label="tap0"];
qemuB->br0[label="tap1"];
veth0->br0[label="veth1"];
}
}

手順

ネットワーク設定

wifi しか繋がってないノートなので
ホスト - qemu通信用に 仮想イーサを作成する
有線接続してるならそのままブリッジに突っ込めばOK

# 仮想ブリッジ作成
sudo ip link add br0 type bridge
sudo ip link set br0 up

# vethペア作成(ホストとブリッジをつなぐ)
sudo ip link add veth0 type veth peer name veth1
sudo ip link set veth0 up
sudo ip link set veth1 up
sudo ip link set veth1 master br0

# ホスト側にIPアドレスを付与(例:192.168.7.1)
sudo ip addr add 192.168.7.1/24 dev veth0

# QEMU A用
sudo ip tuntap add dev tap0 mode tap
sudo ip link set tap0 up
sudo ip link set tap0 master br0

# QEMU B用
sudo ip tuntap add dev tap1 mode tap
sudo ip link set tap1 up
sudo ip link set tap1 master br0

# IP転送を有効化
sudo sysctl -w net.ipv4.ip_forward=1

# NATルール(Wi-Fiインターフェースが wlp3s0)
sudo iptables -t nat -A POSTROUTING -o wlp3s0 -j MASQUERADE

qemu Aの準備

普通にqemuにubuntu入れてrosインストールするだけ

例えばこことかからxubuntu-24.04.3-desktop-amd64.iso をダウンロード

qemu-img create -f qcow2 xubuntu.qcow2 40G
qemu-system-x86_64 -M q35,smm=on \
    -smp 2 -m 8G \
    -enable-kvm \
    -netdev tap,id=net0,ifname=tap0,script=no,downscript=no \
    -device virtio-net-pci,netdev=net0 \
    xubuntu.qcow2 \
    -boot menu=on \
    -cdrom xubuntu-24.04.3-desktop-amd64.iso

でubuntuのインストーラが起動するので普通にインストール

ubuntuでnetplanが悪さしてipが割り当てられてなかったら
/etc/netplan/ 以下を修正する

例えば /etc/netplan/01-netcfg.yaml を作成

network:
  version: 2
  ethernets:
    enp0s2:
      addresses: [192.168.7.3/24]
      routes:
        - to: default
          via: 192.168.7.1
      nameservers:
        addresses: [8.8.8.8]

50-cloud-init.yamlmv50-cloud-init.yaml.backup にリネームするなどする

sudo netplan apply
ip a

で設定反映とip確認

ros2のインストールはこのままなので記載省略

qemu Bの準備

マシンの作り方は以下参照

sudo /sbin/ip a add 192.168.7.4/24 dev eth0
でip設定する(*1)

(*1)
ipコマンドにパスが通ってないと思うので注意

実行

ネットワークさえちゃんとつながっていれば
特に設定などもなしに普通に双方向共つながるはず

A(ubuntu)からB(yocto arm)

qemu A側

ping -w1 192.168.7.4
ros2 topic pub /hello std_msgs/String "data: 'Hello from A'"

qemu B側

ping -w1 192.168.7.3
ros2 topic echo /hello

B側にこんな表示がされればOK

data: Hello from A
---
data: Hello from A
---
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?