| 1 | #!/bin/sh |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | # This logic to detect virtualization technology is inspired |
| 6 | # from systemd-detect-virt (detect_vm() in src/basic/virt.c) |
| 7 | detect_virt_dmi_entry() { |
| 8 | local entry=$1 |
| 9 | if [ ! -e "$entry" ]; then |
| 10 | return |
| 11 | fi |
| 12 | case "$(cat $entry)" in |
| 13 | KVM*) echo "kvm";; |
| 14 | QEMU*) echo "qemu";; |
| 15 | VMware*) echo "vmware";; |
| 16 | VMW*) echo "vmware";; |
| 17 | innotek*) echo "oracle";; |
| 18 | Xen*) echo "xen";; |
| 19 | Bochs*) echo "bochs";; |
| 20 | Parallels*) echo "parallels";; |
| 21 | BHYVE*) echo "bhyve";; |
| 22 | esac |
| 23 | } |
| 24 | |
| 25 | detect_virt_dmi() { |
| 26 | local path |
| 27 | local result |
| 28 | for path in /sys/class/dmi/id/product_name \ |
| 29 | /sys/class/dmi/id/sys_vendor \ |
| 30 | /sys/class/dmi/id/board_vendor \ |
| 31 | /sys/class/dmi/id/bios_vendor; do |
| 32 | result=$(detect_virt_dmi_entry $path) |
| 33 | if [ -n "$result" ]; then |
| 34 | echo "$result" |
| 35 | return |
| 36 | fi |
| 37 | done |
| 38 | } |
| 39 | |
| 40 | log() { |
| 41 | logger -t hw-detect "$@" |
| 42 | } |
| 43 | |
| 44 | mount_sys_and_proc() { |
| 45 | local to_umount="" |
| 46 | if [ ! -e /target/sys/class ]; then |
| 47 | mount -t sysfs none /target/sys |
| 48 | to_umount="/target/sys" |
| 49 | fi |
| 50 | if [ ! -e /target/proc/sys ]; then |
| 51 | mount -t proc none /target/proc |
| 52 | to_umount="$to_umount /target/proc" |
| 53 | fi |
| 54 | echo "$to_umount" |
| 55 | } |
| 56 | |
| 57 | detect_virt() { |
| 58 | # First try to use systemd-detect-virt, it knows a lot more. |
| 59 | if [ -x /target/usr/bin/systemd-detect-virt ]; then |
| 60 | to_umount=$(mount_sys_and_proc) |
| 61 | virt=$(chroot /target systemd-detect-virt || true) |
| 62 | for path in $to_umount; do |
| 63 | umount $path || true |
| 64 | done |
| 65 | log "detected virtualization '$virt' (with systemd-detect-virt)" |
| 66 | fi |
| 67 | # Otherwise, rely on DMI information |
| 68 | if [ -z "$virt" ]; then |
| 69 | virt=$(detect_virt_dmi) |
| 70 | log "detected virtualization '$virt' (with DMI data)" |
| 71 | fi |
| 72 | echo -n "$virt" |
| 73 | } |
| 74 | |
| 75 | detect_desktop() { |
| 76 | if chroot /target dpkg-query -W -f='${db:Status-Status}\n' \ |
| 77 | libwayland-server0 xserver-xorg-core task-desktop 2>/dev/null \ |
| 78 | | grep -q ^installed; then |
| 79 | return 0 |
| 80 | fi |
| 81 | return 1 |
| 82 | } |
| 83 | |
| 84 | disable_modules_loading() { |
| 85 | if [ ! -d /target/etc/modprobe.d ]; then |
| 86 | return |
| 87 | fi |
| 88 | cat >/target/etc/modprobe.d/di-hw-detect-blacklist.conf <<EOF |
| 89 | # Disable loading of all modules during initial installation |
| 90 | # (temporary change made by debian-installer $0) |
| 91 | alias * all-modules |
| 92 | blacklist all-modules |
| 93 | EOF |
| 94 | } |
| 95 | |
| 96 | enable_modules_loading() { |
| 97 | rm -f /target/etc/modprobe.d/di-hw-detect-blacklist.conf |
| 98 | |
| 99 | # Now regenerate the initrd without the blacklist that got |
| 100 | # embedded and that we don't want |
| 101 | in-target update-initramfs -u -k all || true |
| 102 | } |
| 103 | |
| 104 | |
| 105 | case "$(detect_virt)" in |
| 106 | vmware) |
| 107 | if detect_desktop; then |
| 108 | apt-install --with-recommends open-vm-tools-desktop || true |
| 109 | else |
| 110 | apt-install --with-recommends open-vm-tools || true |
| 111 | fi |
| 112 | ;; |
| 113 | oracle) |
| 114 | # XXX: Virtualbox uses DKMS, which will load modules that |
| 115 | # will break d-i's X server, so disable module loading for now |
| 116 | # see #931017 for the dkms bug report |
| 117 | disable_modules_loading |
| 118 | if detect_desktop; then |
| 119 | apt-install --with-recommends virtualbox-guest-x11 || true |
| 120 | else |
| 121 | apt-install --with-recommends virtualbox-guest-utils || true |
| 122 | fi |
| 123 | enable_modules_loading |
| 124 | ;; |
| 125 | microsoft) |
| 126 | apt-install --with-recommends hyperv-daemons || true |
| 127 | ;; |
| 128 | kvm|qemu) |
| 129 | apt-install --with-recommends qemu-guest-agent || true |
| 130 | if detect_desktop; then |
| 131 | apt-install --with-recommends spice-vdagent || true |
| 132 | fi |
| 133 | ;; |
| 134 | xen) |
| 135 | # XXX: do we need anything for Xen? |
| 136 | ;; |
| 137 | esac |
| 138 | |
| 139 | # Might have been queued early on, expecting either 0 or 1 by construction: |
| 140 | if [ -s /tmp/microcode.list ]; then |
| 141 | pkg=$(cat /tmp/microcode.list) |
| 142 | log "installing $pkg" |
| 143 | apt-install --with-recommends $pkg || true |
| 144 | fi |