Get device MAC address from within container

The Supervisor API has endpoint GET /v1/device which provides the IP address. Now, I’m trying to get the MAC address associated with that IP address.

How can I get the MAC address from within a container? I can get it using network_mode: host in docker-compose.yml, but for security reasons, I can’t expose the container like that. Is there another way?

Hi,

This can be done by having your container run with the label io.balena.features.sysfs: '1' as this will then allow you to access the MAC via cat /sys/class/net/{intf name}/address.

I just tried this on my own container and it worked nicely :+1:

@richbayliss using label io.balena.features.sysfs: '1' appears to only work when the service is run with network_mode: host. Without network_mode: host, the interfaces listed in /sys/class/net/, are those of the container and not the device.

Here’s my docker-compose.yml:

version: '2.1'
services:
  init:
    build: ./init
    labels:
      io.balena.features.supervisor-api: '1'
      io.balena.features.sysfs: '1'
    # network_mode: host
    ports:
      - '80:80'
    privileged: true
    restart: always
  wpe:
    build: ./wpe
    depends_on:
      - init
    privileged: true
    restart: always

Not sure what to suggest; I know my own implementation is not running in the host network, Prior to adding the label I would only see the containers interfaces, and once the label was added and the release pushed/running it had the host interfaces.

Are you running this compose file via balena push to cloud/localmode, or simply using plain docker-compose up etc?

@richbayliss I’m running balena push to an rpi3 device in localmode.

To test, I run balena ssh 59e68c5.local init to ssh into the container.

This is what I see within the init container:

root@7581fe9c8415:/usr/src/app# ls -la /sys/class/net/
total 0
drwxr-xr-x  2 root root 0 Jun  3 12:41 .
drwxr-xr-x 55 root root 0 Jun  3 12:39 ..
lrwxrwxrwx  1 root root 0 Jun  3 12:41 eth0 -> ../../devices/virtual/net/eth0
lrwxrwxrwx  1 root root 0 Jun  3 12:41 eth1 -> ../../devices/virtual/net/eth1
lrwxrwxrwx  1 root root 0 Jun  3 12:41 lo -> ../../devices/virtual/net/lo

When I add network_mode: host I get:

root@59e68c5:~# ls -la /sys/class/net/
total 0
drwxr-xr-x  2 root root 0 Jun  3 12:39 .
drwxr-xr-x 55 root root 0 Jun  3 12:39 ..
lrwxrwxrwx  1 root root 0 Jun  3 12:39 balena0 -> ../../devices/virtual/net/balena0
lrwxrwxrwx  1 root root 0 Jun  3 12:39 br-8f1983385f2e -> ../../devices/virtual/net/br-8f1983385f2e
lrwxrwxrwx  1 root root 0 Jun  3 12:39 eth0 -> ../../devices/platform/soc/3f980000.usb/usb1/1-1/1-1.1/1-1.1:1.0/net/eth0
lrwxrwxrwx  1 root root 0 Jun  3 12:39 lo -> ../../devices/virtual/net/lo
lrwxrwxrwx  1 root root 0 Jun  3 12:39 resin-dns -> ../../devices/virtual/net/resin-dns
lrwxrwxrwx  1 root root 0 Jun  3 12:39 resin-vpn -> ../../devices/virtual/net/resin-vpn
lrwxrwxrwx  1 root root 0 Jun  3 12:39 supervisor0 -> ../../devices/virtual/net/supervisor0
lrwxrwxrwx  1 root root 0 Jun  3 12:39 vethfebfea2 -> ../../devices/virtual/net/vethfebfea2

This is very strange and I’m not sure why that’s the case. it’s a little bit hacky but a workaround for this would be to have another network_mode host container which does not expose any information and instead writes out the mac addresses to some shared volume.

As it happens, we were planning on adding the mac address to the device resource so it can be reported, both in the dashboard and by the supervisor api ,so that change should be out relatively soon.

I for sure think it’s worth investigating why the sysfs label is behaving differently for you. What is your balenaOS version?

Thanks for all the help.

I’m not sure why io.balena.features.sysfs: '1' isn’t working for my rpi3 in localmode. I deployed the same code to a production BalenaFin 1.1. device and it’s working as you and @richbayliss described.

@CameronDiver Exposing the MAC Address through the supervisor API would be a great addition!

This is interesting indeed, we are looking into this. Can you please verify the version of the balenaOS that you are running, as also the version of the supervisor (both are visible in the dashboard)? Thanks!

Sure thing; thanks for looking into it.

OS: balenaOS 2.47.0+rev1
Supervisor: 10.6.27
device : RPI 3 B 1.2

Thanks Nick for that info, we are looking into it and will come back when we have more information on the matter. Cheers :call_me_hand:

Hi @nick.slocum – we believe you have hit a bug that has been fixed in the latest version of BalenaOS. Can you please try upgrading to the latest version (2.50.4+rev1, as I write this) and see if it resolves your issue? You can find instructions for this process here; please ensure that any data on the device you wish to preserve is within the /data folder or a named volume. If you’d like details on what happens during the update process, you can read more here.

All the best,
Hugh

@saintaardvark I updated the OS to the new release and it fixed the issue.

Thanks for the help
-Nick

Glad to hear it!

All the best,
Hugh