Changing network settings at runtime

On a running device, I’m trying use the balena dashboard to follow the instructions at https://www.balena.io/docs/reference/OS/network/2.x/#changing-the-network-at-runtime

Of course this is all a very risky operation, but in order to do this on a device not based on a python image (because I have a different base image…) I tried the following set of commands. I guess I’m a bit frustrated because (a) I’m not starting from a balena python base image, (b) I’m trying to do this in the ssh window, and (c ) the documentation doesn’t provide enough information for those of use who aren’t familiar with all of the linux tools. Thank goodness for google and stackoverflow because otherwise I would have hit the brick wall immediately.

To start, because my container was built with a deletion of the apt-get cache, and using the instructions at https://www.balena.io/docs/reference/OS/network/2.x/#changing-the-network-at-runtime

apt-get update
apt-get install python-dbus
export DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket
apt-get install -y network-manager && systemctl mask NetworkManager.service

Now paste the sample code into a file and try to run it:

nano addwifi.py
python addwifi.py

Now try to install packages that appear to be required (stackoverflow):

pip install python-networkmanager.
apt-get install python-pip python-dev build-essential
pip install --upgrade pip
pip install --upgrade virtualenv
pip install python-networkmanager
apt-get install python-setuptools
pip install python-networkmanager

and now I’m at the errors below, and frustrated. Here is what I would appreciate:
(a) instructions on how to finish this experiment without reflashing the device, OR
(b) a github example of a working docker container and runtime script for a container that I can copy into my multicontainer project, AND
(c ) an understanding that not everyone who attempts to use balenaOS is an expert on linux, python, docker, etc.

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting python-networkmanager
  Using cached https://files.pythonhosted.org/packages/73/ec/2cc46290cedc99f59df3633d428dfa91d95946ca49af96fdb53514ee195f/python-networkmanager-2.1.tar.gz
Collecting dbus-python (from python-networkmanager)
  Downloading https://files.pythonhosted.org/packages/3f/e7/4edb582d1ffd5ac3c84188deea32e960b5c8c0fe1da56ce70224f85ce542/dbus-python-1.2.8.tar.gz (787kB)
    100% |████████████████████████████████| 788kB 7.8MB/s
Collecting six (from python-networkmanager)
  Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Installing collected packages: dbus-python, six, python-networkmanager
  Running setup.py install for dbus-python ... error
    Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-2CGQUU/dbus-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-kYw2of/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    creating /tmp/pip-install-2CGQUU/dbus-python/build
    creating /tmp/pip-install-2CGQUU/dbus-python/build/temp.linux-aarch64-2.7
    checking whether make supports nested variables... yes
    checking whether to enable maintainer-specific portions of Makefiles... no
    checking for gcc... gcc
    checking whether the C compiler works... yes
    checking for C compiler default output file name... a.out
    checking for suffix of executables...
    checking whether we are cross compiling... no
    checking for suffix of object files... o
    checking whether we are using the GNU C compiler... yes
    checking whether gcc accepts -g... yes
    checking for gcc option to accept ISO C89... none needed
    checking whether gcc understands -c and -o together... yes
    checking how to run the C preprocessor... gcc -E
    checking for grep that handles long lines and -e... /bin/grep
    checking for egrep... /bin/grep -E
    checking for ANSI C header files... yes
    checking for sys/types.h... yes
    checking for sys/stat.h... yes
    checking for stdlib.h... yes
    checking for string.h... yes
    checking for memory.h... yes
    checking for strings.h... yes
    checking for inttypes.h... yes
    checking for stdint.h... yes
    checking for unistd.h... yes
    checking minix/config.h usability... no
    checking minix/config.h presence... no
    checking for minix/config.h... no
    checking whether it is safe to define __EXTENSIONS__... yes
    checking for a BSD-compatible install... /usr/bin/install -c
    checking whether build environment is sane... yes
    checking for a thread-safe mkdir -p... /bin/mkdir -p
    checking for gawk... no
    checking for mawk... mawk
    checking whether make sets $(MAKE)... yes
    checking for style of include used by make... GNU
    checking whether UID '0' is supported by ustar format... yes
    checking whether GID '0' is supported by ustar format... yes
    checking how to create a ustar tar archive... gnutar
    checking dependency style of gcc... gcc3
    checking build system type... aarch64-unknown-linux-gnu
    checking host system type... aarch64-unknown-linux-gnu
    checking for native Windows host... no
    checking how to print strings... printf
    checking for a sed that does not truncate output... /bin/sed
    checking for fgrep... /bin/grep -F
    checking for ld used by gcc... /usr/bin/ld
    checking if the linker (/usr/bin/ld) is GNU ld... yes
    checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
    checking the name lister (/usr/bin/nm -B) interface... BSD nm
    checking whether ln -s works... yes
    checking the maximum length of command line arguments... 1572864
    checking how to convert aarch64-unknown-linux-gnu file names to aarch64-unknown-linux-gnu format... func_convert_file_noop
    checking how to convert aarch64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
    checking for /usr/bin/ld option to reload object files... -r
    checking for objdump... objdump
    checking how to recognize dependent libraries... pass_all
    checking for dlltool... no
    checking how to associate runtime and link libraries... printf %s\n
    checking for ar... ar
    checking for archiver @FILE support... @
    checking for strip... strip
    checking for ranlib... ranlib
    checking command to parse /usr/bin/nm -B output from gcc object... ok
    checking for sysroot... no
    checking for a working dd... /bin/dd
    checking how to truncate binary pipes... /bin/dd bs=4096 count=1
    checking for mt... mt
    checking if mt is a manifest tool... no
    checking for dlfcn.h... yes
    checking for objdir... .libs
    checking if gcc supports -fno-rtti -fno-exceptions... no
    checking for gcc option to produce PIC... -fPIC -DPIC
    checking if gcc PIC flag -fPIC -DPIC works... yes
    checking if gcc static flag -static works... yes
    checking if gcc supports -c -o file.o... yes
    checking if gcc supports -c -o file.o... (cached) yes
    checking whether the gcc linker (/usr/bin/ld) supports shared libraries... yes
    checking whether -lc should be explicitly linked in... no
    checking dynamic linker characteristics... GNU/Linux ld.so
    checking how to hardcode library paths into programs... immediate
    checking whether stripping libraries is possible... yes
    checking if libtool supports shared libraries... yes
    checking whether to build shared libraries... yes
    checking whether to build static libraries... no
    checking for inline... inline
    checking for gawk... (cached) mawk
    checking for python... /usr/bin/python
    checking for a version of Python >= '2.1.0'... yes
    checking for a version of Python >= '2.7'... yes
    checking for the distutils Python package... yes
    checking for Python include path... -I/usr/include/python2.7
    checking for Python library path... -L/usr/lib -lpython2.7
    checking for Python site-packages path... /usr/lib/python2.7/dist-packages
    checking python extra libraries... -lpthread -ldl  -lutil -lm
    checking python extra linking flags... -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
    checking consistency of all components of python development environment... yes
    checking for python version... 2.7
    checking for python platform... linux2
    checking for python script directory... ${prefix}/lib/python2.7/site-packages
    checking for python extension module directory... ${exec_prefix}/lib/python2.7/site-packages
    checking for git... /usr/bin/git
    checking python module: sphinx... no
    checking python module: sphinx_rtd_theme... no
    checking for dbus-run-session... /usr/bin/dbus-run-session
    checking for pkg-config... no
    checking for DBUS... no
    configure: error: in `/tmp/pip-install-2CGQUU/dbus-python/build/temp.linux-aarch64-2.7':
    configure: error: The pkg-config script could not be found or is too old.  Make sure it
    is in your PATH or set the PKG_CONFIG environment variable to the full
    path to pkg-config.

    Alternatively, you may set the environment variables DBUS_CFLAGS
    and DBUS_LIBS to avoid the need to call pkg-config.
    See the pkg-config man page for more details.

    To get pkg-config, see <http://pkg-config.freedesktop.org/>.
    See `config.log' for more details
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-2CGQUU/dbus-python/setup.py", line 109, in <module>
        tests_require=['tap.py'],
      File "/usr/lib/python2.7/distutils/core.py", line 151, in setup
        dist.run_commands()
      File "/usr/lib/python2.7/distutils/dist.py", line 953, in run_commands
        self.run_command(cmd)
      File "/usr/lib/python2.7/distutils/dist.py", line 972, in run_command
        cmd_obj.run()
      File "/usr/lib/python2.7/dist-packages/setuptools/command/install.py", line 61, in run
        return orig.install.run(self)
      File "/usr/lib/python2.7/distutils/command/install.py", line 601, in run
        self.run_command('build')
      File "/usr/lib/python2.7/distutils/cmd.py", line 326, in run_command
        self.distribution.run_command(command)
      File "/usr/lib/python2.7/distutils/dist.py", line 972, in run_command
        cmd_obj.run()
      File "/tmp/pip-install-2CGQUU/dbus-python/setup.py", line 62, in run
        cwd=builddir)
      File "/usr/lib/python2.7/subprocess.py", line 186, in check_call
        raise CalledProcessError(retcode, cmd)
    subprocess.CalledProcessError: Command '['/tmp/pip-install-2CGQUU/dbus-python/configure', '--disable-maintainer-mode', 'PYTHON=/usr/bin/python', '--prefix=/tmp/pip-install-2CGQUU/dbus-python/build/temp.linux-aarch64-2.7/prefix']' returned non-zero exit status 1

    ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-2CGQUU/dbus-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-kYw2of/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-2CGQUU/dbus-python/

@jason10 is changing the network a one-time operation, or is it something that you would like to do repeatedly? That configuration/docs is generally for cases when you want to change the network programmatically.

If you want to do this one time only, then you can do that by editing the the network manager configuration on the device. In the host OS, go

vi /etc/NetworkManager/system-connections/resin-wifi-01

and edit it with vi (if not familiar with it, check out some quick tutorials, or can press i for editing mode, navigate to the ssid= and psk= lines, edit them, then ESC, w, q, to save. Then reboot the device with reboot, or restart NetworkManager with systemctl restart NetworkManager.

This is the quick manual way, instead of installing anything.
Let us know if you try it how did it go.

If you want to do it only sometimes, but you are local to the device, you can check out the wifi-connect project, which you can integrate into your project, and would let you change the wifi settings without editing or writing any other code (this I mention for the future)

We are planning to integrate this into the operating system, to make it even easier to use. But that’s further down the line.

For your error messages above, it really depends on what base image you are starting from, and what tools are available. If you have more info on it, that would.

Thanks for the feedback on the documentation as well. We are trying to find a balance between being manageable amount of documentation and having enough information. From what you mention it seems we have to go deeper than we are now.

Actually, I’m guessing the error message is because of a missing dbus library package.

One note, that you don’t need to install python-networkmanager from pip, you could install the system package I think apt-get install python-networkmanager (looking at the available packages)

Well, it started out as a one time change to see if I could bring up wlan0 because it wasn’t connected. And we are planning on using balena-io/wifi-connect. So a bit of both.

I guess the take home message is: don’t use this code unless you meet certain prerequisites, like using a balena python image.

Because we’re on Jetson TX2 and Intel NUCs and because we started last year, We’re using a pretty basic resin image (resin/%%RESIN_MACHINE_NAME%%-debian:stretch) and installing all of the packages I need for C++, Java, CUDA, etc. Now that we have access to multicontainers we’ll be able to split that up and use base images more easily.

Thanks for the explanation and advice.