|
| 1 | +#!/bin/bash |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" |
| 20 | +CMDLINE=/var/cache/cloud/cmdline |
| 21 | + |
| 22 | +. /lib/lsb/init-functions |
| 23 | + |
| 24 | +log_it() { |
| 25 | + echo "$(date) $@" >> /var/log/cloud.log |
| 26 | + log_action_msg "$@" |
| 27 | +} |
| 28 | + |
| 29 | +hypervisor() { |
| 30 | + if [ -d /proc/xen ]; then |
| 31 | + mount -t xenfs none /proc/xen |
| 32 | + $(dmesg | grep -q "Xen HVM") |
| 33 | + if [ $? -eq 0 ]; then # 1=PV,0=HVM |
| 34 | + echo "xen-hvm" && return 0 |
| 35 | + else |
| 36 | + echo "xen-pv" && return 0 |
| 37 | + fi |
| 38 | + fi |
| 39 | + |
| 40 | + [ -x /usr/sbin/virt-what ] && local facts=( $(virt-what) ) |
| 41 | + if [ "$facts" != "" ]; then |
| 42 | + # Xen HVM is recognized as Hyperv when Viridian extensions are enabled |
| 43 | + if [ "${facts[-1]}" == "xen-domU" ] && [ "${facts[0]}" == "hyperv" ]; then |
| 44 | + echo "xen-hvm" && return 0 |
| 45 | + else |
| 46 | + echo ${facts[-1]} && return 0 |
| 47 | + fi |
| 48 | + fi |
| 49 | + |
| 50 | + grep -q QEMU /proc/cpuinfo && echo "kvm" && return 0 |
| 51 | + grep -q QEMU /var/log/messages && echo "kvm" && return 0 |
| 52 | + |
| 53 | + vmware-checkvm &> /dev/null && echo "vmware" && return 0 |
| 54 | + |
| 55 | + echo "unknown" && return 1 |
| 56 | +} |
| 57 | + |
| 58 | +config_guest() { |
| 59 | + if [ "$HYPERVISOR" == "kvm" ] |
| 60 | + then |
| 61 | + # Configure hot-plug |
| 62 | + modprobe acpiphp || true |
| 63 | + modprobe pci_hotplug || true |
| 64 | + sed -i -e "/^s0:2345:respawn.*/d" /etc/inittab |
| 65 | + sed -i -e "/6:23:respawn/a\s0:2345:respawn:/sbin/getty -L 115200 ttyS0 vt102" /etc/inittab |
| 66 | + fi |
| 67 | + [ ! -d /proc/xen ] && sed -i 's/^vc/#vc/' /etc/inittab && telinit q |
| 68 | + [ -d /proc/xen ] && sed -i 's/^#vc/vc/' /etc/inittab && telinit q |
| 69 | +} |
| 70 | + |
| 71 | +get_boot_params() { |
| 72 | + case $HYPERVISOR in |
| 73 | + xen-pv|xen-domU) |
| 74 | + cat /proc/cmdline > $CMDLINE |
| 75 | + sed -i "s/%/ /g" $CMDLINE |
| 76 | + ;; |
| 77 | + xen-hvm) |
| 78 | + if [ ! -f /usr/bin/xenstore-read ]; then |
| 79 | + log_it "ERROR: xentools not installed, cannot found xenstore-read" && exit 5 |
| 80 | + fi |
| 81 | + /usr/bin/xenstore-read vm-data/cloudstack/init > $CMDLINE |
| 82 | + sed -i "s/%/ /g" $CMDLINE |
| 83 | + ;; |
| 84 | + kvm) |
| 85 | + systemctl enable --now qemu-guest-agent |
| 86 | + # Wait for $CMDLINE file to be written by the qemu-guest-agent |
| 87 | + for i in {1..60}; do |
| 88 | + if [ -s $CMDLINE ]; then |
| 89 | + log_it "Received a new non-empty cmdline file from qemu-guest-agent" |
| 90 | + break |
| 91 | + fi |
| 92 | + sleep 1 |
| 93 | + done |
| 94 | + if [ ! -s $CMDLINE ]; then |
| 95 | + log_it "Failed to receive the cmdline file via the qemu-guest-agent" |
| 96 | + fi |
| 97 | + ;; |
| 98 | + vmware) |
| 99 | + vmtoolsd --cmd 'machine.id.get' > $CMDLINE |
| 100 | + ;; |
| 101 | + virtualpc|hyperv) |
| 102 | + # Hyper-V is recognized as virtualpc hypervisor type. Boot args are passed using KVP Daemon |
| 103 | + #waiting for the hv_kvp_daemon to start up |
| 104 | + #sleep need to fix the race condition of hv_kvp_daemon and cloud-early-config |
| 105 | + [ -f /usr/sbin/hv_kvp_daemon ] && /usr/sbin/hv_kvp_daemon |
| 106 | + sleep 5 |
| 107 | + cp -f /var/opt/hyperv/.kvp_pool_0 $CMDLINE |
| 108 | + cat /dev/null > /var/opt/hyperv/.kvp_pool_0 |
| 109 | + ;; |
| 110 | + virtualbox) |
| 111 | + # Virtualbox is used to test the virtual router |
| 112 | + # get the commandline from a dmistring (yes, hacky!) |
| 113 | + dmidecode | grep cmdline | sed 's/^.*cmdline://' > $CMDLINE |
| 114 | + RV=$? |
| 115 | + if [ $RV -ne 0 ] ; then |
| 116 | + log_it "Failed to get cmdline from a virtualbox dmi property" |
| 117 | + fi |
| 118 | + ;; |
| 119 | + esac |
| 120 | +} |
| 121 | + |
| 122 | +get_systemvm_type() { |
| 123 | + export TYPE=$(grep -Po 'type=\K[a-zA-Z]*' $CMDLINE) |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +patch_systemvm() { |
| 128 | + local patchfile=$1 |
| 129 | + local backupfolder="/tmp/.conf.backup" |
| 130 | + local logfile="/var/log/patchsystemvm.log" |
| 131 | + if [ -f /usr/local/cloud/systemvm/conf/cloud.jks ]; then |
| 132 | + rm -fr $backupfolder |
| 133 | + mkdir -p $backupfolder |
| 134 | + cp -r /usr/local/cloud/systemvm/conf/* $backupfolder/ |
| 135 | + fi |
| 136 | + rm /usr/local/cloud/systemvm -rf |
| 137 | + mkdir -p /usr/local/cloud/systemvm |
| 138 | + echo "All" | unzip $patchfile -d /usr/local/cloud/systemvm >$logfile 2>&1 |
| 139 | + find /usr/local/cloud/systemvm/ -name \*.sh | xargs chmod 555 |
| 140 | + if [ -f $backupfolder/cloud.jks ]; then |
| 141 | + cp -r $backupfolder/* /usr/local/cloud/systemvm/conf/ |
| 142 | + echo "Restored keystore file and certs using backup" >> $logfile |
| 143 | + fi |
| 144 | + rm -fr $backupfolder |
| 145 | + # Import global cacerts into 'cloud' service's keystore |
| 146 | + keytool -importkeystore -srckeystore /etc/ssl/certs/java/cacerts -destkeystore /usr/local/cloud/systemvm/certs/realhostip.keystore -srcstorepass changeit -deststorepass vmops.com -noprompt || true |
| 147 | + return 0 |
| 148 | +} |
| 149 | + |
| 150 | +patch() { |
| 151 | + local PATCH_MOUNT=/media/cdrom |
| 152 | + local logfile="/var/log/patchsystemvm.log" |
| 153 | + if [ "$TYPE" == "consoleproxy" ] || [ "$TYPE" == "secstorage" ] && [ -f ${PATCH_MOUNT}/agent.zip ] && [ -f /var/cache/cloud/patch.required ] |
| 154 | + then |
| 155 | + echo "Patching systemvm for cloud service with mount=$PATCH_MOUNT for type=$TYPE" >> $logfile |
| 156 | + patch_systemvm ${PATCH_MOUNT}/agent.zip |
| 157 | + if [ $? -gt 0 ] |
| 158 | + then |
| 159 | + echo "Failed to apply patch systemvm\n" >> $logfile |
| 160 | + exit 1 |
| 161 | + fi |
| 162 | + fi |
| 163 | + |
| 164 | + rm -f /var/cache/cloud/patch.required |
| 165 | + chmod -x /etc/systemd/system/cloud*.service |
| 166 | + systemctl daemon-reload |
| 167 | + umount $PATCH_MOUNT || true |
| 168 | + |
| 169 | + if [ -f /mnt/cmdline ]; then |
| 170 | + cat /mnt/cmdline > $CMDLINE |
| 171 | + fi |
| 172 | + return 0 |
| 173 | +} |
| 174 | + |
| 175 | +bootstrap() { |
| 176 | + log_it "Bootstrapping systemvm appliance" |
| 177 | + |
| 178 | + export HYPERVISOR=$(hypervisor) |
| 179 | + [ $? -ne 0 ] && log_it "Failed to detect hypervisor type, bailing out of early init" && exit 10 |
| 180 | + log_it "Detected that we are running inside $HYPERVISOR" |
| 181 | + |
| 182 | + config_guest |
| 183 | + get_boot_params |
| 184 | + get_systemvm_type |
| 185 | + patch |
| 186 | + sync |
| 187 | + sysctl -p |
| 188 | + |
| 189 | + log_it "Configuring systemvm type=$TYPE" |
| 190 | + if [ -f "/opt/cloud/bin/setup/$TYPE.sh" ]; then |
| 191 | + /opt/cloud/bin/setup/$TYPE.sh |
| 192 | + else |
| 193 | + /opt/cloud/bin/setup/default.sh |
| 194 | + fi |
| 195 | + log_it "Finished setting up systemvm" |
| 196 | + exit 0 |
| 197 | +} |
| 198 | + |
| 199 | +bootstrap |
0 commit comments