summarylogtreecommitdiffstats
path: root/netinstall.sh
blob: 033a87a382ee4143ebfb4b83995fb6c2412bb55a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash

set -e

INTERFACE=
PACKAGES=()
OPTIONS=()

function help() {
  echo "usage: ${0} [OPTIONS]"
  echo
  echo "where OPTIONS are:"
  echo " -h            show this help"
  echo " -i INTERFACE  install via INTERFACE"
  echo " -k KEYFILE    use KEYFILE"
  echo " -p PACKAGE    install PACKAGE"
  echo " -r            reset configuration"
  echo " -s SCRIPT     run script"
}

while getopts "hi:k:p:rs:" opt; do
  case ${opt} in
    h)
      help
      exit 0
      ;;
    i)
      if ! ip addr show dev "${OPTARG}" >/dev/null 2>/dev/null; then
        echo "Interface '${OPTARG}' does not exist." >&2
        exit 1
      fi
      INTERFACE="${OPTARG}"
      ;;
    k)
      OPTIONS+=(-k "${OPTARG}")
      ;;
    p)
      if [ ! -s "${OPTARG}" ]; then
        echo "Package '${OPTARG}' does not exist." >&2
        exit 1
      fi
      PACKAGES+=("${OPTARG}")
      ;;
    r)
      OPTIONS+=(-r)
      ;;
    s)
      OPTIONS+=(-s "${OPTARG}")
      ;;
  esac
done
      
if [ -z "${PACKAGES}" ]; then
  echo "No package given for installation." >&2
  exit 1
fi

if [ -z "${INTERFACE}" ]; then
  echo "No interface given for installation." >&2
  exit 1
fi

if [ "${UID}" -gt 0 ]; then
  exec sudo -p "This must be run as root. Please enter the password for %u to continue: " -- /bin/bash -- "${0}" $@
fi

ip netns add netinstall
trap 'ip netns delete netinstall' EXIT INT QUIT TERM HUP

ip link set dev "${INTERFACE}" netns netinstall
ip netns exec netinstall ip addr add 192.168.88.2/24 dev "${INTERFACE}"
ip netns exec netinstall ip link set dev "${INTERFACE}" up
ip netns exec netinstall ip route add default dev "${INTERFACE}"
ip netns exec netinstall /usr/lib/netinstall/netinstall-cli "${OPTIONS[@]}" -a 192.168.88.1 "${PACKAGES[@]}"