Skip to content

Instantly share code, notes, and snippets.

@aelindeman
Last active May 17, 2024 06:29
Show Gist options
  • Save aelindeman/a0a195494d63181642954ef0e99034d4 to your computer and use it in GitHub Desktop.
Save aelindeman/a0a195494d63181642954ef0e99034d4 to your computer and use it in GitHub Desktop.
ZeroTier on PiKVM

Using ZeroTier on PiKVM

Requires PiKVM OS version 2022.06.20 or newer.

Steps

  1. Install ZeroTier, then start and stop it to generate an identity

    rw
    pacman -S zerotier-one
    systemctl enable --now zerotier-one.service
    systemctl stop zerotier-one.service
  2. Copy the ZeroTier address

    cat /var/lib/zerotier-one/identity.public | cut -d: -f 1
  3. Add the new address to your ZeroTier network on my.zerotier.com (click your network, then Settings → Advanced → Manually Add Member → Node ID)

  4. Move ZeroTier identity files from /var/lib/zerotier-one to persistent storage

    pst=/var/lib/kvmd/pst/data/zerotier-one
    kvmd-pstrun -- cp -a /var/lib/zerotier-one/{*.public,*.secret} $pst
  5. Add a directory named networks.d and create an empty file named <network id>.conf inside

    read -r -p "Enter your network ID: " NETWORK_ID
    kvmd-pstrun -- mkdir -p $pst/networks.d
    kvmd-pstrun -- touch $pst/networks.d/$NETWORK_ID.conf

    If you need to set additional settings for your network (e.g. DNS), create another file named <network id>.local.conf in the same directory with your settings.:

    echo "allowDNS=1" >> $pst/networks.d/$NETWORK_ID.local.conf
  6. Delete /var/lib/zerotier-one then add it to /etc/fstab

    rm -r /var/lib/zerotier-one
    echo "tmpfs /var/lib/zerotier-one  tmpfs  mode=0755  0  0" >> /etc/fstab
  7. Edit the zerotier-one.service to copy the persistent store and mount the in-memory volume

    systemctl edit zerotier-one.zervice
    [Unit]
    # Creates a systemd dependency for the tmpfs mount
    Requires=var-lib-zerotier\x2done.mount
    ConditionPathIsReadWrite=/var/lib/zerotier-one
    
    [Service]
    # Cleans zerotier-one runtime directory before start
    ExecStartPre=-/usr/bin/find /var/lib/zerotier-one -mindepth 1 -delete
    # Copies secrets and identities from persistent storage to runtime directory
    ExecStartPre=/usr/bin/cp -a /var/lib/kvmd/pst/data/zerotier-one /var/lib/
  8. Make the system read-only again

    ro
  9. Start the ZeroTier service and verify your device is connected to your network

    systemctl start zerotier-one.service
    zerotier-cli listnetworks

    Running systemctl status zerotier-one.service you should see the new override.conf steps and some successful logs:

    * zerotier-one.service - ZeroTier One
       Loaded: loaded (/etc/systemd/system/zerotier-one.service; enabled; preset: disabled)
      Drop-In: /etc/systemd/system/zerotier-one.service.d
               `-override.conf
       Active: active (running) since Tue 2024-03-26 13:31:41 EDT; 2min ago
      Process: 10378 ExecStartPre=/usr/bin/find /var/lib/zerotier-one -mindepth 1 -delete (code=exited, status=0/SUCCESS)
      Process: 10380 ExecStartPre=/usr/bin/cp -a /var/lib/kvmd/pst/data/zerotier-one /var/lib/ (code=exited, status=0/SUCCESS)
     Main PID: 10406 (zerotier-one)
        Tasks: 25 (limit: 4015)
          CPU: 5.119s
       CGroup: /system.slice/zerotier-one.service
               `-10406 /usr/bin/zerotier-one
    
    Mar 26 13:31:41 pikvm systemd[1]: Starting ZeroTier One...
    Mar 26 13:31:41 pikvm systemd[1]: Started ZeroTier One.
    Mar 26 13:31:41 pikvm zerotier-one[10406]: Starting Control Plane...
    Mar 26 13:31:41 pikvm zerotier-one[10406]: Starting V6 Control Plane...
    
  10. Grab your ZeroTier IP, open it in your browser, and you should see PiKVM.

    ip a | grep zt

Sources

@lupohan44
Copy link

lupohan44 commented May 17, 2024

Thank you for this document, it really helps a lot, but there are some mistakes need to be fixed.

Step 5:

kvmd-pstrun -- cp -a /var/lib/zerotier-one/{.public,.secret} $pst

Before this command, you need to create folder first using this command kvmd-pstrun -- mkdir $pst

Step 6:

rm -r /var/lib/zerotier-one

After the folder is removed, you need to create an empty folder so it can be mounted using command mkdir /var/lib/zerotier-one

echo "tmpfs /var/lib/zerotier-one tmpfs mode=0755 0 0" >> /etc/fstab

After making tmpfs, you need to use mount -a to make it take effect or zerotier service will fail

Step 7:

systemctl edit zerotier-one.zervice

Typo, it should be zerotier-one.service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment