aboutsummaryrefslogtreecommitdiff
path: root/lib/40-os
blob: 3adcadc92d905db7146139de3e11470cd115a85b (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/sh

set_sysctl(){
  # Set sysctl value(s) and persist them to /etc/sysctl.conf.
  # $1..$N = sysctl values (as "name=value" strings)
  while [ $# -gt 0 ]; do
    sysctl "$1"
    sed -i.bak "/^${1%%=*}=/{
h
s/=.*/=${1#*=}/
}
\${
x
/^\$/{
s//${1}/
H
}
x
}" /etc/sysctl.conf
    shift
  done
  rm -f /etc/sysctl.conf.bak
}

set_loader_conf(){
  # Set the FreeBSD bootloader options in /boot/loader.conf.
  # The host will be rebooted if the file is changed.
  # $1..$N = bootloader options (as "name=value" strings)
  [ "$BOXCONF_OS" = freebsd ] || bug 'set_loader_conf can only be used on FreeBSD'

  while [ $# -gt 0 ]; do
    grep -qxF "${1%%=*}=\"${1#*=}\"" /boot/loader.conf || BOXCONF_NEED_REBOOT=true
    sed -i.bak "/^${1%%=*}=/{
h
s/=.*/=\"${1#*=}\"/
}
\${
x
/^\$/{
s//${1%%=*}=\"${1#*=}\"/
H
}
x
}" /boot/loader.conf
    shift
  done
  rm -f /boot/loader.conf.bak
}

load_kernel_module(){
  # Ensure the given kernel modules are loaded.
  # $1..$N = module names
  case $BOXCONF_OS in
    freebsd)
      while [ $# -gt 0 ]; do
        kldstat -qn "$1" || kldload -v "$1"
        shift
      done
      ;;
    *)
      die "load_kernel_module unimplemented for ${BOXCONF_OS}"
      ;;
  esac
}

add_local_user(){
  _bcalu_homedir_mode=700
  _bcalu_create_homedir=
  _bcalu_homedir=
  _bcalu_comment=
  _bcalu_shell=/sbin/nologin
  _bcalu_pgroup=
  _bcalu_grouplist=
  _bcalu_uid=

  # Add a local user if it doesn't exist.
  while getopts c:d:G:g:mM:s:u: _bcalu_opt; do
    case $_bcalu_opt in
      c) _bcalu_comment=$OPTARG ;;
      d) _bcalu_homedir=$OPTARG ;;
      G) _bcalu_grouplist=$OPTARG ;;
      g) _bcalu_pgroup=$OPTARG ;;
      M) _bcalu_homedir_mode=$OPTARG ;;
      m) _bcalu_create_homedir=true ;;
      s) _bcalu_shell=$OPTARG ;;
      u) _bcalu_uid=$OPTARG ;;
    esac
  done
  shift $((OPTIND - 1))

  _bcalu_username=$1
  : ${_bcalu_homedir:="/home/${_bcalu_username}"}
  : ${_bcalu_comment:="${_bcalu_username} user"}

  case $BOXCONF_OS in
    freebsd)
      if pw usershow "$_bcalu_username" > /dev/null 2>&1; then
        log "local user ${_bcalu_username} already exists"
        return 0
      fi

      pw useradd \
        -n "$_bcalu_username" \
        -c "$_bcalu_comment" \
        -s "$_bcalu_shell" \
        -M "$_bcalu_homedir_mode" \
        -d "$_bcalu_homedir" \
        ${_bcalu_create_homedir:+-m} \
        ${_bcalu_grouplist:+-G ${_bcalu_grouplist}} \
        ${_bcalu_pgroup:+-g ${_bcalu_pgroup}} \
        ${_bcalu_uid:+-u ${_bcalu_uid}}

        log "added local user ${_bcalu_username}"
      ;;
    *)
      die "add_local_user unimplemented for ${BOXCONF_OS}"
      ;;
  esac
}