1#! /bin/sh 2set -e 3 4# grub-mkconfig helper script. 5# Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc. 6# 7# GRUB is free software: you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation, either version 3 of the License, or 10# (at your option) any later version. 11# 12# GRUB is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with GRUB. If not, see <http://www.gnu.org/licenses/>. 19 20prefix="/usr" 21exec_prefix="/usr" 22datarootdir="/usr/share" 23 24. "$pkgdatadir/grub-mkconfig_lib" 25 26CLASS="--class gnu-linux --class gnu --class os --class acrn" 27 28# read ACRN config (board/scenario) from debconf 29ACRN_BOARD=$(echo "get acrn-hypervisor/board" | debconf-communicate || true) 30if [ "$(echo "${ACRN_BOARD}" | awk '{print $1}')" != "0" ]; then 31 gettext_printf "ACRN: Cannot access debconf acrn-hypervisor/board: %s" "${ACRN_BOARD}\n" >&2 32 exit 0 33fi 34ACRN_BOARD="$(echo "${ACRN_BOARD}" | awk '{print $2}')" 35 36ACRN_SCENARIO=$(echo "get acrn-hypervisor/scenario" | debconf-communicate || true) 37if [ "$(echo "${ACRN_SCENARIO}" | awk '{print $1}')" != "0" ]; then 38 gettext_printf "ACRN: Cannot access debconf acrn-hypervisor/:scenario %s" "${ACRN_SCENARIO}\n" >&2 39 exit 0 40fi 41ACRN_SCENARIO="$(echo "${ACRN_SCENARIO}" | awk '{print $2}')" 42 43# we obtain any data from scenario config 44ACRN_SCENARIO_FILE=/usr/lib/x86_64-linux-gnu/acrn/${ACRN_BOARD}/${ACRN_SCENARIO}/scenario.xml 45if [ ! -f ${ACRN_SCENARIO_FILE} ]; then 46 gettext_printf "ACRN: Missing scenario config %s\n" "${ACRN_SCENARIO_FILE}" >&2 47 exit 0 48fi 49 50# get list of vm ids from scenario config 51ACRN_VM_IDS=$(xmllint --xpath '//vm/@id' ${ACRN_SCENARIO_FILE} 2>/dev/null | sed 's/\s*id="\([^"]*\)"/\1/g') 52if [ -z "${ACRN_VM_IDS}" ]; then 53 gettext_printf "ACRN: No VMs defined in scenario config %s" "${ACRN_SCENARIO_FILE}\n" >&2 54 exit 0 55fi 56 57# get number of configured pre-launched VMs 58ACRN_PRE_LAUNCHED_VM_COUNT=$(xmllint --xpath "count(//vm[load_order=\"PRE_LAUNCHED_VM\"])" ${ACRN_SCENARIO_FILE}) 59 60for id in ${ACRN_VM_IDS}; do 61 # get grub relevant data from scenario configuration (ugly handling of pseudo array in conventional shell, sigh!) 62 eval ACRN_LOAD_ORDER_VM${id}=$(xmllint --xpath "//vm[@id=\"${id}\"]/load_order/text()" ${ACRN_SCENARIO_FILE} 2>/dev/null || true) 63 eval ACRN_NAME_VM${id}=$(xmllint --xpath "//vm[@id=\"${id}\"]/name/text()" ${ACRN_SCENARIO_FILE} 2>/dev/null || true) 64 eval ACRN_KERN_MOD_VM${id}=$(xmllint --xpath "//vm[@id=\"${id}\"]/os_config/kern_mod/text()" ${ACRN_SCENARIO_FILE} 2>/dev/null || true) 65 eval ACRN_RAMDISK_MOD_VM${id}=$(xmllint --xpath "//vm[@id=\"${id}\"]/os_config/ramdisk_mod/text()" ${ACRN_SCENARIO_FILE} 2>/dev/null || true) 66 eval ACRN_ACPI_MOD_VM${id}=$(xmllint --xpath "//vm[@id=\"${id}\"]/os_config/acpi_mod_tag/text()" ${ACRN_SCENARIO_FILE} 2>/dev/null || true) 67 68 # eventually store Service VM id 69 [ "$(eval echo \${ACRN_LOAD_ORDER_VM${id}})" = "SERVICE_VM" ] && ACRN_SERVICE_VM_ID=${id} 70 71 if [ "$(eval echo \${ACRN_LOAD_ORDER_VM${id}})" = "PRE_LAUNCHED_VM" ]; then 72 # pre-launched VMs always need ACPI data, eventually set acpi_mod_tag to default 73 if [ -z "$(eval echo \${ACRN_ACPI_MOD_VM${id}})" ]; then 74 eval ACRN_ACPI_MOD_VM${id}="ACPI_VM${id}" 75 eval GRUB_ACRN_MOD_ACPI_VM${id}="/boot/ACPI_VM${id}.bin" 76 fi 77 fi 78done 79 80# get performance policy parameter from scenario configuration 81ACRN_CPU_PERF_POLICY=$(xmllint --xpath '//CPU_PERFORMANCE_POLICY/text()' ${ACRN_SCENARIO_FILE} 2>/dev/null || true) 82if [ -z "${ACRN_CPU_PERF_POLICY}" ]; then 83 ACRN_CPU_PERF_POLICY=Performance 84fi 85GRUB_CMDLINE_ACRN="cpu_perf_policy=${ACRN_CPU_PERF_POLICY} ${GRUB_CMDLINE_ACRN}" 86 87if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then 88 OS=GNU/Linux 89else 90 case ${GRUB_DISTRIBUTOR} in 91 Ubuntu|Kubuntu) 92 OS="${GRUB_DISTRIBUTOR}" 93 ;; 94 *) 95 OS="${GRUB_DISTRIBUTOR} GNU/Linux" 96 ;; 97 esac 98 CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1|LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}" 99fi 100 101# loop-AES arranges things so that /dev/loop/X can be our root device, but 102# the initrds that Linux uses don't like that. 103case ${GRUB_DEVICE} in 104 /dev/loop/*|/dev/loop[0-9]) 105 GRUB_DEVICE=$(losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/") 106 # We can't cope with devices loop-mounted from files here. 107 case ${GRUB_DEVICE} in 108 /dev/*) ;; 109 *) exit 0 ;; 110 esac 111 ;; 112esac 113 114# btrfs may reside on multiple devices. We cannot pass them as value of root= parameter 115# and mounting btrfs requires user space scanning, so force UUID in this case. 116if [ "x${GRUB_DEVICE_UUID}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \ 117 || ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \ 118 || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then 119 LINUX_ROOT_DEVICE=${GRUB_DEVICE} 120else 121 LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID} 122fi 123 124case x"$GRUB_FS" in 125 xbtrfs) 126 rootsubvol="$(make_system_path_relative_to_its_root /)" 127 rootsubvol="${rootsubvol#/}" 128 if [ "x${rootsubvol}" != x ]; then 129 GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}" 130 fi;; 131 xzfs) 132 rpool=$(${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true) 133 bootfs="$(make_system_path_relative_to_its_root / | sed -e "s,@$,,")" 134 LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs%/}" 135 ;; 136esac 137 138# add_mod_tag: Add entry for a module tag 139# add_mod_tag <tagtype> <tagname> <vmid> 140# tagtype: Type of tag: kernel, ramdisk, acpi 141# tagname: Name of the tag (from config) 142# vmid: ID of the repsective VM 143add_mod_tag() 144{ 145 local tagtype=$1 146 local tagname=$2 147 local vmid=$3 148 149 # silently skip empty tagnames 150 if [ -z "${tagname}" ]; then 151 return 152 fi 153 154 local tagvalue="$(eval echo \${GRUB_ACRN_MOD_${tagname}})" 155 if [ -n "${tagvalue}" ]; then 156 local message="$(gettext_printf "Loading ACRN %s %s for %s" "VM${vmid}" "${tagtype}" "$(eval echo \${ACRN_NAME_VM${vmid}})")" 157 local basename=$(basename ${tagvalue}) 158 local dirname=$(dirname ${tagvalue}) 159 local rel_dirname=$(make_system_path_relative_to_its_root $dirname) 160 local modparams 161 local loaderparams 162 163 # kernel might add a command line 164 [ "${tagtype}" = "kernel" ] && modparams="$(eval echo \${GRUB_ACRN_MOD_CMDLINE_${tagname}})" 165 # ramdisk will not be unzipped 166 [ "${tagtype}" = "ramdisk" ] && loaderparams="--nounzip" 167 cat << EOF 168 echo '$(echo "$message" | grub_quote)' 169 ${module_loader} ${loaderparams} ${rel_dirname}/${basename} ${tagname} ${modparams} 170EOF 171 else 172 gettext_printf "ACRN: GRUB_ACRN_MOD_${tagname} not set, skipping\n" >&2 173 fi 174} 175 176 177add_prelaunched_vms() 178{ 179 # eventually also load data for pre-launched VMs 180 for id in ${ACRN_VM_IDS}; do 181 # we just care for pre-launched VMs 182 [ "$(eval echo \${ACRN_LOAD_ORDER_VM${id}})" != "PRE_LAUNCHED_VM" ] && continue 183 # start with kernel mod tag 184 tagname="$(eval echo \${ACRN_KERN_MOD_VM${id}})" 185 if [ -z "${tagname}" ]; then 186 gettext_printf "ACRN: No kernel module tag set for %s(%s), skipping\n" "$(eval echo \${ACRN_NAME_VM${id}})" "VM${id}" >&2 187 continue 188 fi 189 if [ -z "$(eval echo \${GRUB_ACRN_MOD_${tagname}})" ]; then 190 gettext_printf "ACRN: GRUB_ACRN_MOD_${tagname} not set, skipping %s(%s)\n" "$(eval echo \${ACRN_NAME_VM${id}})" "VM${id}" >&2 191 continue 192 fi 193 gettext_printf "Found ACRN pre-launched %s %s: %s\n" "VM${id}" "$(eval echo \${ACRN_NAME_VM${id}})" "$(eval echo \${GRUB_ACRN_MOD_${tagname}})">&2 194 add_mod_tag kernel ${tagname} ${id} 195 add_mod_tag acpi "$(eval echo \${ACRN_ACPI_MOD_VM${id}})" ${id} 196 add_mod_tag ramdisk "$(eval echo \${ACRN_RAMDISK_MOD_VM${id}})" ${id} 197 done 198} 199 200linux_entry () 201{ 202 os="$1" 203 version="$2" 204 acrn_version="$3" 205 args="$4" 206 acrn_args="$5" 207 if [ -z "$boot_device_id" ]; then 208 boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")" 209 fi 210 title="$(gettext_printf "%s with ACRN hypervisor" "${os}")" 211 echo "menuentry '$(echo "$title, with Linux ${version} (ACRN ${acrn_version})" | grub_quote)' ${CLASS} \$menuentry_id_option 'acrn-gnulinux-$boot_device_id' {" 212 213 if [ -z "${prepare_boot_cache}" ]; then 214 prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)" 215 fi 216 printf '%s\n' "${prepare_boot_cache}" 217 message="$(gettext_printf "Loading ACRN hypervisor %s ..." ${acrn_version})" 218 cat << EOF 219 echo '$(echo "$message" | grub_quote)' 220 ${acrn_loader} ${rel_acrn_dirname}/${acrn_basename} ${acrn_args} root=${linux_root_device_thisversion} ro ${args} 221EOF 222 message="$(gettext_printf "Loading ACRN Service VM Linux kernel %s ..." ${version})" 223 ktagname="$(eval echo \${ACRN_KERN_MOD_VM${ACRN_SERVICE_VM_ID}})" 224 cat << EOF 225 echo '$(echo "$message" | grub_quote)' 226 ${module_loader} ${rel_dirname}/${basename} ${ktagname} $(eval echo \${GRUB_ACRN_MOD_CMDLINE_${ktagname}}) 227EOF 228 if test -n "${initrd}"; then 229 # TRANSLATORS: ramdisk isn't identifier. Should be translated. 230 message="$(gettext_printf "Loading ACRN Service VM initial ramdisk ...")" 231 rtagname="$(eval echo \${ACRN_RAMDISK_MOD_VM${ACRN_SERVICE_VM_ID}})" 232 cat << EOF 233 echo '$(echo "$message" | grub_quote)' 234 ${module_loader} --nounzip ${rel_dirname}/${initrd} ${rtagname} 235EOF 236 fi 237 add_prelaunched_vms 238 cat << EOF 239} 240EOF 241} 242 243machine=$(uname -m) 244# ACRN only on x86_64 machines 245if [ "x${machine}" != "xx86_64" ]; then 246 exit 0 247fi 248GENKERNEL_ARCH="x86" 249 250# ServiceVM: Gather all Linux images with ACRN support, i.e. CONFIG_ACRN_HSM is set 251linux_list= 252for i in /boot/vmlinu[xz]-* /vmlinu[xz]-* /boot/kernel-*; do 253 if grub_file_is_not_garbage "$i"; then 254 basename=$(basename $i) 255 version=$(echo $basename | sed -e "s,^[^0-9]*-,,g") 256 alt_version=$(echo $version | sed -e "s,\.old$,,g") 257 dirname=$(dirname $i) 258 config= 259 for j in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do 260 if test -e "${j}" ; then 261 config="${j}" 262 break 263 fi 264 done 265 if grep -Eqx "^CONFIG_ACRN_HSM=(y|m)" "${config}" 2> /dev/null; then 266 linux_list="$linux_list $i" 267 fi 268 fi 269done 270 271file_is_not_sym () { 272 case "$1" in 273 */acrn-syms-*) 274 return 1;; 275 *) 276 return 0;; 277 esac 278} 279 280# use ELF *.out files for multiboot 281acrn_out_list= 282for i in /boot/acrn*.out; do 283 if grub_file_is_not_garbage "$i" && file_is_not_sym "$i" && grub-file --is-x86-multiboot "$i"; then 284 acrn_out_list="$acrn_out_list $i" 285 fi 286done 287# use raw binary *.bin files for multiboot2 288acrn_bin_list= 289for i in /boot/acrn*.bin; do 290 if grub_file_is_not_garbage "$i" && file_is_not_sym "$i" && grub-file --is-x86-multiboot2 "$i"; then 291 acrn_bin_list="$acrn_bin_list $i" 292 fi 293done 294# we prefer multiboot2 295if [ "x${acrn_bin_list}" != "x" ]; then 296 acrn_list="${acrn_bin_list}" 297 acrn_loader="multiboot2" 298 module_loader="module2" 299else 300 acrn_list="${acrn_out_list}" 301 acrn_loader="multiboot --quirk-modules-after-kernel" 302 module_loader="module" 303fi 304# no ACRN binary found 305if [ "x${acrn_list}" = "x" ] ; then 306 exit 0 307fi 308 309prepare_boot_cache= 310boot_device_id= 311acrn_first_entry= 312 313while [ "x${acrn_list}" != "x" ] ; do 314 current_acrn=$(echo ${acrn_list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | LC_ALL=C sort -V -r | sed -e 's/ 1$/.old/; s/ 2$//' | head -n 1) 315 acrn_basename=$(basename ${current_acrn}) 316 acrn_dirname=$(dirname ${current_acrn}) 317 rel_acrn_dirname=$(make_system_path_relative_to_its_root $acrn_dirname) 318 acrn_version=$(echo $acrn_basename | sed -e "s,.out$,,g;s,.bin$,,g;s,^acrn-,,g") 319 320 list="${linux_list}" # this is the list of possible ServiceVM kernels 321 322 # no ACRN capable linux kernel, eventually add ACRN entry for partitioned system 323 if ([ "x$list" = "x" ] || [ "x${ACRN_SERVICE_VM_ID}" = "x" ]) && [ "${ACRN_PRE_LAUNCHED_VM_COUNT}" != "0" ]; then 324 if [ -z "${acrn_first_entry}" ]; then 325 title="$(gettext_printf "%s with ACRN hypervisor" "${OS}")" 326 acrn_first_entry="false" 327 else 328 title="$(gettext_printf "%s with ACRN hypervisor %s" "${OS}" "${acrn_version}")" 329 fi 330 echo "menuentry '$(echo "$title, with Linux ${version} (ACRN ${acrn_version})" | grub_quote)' ${CLASS} \$menuentry_id_option 'acrn-gnulinux-partitioned-${acrn_version}' {" 331 message="$(gettext_printf "Loading ACRN hypervisor %s ..." ${acrn_version})" 332 cat << EOF 333 echo '$(echo "$message" | grub_quote)' 334 ${acrn_loader} ${rel_acrn_dirname}/${acrn_basename} ${GRUB_CMDLINE_ACRN} 335EOF 336 add_prelaunched_vms 337 echo "}" 338 fi 339 if [ -z "$boot_device_id" ]; then 340 boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")" 341 fi 342 343 # only if we have at least one ACRN capable kernel and a Service VM entry defined 344 while [ "x$list" != "x" ] && [ "x${ACRN_SERVICE_VM_ID}" != "x" ] ; do 345 linux=$(echo ${list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | LC_ALL=C sort -V -r | sed -e 's/ 1$/.old/; s/ 2$//' | head -n 1) 346 gettext_printf "Found ACRN linux image: %s\n" "$linux" >&2 347 basename=$(basename $linux) 348 dirname=$(dirname $linux) 349 rel_dirname=$(make_system_path_relative_to_its_root $dirname) 350 version=$(echo $basename | sed -e "s,^[^0-9]*-,,g") 351 alt_version=$(echo $version | sed -e "s,\.old$,,g") 352 linux_root_device_thisversion="${LINUX_ROOT_DEVICE}" 353 354 initrd= 355 for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \ 356 "initrd-${version}" "initramfs-${version}.img" \ 357 "initrd.img-${alt_version}" "initrd-${alt_version}.img" \ 358 "initrd-${alt_version}" "initramfs-${alt_version}.img" \ 359 "initramfs-genkernel-${version}" \ 360 "initramfs-genkernel-${alt_version}" \ 361 "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \ 362 "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}" ; do 363 if test -e "${dirname}/${i}" ; then 364 initrd="$i" 365 break 366 fi 367 done 368 if [ -n "${initrd}" ]; then 369 if [ -z "$(eval echo \${ACRN_RAMDISK_MOD_VM${ACRN_SERVICE_VM_ID}})" ]; then 370 # Use the found ramdisk despite we don't have a respective module tag 371 # in ACRN configuration. This makes the UUID magic work and avoid the 372 # problem of duplicated boot menu entries detected by os-prober when 373 # using GRUB_DEVICE directly and running grub-mkconfig on such a system. 374 gettext_printf "WARNING: Using ${initrd} despite ramdisk module tag of ACRN Service VM is not set.\n" >&2 375 else 376 gettext_printf "Found ACRN initrd image: %s\n" "${dirname}/${initrd}" >&2 377 fi 378 else 379 gettext_printf "ACRN: No initrd image for ${ACRN_BOARD}:${ACRN_SCENARIO}\n" >&2 380 # "UUID=" magic is parsed by initrds. Since there's no initrd, it can't work here. 381 linux_root_device_thisversion=${GRUB_DEVICE} 382 fi 383 384 linux_entry "${OS}" "${version}" "${acrn_version}" \ 385 "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}" "${GRUB_CMDLINE_ACRN}" 386 387 if [ -z "$boot_device_id" ]; then 388 boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")" 389 fi 390 391 list=$(echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' ') 392 done 393 acrn_list=$(echo $acrn_list | tr ' ' '\n' | fgrep -vx "$current_acrn" | tr '\n' ' ') 394done 395 396echo "" 397