How to apply a netplan from a snap?

Hi Folks,

Is it possible to apply a new netplan from within a snap?

A bit of an explanation:

I have a web service being hosted from a snap where the user can login and configure this IoT device. One of the options will be to select which WiFi to connect to.

Once the user selects his wifi and saves, a new netplan will be generated.

After which, I will need the new netplan to be loaded upon reboot.

Is that a possibility? If so, what am I looking at?

Thanks!

1 Like

take a look here to get some ideas …

(this uses the now obsolete udisks2 snap for mounting the usb stick with configs on it, for a new implementation the udisks2 needs to be pulled in and configured as stage-package)

Thanks, that helped a lot.

I’ve got one last question, how can I cp a new netplan over without needing sudo?

Current snapcraft.yaml

name: netplan-test
version: '1'
summary: netplan-test
description: |
  Test for netplan.
grade: stable

architectures:
  - build-on: arm64
    run-on: arm64

confinement: strict

base: core18

plugs:
  netplan-files:
    interface: system-files
    write: [ /etc/netplan ]

apps:
  netplan-test:
      command: ./netplan-apply.sh
      daemon: oneshot
      plugs: [netplan-files, hostname-control, network, network-bind, network-setup-control]

parts:
  netplan:
    plugin: dump
    source: ./app

Current netplan-apply.sh

#!/bin/bash

cp $SNAP/netplan-ap.yaml /etc/netplan/
netplan apply

make it a daemon … add a check if /etc/netplan/netplan-ap.yaml exists to the top of your script and make it exit gracefully should the file be there.

CC @ijohnson as he was working on this area.

it might be that you can not call netplan apply directly, i think only the dbus interface is actually allowed by network-setup-control, so you want something like:

dbus-send --system --type=method_call --print-reply \
                  --dest=io.netplan.Netplan /io/netplan/Netplan \
                  io.netplan.Netplan.Apply

Interesting,

Here is the output of the dbus

ubuntu@ubuntu:~/Desktop/netplan snap$ snap run netplan-test.new-netplan
Error org.freedesktop.DBus.Error.AccessDenied: Access to io.netplan.Netplan.Apply() not permitted.

is the network-setup-control interface connected ? also use sudo, the /etc/netplan/ dir is not writable by normal users …

Connected?

I feel another lesson coming on!

How do I connect it? Is it something I can do in the snapcraft.yaml?

snap connect <yoursnapname>:network-setup-control
(it does not auto-connect)

Ok, so that requires authentication.

Is it possible to apply the netplan without authentication?

by running it as a daemon (and have an unauthenticated user facing app that sends control commands to that daemon)

So instead of daemon: oneshot I would go simple?

How would I trigger it then?