summaryrefslogtreecommitdiff
path: root/libre/virtualbox-libre/vboxbuild
blob: 27519950f1a8192618500f4d17d0339cf454093d (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash

. /etc/vbox/vbox.cfg

MODLIST=()
LOG="/var/log/vbox-install.log"
>| "$LOG"

stat_busy() {
  printf '==> %s ... ' "$1"
}

stat_done() {
  echo 'done'
}

stat_fail() {
  echo 'failed'
}

if [[ $INSTALL_DIR ]]; then
  VBOXMANAGE=$INSTALL_DIR/VBoxManage
  BUILDVBOXDRV=$INSTALL_DIR/src/vboxdrv/build_in_tmp
  BUILDVBOXNETFLT=$INSTALL_DIR/src/vboxnetflt/build_in_tmp
  BUILDVBOXNETADP=$INSTALL_DIR/src/vboxnetadp/build_in_tmp
  BUILDVBOXPCI=$INSTALL_DIR/src/vboxpci/build_in_tmp
else
  echo "error: missing vbox.cfg"
  exit 1
fi

if [[ -f /proc/modules ]]; then
  IFS=$'\n' read -r -d '' -a MODLIST < \
    <(grep -oE '^vbox(pci|netflt|netadp|drv)' /proc/modules)
fi

if (( ${#MODLIST[*]} )); then
  stat_busy "Unloading VirtualBox kernel libre modules"
  modprobe -ar "${MODLIST[@]}" && stat_done || stat_fail
fi

for kdir in /usr/lib/modules/[2-3]*; do
  if [[ ! -d $kdir/kernel ]]; then
    # found a stale kernel
    mods=("$kdir/extramodules"{drv,netadp,netflt,pci}.ko*)
    if (( ${#mods[@]} )); then
      stat_busy "Removing all old VirtualBox kernel libre modules"
      if rm -f "${mods[@]}" &&
          rmdir -p --ignore-fail-on-non-empty "$kdir/extramodules" 2>/dev/null; then
        stat_done
      else
        stat_fail
      fi
    fi
  fi
done

# default to the currently running kernel
if (( ! $# )); then
  set -- $(uname -r)
fi

for kernver; do
  export KERN_DIR=/usr/lib/modules/$kernver/build
  export MODULE_DIR=/usr/lib/modules/$kernver/extramodules
  if [[ ! -d $KERN_DIR ]]; then
    printf "error: \`%s' does not appear to be a valid kernel libre build directory.\n" \
        "$KERN_DIR"
    continue
  fi

  stat_busy "Recompiling VirtualBox kernel libre modules ($kernver)"
    if ! $BUILDVBOXDRV \
      --save-module-symvers /tmp/vboxdrv-Module.symvers \
      --no-print-directory install >> $LOG 2>&1; then
      echo  "Look at $LOG to find out what went wrong"
      stat_fail
    fi
    for build in BUILDVBOX{NETFLT,NETADP,PCI}; do
      if ! ${!build} \
        --use-module-symvers /tmp/vboxdrv-Module.symvers \
        --no-print-directory install >> $LOG 2>&1; then
        echo  "Look at $LOG to find out what went wrong"
        stat_fail
      fi
    done
    gzip -f9 "$MODULE_DIR"/*.ko
    depmod "$kernver"
  stat_done
done

if (( ${#MODLIST[*]} )); then
  stat_busy "Reloading VirtualBox kernel libre modules"
  modprobe -a "${MODLIST[@]}" && stat_done || stat_fail
fi