From 091b713e6200b835964aed16e2e5731a51ab8a14 Mon Sep 17 00:00:00 2001 From: David Worms Date: Tue, 19 Mar 2024 14:02:57 +0100 Subject: [PATCH] feat(incus): multipass shell commands --- packages/incus/assets/multipass.sh | 200 +++++++++++++++++++---------- 1 file changed, 129 insertions(+), 71 deletions(-) diff --git a/packages/incus/assets/multipass.sh b/packages/incus/assets/multipass.sh index 9ac2d88c2..8a8b9d5b7 100755 --- a/packages/incus/assets/multipass.sh +++ b/packages/incus/assets/multipass.sh @@ -1,63 +1,55 @@ #/bin/bash set -e -# Logs: `ll /Library/Logs/Multipass` - -brew list | grep -x "bash-completion" || brew install bash-completion -brew list | grep -x "multipass" || brew install --cask multipass -brew list | grep -x "incus" || brew install incus - -if multipass info nikita ; then - multipass start nikita - exit 0 -fi - -# Host creation -multipass launch \ - --name nikita \ - --cpus 8 \ - --memory 30G \ - --disk 100G - -# Error: no DNS resolution -# Solution: -# In case you need a custom DNS -# multipass exec nikita -- sudo bash -c "echo 'DNS=8.8.8.8' >> /etc/systemd/resolved.conf" -# multipass exec nikita -- sudo systemctl restart systemd-resolved - -# Error: `NikitaError: Error: Failed add validation for device "nikitadir": Missing source path "/Users/david/projects/github/nikita/" for disk "nikitadir"` -# Solution: -# Re-run `multipass mount $HOME nikita` - -# Multipass does not respond -# Solution: -# restart multipass -# sudo launchctl stop com.canonical.multipassd -# sudo launchctl start com.canonical.multipassd - -# MacOS crash and image cant start: -# see https://github.com/canonical/multipass/issues/1924 -# Force kill multipass, it shall restart: -# sudo pkill multipassd -# multipass start nikita - -# Fix DNS -multipass exec nikita -- sudo bash -c "echo 'DNS=8.8.8.8' >> /etc/systemd/resolved.conf" -multipass exec nikita -- sudo systemctl restart systemd-resolved -# OS preparation installation -multipass exec nikita -- sudo apt update -multipass exec nikita -- sudo apt upgrade -y -multipass exec nikita -- sudo sh -c "echo 'fs.protected_regular = 0' >> /etc/sysctl.conf && sysctl -p" -# File mount -multipass exec nikita -- sudo snap install multipass-sshfs -# Only pass the target machine name, if the mount point is omitted, -# it is the same as the source's absolute path -multipass mount $HOME nikita -# Incus installation -multipass exec nikita -- curl -fsSL https://pkgs.zabbly.com/key.asc | gpg --show-keys --fingerprint -multipass exec nikita -- sudo mkdir -p /etc/apt/keyrings/ -multipass exec nikita -- sudo curl -fsSL https://pkgs.zabbly.com/key.asc -o /etc/apt/keyrings/zabbly.asc -multipass exec nikita -- sudo sh -c 'cat < /etc/apt/sources.list.d/zabbly-incus-stable.sources + + +help(){ + echo """ +Usage: + ./multipass.sh + +Available Commands: + install + reload + help + +""" +} + +install(){ + # Logs: `ll /Library/Logs/Multipass` + brew list | grep -x "bash-completion" || brew install bash-completion + brew list | grep -x "multipass" || brew install --cask multipass + brew list | grep -x "incus" || brew install incus + if multipass info nikita ; then + multipass start nikita + else + # Host creation + multipass launch \ + --name nikita \ + --cpus 8 \ + --memory 30G \ + --disk 100G + fi + # Fix DNS + multipass exec nikita -- sudo bash -c "echo 'DNS=8.8.8.8' >> /etc/systemd/resolved.conf" + multipass exec nikita -- sudo systemctl restart systemd-resolved + # OS preparation installation + multipass exec nikita -- sudo apt update + multipass exec nikita -- sudo DEBIAN_FRONTEND=noninteractive apt upgrade -y + multipass exec nikita -- sudo sh -c "echo 'fs.protected_regular = 0' >> /etc/sysctl.conf && sysctl -p" + # File mount + multipass exec nikita -- sudo snap install multipass-sshfs + # Only pass the target machine name, if the mount point is omitted, + # it is the same as the source's absolute path + if ! multipass info nikita --format json | jq -re '.info.nikita.mounts."/Users/david"'; then + multipass mount $HOME nikita + fi + # Incus installation + multipass exec nikita -- curl -fsSL https://pkgs.zabbly.com/key.asc | gpg --show-keys --fingerprint + multipass exec nikita -- sudo mkdir -p /etc/apt/keyrings/ + multipass exec nikita -- sudo curl -fsSL https://pkgs.zabbly.com/key.asc -o /etc/apt/keyrings/zabbly.asc + multipass exec nikita -- sudo sh -c 'cat < /etc/apt/sources.list.d/zabbly-incus-stable.sources Enabled: yes Types: deb URIs: https://pkgs.zabbly.com/incus/stable @@ -67,13 +59,13 @@ Architectures: $(dpkg --print-architecture) Signed-By: /etc/apt/keyrings/zabbly.asc EOF' -multipass exec nikita -- sudo apt update -multipass exec nikita -- sudo apt install -y incus -multipass exec nikita -- sudo apt install -y zfsutils-linux -multipass exec nikita -- sudo truncate -s 100G /opt/zfs.img -multipass exec nikita -- sudo zpool create incus /opt/zfs.img -# Initialisation -multipass exec nikita -- sudo sh -c 'cat <> /etc/systemd/resolved.conf" +multipass exec nikita -- sudo systemctl restart systemd-resolved + +Error: `NikitaError: Error: Failed add validation for device "nikitadir": Missing source path "/Users/david/projects/github/nikita/" for disk "nikitadir"` +Solution: +Re-run `multipass mount $HOME nikita` + +Multipass does not respond +Solution: +restart multipass +sudo launchctl stop com.canonical.multipassd +sudo launchctl start com.canonical.multipassd + +MacOS crash and image cant start: +see https://github.com/canonical/multipass/issues/1924 +Force kill multipass, it shall restart: +sudo pkill multipassd +multipass start nikita +""" +} -if incus remote list --format json | jq -re .nikita; then - incus remote switch local - incus remote remove nikita -fi -certificate=`multipass exec nikita -- sudo incus config trust add nikita --quiet` -incus remote add nikita $certificate -incus remote switch nikita +case "$1" in + remove) remove + ;; + install) install + ;; + reload) reload + ;; + info) info + ;; + *) help + ;; +esac