|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +usage() { |
| 4 | + echo "install Incus, a system container and virtual machine manager" |
| 5 | + |
| 6 | + # shellcheck disable=1004 |
| 7 | + echo ' |
| 8 | + _ |
| 9 | +(_)_ __ ___ _ _ ___ |
| 10 | +| | |_ \ / __| | | / __| |
| 11 | +| | | | | (__| |_| \__ \ |
| 12 | +|_|_| |_|\___|\__,_|___/ |
| 13 | + ' |
| 14 | +} |
| 15 | + |
| 16 | +root=${root:?"root must be set"} |
| 17 | + |
| 18 | +setup_incus_user() { |
| 19 | + msg "manage incus as a non-root user" |
| 20 | + sudo groupadd -f incus-admin |
| 21 | + sudo usermod -aG incus-admin "$USER" |
| 22 | +} |
| 23 | + |
| 24 | +initialize_incus() { |
| 25 | + if sudo incus info >/dev/null 2>&1 && |
| 26 | + sudo incus storage list --format csv 2>/dev/null | grep -q .; then |
| 27 | + msg 'incus already initialized' 'notice' |
| 28 | + return 0 |
| 29 | + fi |
| 30 | + |
| 31 | + if yes_or_no 'incus' 'initialize incus with default settings (--minimal)?'; then |
| 32 | + if ! sudo incus admin init --minimal; then |
| 33 | + msg 'failed to initialize incus' 'error' |
| 34 | + return 1 |
| 35 | + fi |
| 36 | + else |
| 37 | + msg 'skipping incus init -- run "incus admin init" manually later' 'notice' |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +setup_parham_profile() { |
| 42 | + local profile='parham' |
| 43 | + local user_data="$root/incus/ansible.yaml" |
| 44 | + |
| 45 | + if [ ! -f "$user_data" ]; then |
| 46 | + msg "cloud-init user-data not found at $user_data" 'error' |
| 47 | + return 1 |
| 48 | + fi |
| 49 | + |
| 50 | + if ! sudo incus profile show "$profile" >/dev/null 2>&1; then |
| 51 | + msg "creating incus profile '$profile'" |
| 52 | + if ! sudo incus profile create "$profile" >/dev/null; then |
| 53 | + msg "failed to create incus profile '$profile'" 'error' |
| 54 | + return 1 |
| 55 | + fi |
| 56 | + else |
| 57 | + msg "incus profile '$profile' already exists, updating" 'notice' |
| 58 | + fi |
| 59 | + |
| 60 | + msg "loading $user_data into '$profile' profile" |
| 61 | + if ! sudo cat "$user_data" | sudo incus profile set "$profile" cloud-init.user-data -; then |
| 62 | + msg "failed to set cloud-init.user-data on '$profile'" 'error' |
| 63 | + return 1 |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +main_pacman() { |
| 68 | + require_pacman incus qemu-base |
| 69 | + |
| 70 | + if ! sudo systemctl enable --now incus.socket incus.service; then |
| 71 | + msg 'failed to enable incus service' 'error' |
| 72 | + return 1 |
| 73 | + fi |
| 74 | + |
| 75 | + setup_incus_user |
| 76 | + initialize_incus || return 1 |
| 77 | + setup_parham_profile |
| 78 | +} |
| 79 | + |
| 80 | +main_apt() { |
| 81 | + require_apt incus qemu-system |
| 82 | + |
| 83 | + if ! sudo systemctl enable --now incus.socket incus.service; then |
| 84 | + msg 'failed to enable incus service' 'error' |
| 85 | + return 1 |
| 86 | + fi |
| 87 | + |
| 88 | + setup_incus_user |
| 89 | + initialize_incus || return 1 |
| 90 | + setup_parham_profile |
| 91 | +} |
| 92 | + |
| 93 | +main_brew() { |
| 94 | + msg 'Incus does not run as a host on macOS -- installing the client only.' 'notice' |
| 95 | + require_brew incus |
| 96 | +} |
| 97 | + |
| 98 | +main() { |
| 99 | + if command -v incus >/dev/null; then |
| 100 | + msg "$(incus --version)" |
| 101 | + fi |
| 102 | + |
| 103 | + msg 're-login (or "newgrp incus-admin") to pick up group membership' 'notice' |
| 104 | + msg 'launch a VM with: incus launch images:ubuntu/26.04 dev --vm -p default -p parham' 'info' |
| 105 | +} |
0 commit comments