github.com/rancher/elemental/tests@v0.0.0-20240517125144-ae048c615b3f/scripts/install-vm (about)

     1  #!/bin/bash
     2  
     3  set -e -x
     4  
     5  # Variable(s) and default values
     6  ARCH=$(uname -m)
     7  EMULATED_TPM="none"
     8  FW_CODE=/usr/share/qemu/ovmf-${ARCH}-smm-suse-code.bin
     9  FW_VARS=$(realpath ../assets/ovmf-template-vars.fd)
    10  HDD_SIZE=30
    11  MAC=$2
    12  VM_NAME=$1
    13  
    14  # Configure hugepages if needed
    15  NR_HUGEPAGES=$(</proc/sys/vm/nr_hugepages)
    16  if (( NR_HUGEPAGES == 0 && USE_HUGEPAGES != 0 )); then
    17    # Not configured, do it now!
    18    MEMTOTAL_KB=$(awk '/^MemTotal:/ { print $2 }' /proc/meminfo)
    19  
    20    # Number of hugepages (with hugepagesize set to 2MB)
    21    # NOTE: keep 24GB by default for the hypervisor/Rancher Manager Server
    22    # And this value can be modified with HOST_MEMORY_RESERVED variable
    23    (( VALUE = (MEMTOTAL_KB - ${HOST_MEMORY_RESERVED:-24576} * 1024) / 2048 ))
    24  
    25    # Set nr_hugepage
    26    (( VALUE > 0 )) \
    27      && sudo bash -c "echo ${VALUE} > /proc/sys/vm/nr_hugepages"
    28  
    29    # Set memory config on command line
    30    INSTALL_FLAG+=" --memorybacking hugepages=yes,size=2,unit=M,locked=yes --memory ${VM_MEM:-4096},hugepages=yes"
    31  else
    32    # Set memory config on command line
    33    INSTALL_FLAG+=" --memory ${VM_MEM:-4096}"
    34  fi
    35  
    36  # Don't configure TPM if software emulation (EMULATE_TPM=true) is used
    37  if [[ ${EMULATE_TPM} != "true" ]]; then
    38    EMULATED_TPM="emulator,model=tpm-crb,version=2.0"
    39  fi
    40  
    41  # Create directories: dedicated one for storage pool + logs one
    42  mkdir -p logs ${VM_NAME}
    43  
    44  # iPXE stuff will not be used if ISO is set
    45  if [[ ${BOOT_TYPE} == "iso" ]]; then
    46    ISO=$(realpath ../../elemental-*.iso 2>/dev/null)
    47  
    48    # Exit if ISO is not available
    49    [[ ! -f ${ISO} ]] \
    50      && echo "File ${ISO} not found! Exiting!" >&2 \
    51      && exit 1
    52  
    53    # Soft-link the ISO to avoid "Could not define storage pool" error
    54    ln -s ${ISO} ${VM_NAME}/
    55  
    56    # Force ISO boot
    57    INSTALL_FLAG+=" --cdrom ${VM_NAME}/${ISO##*/} --noreboot"
    58    
    59    # Use noautoconsole to check SeedImage cloud-config
    60    # because we need to ssh into the VM when it is installing
    61    [[ ${POOL} == "master" ]] && INSTALL_FLAG+=" --noautoconsole"
    62  elif [[ ${BOOT_TYPE} == "raw" ]]; then
    63    RAW_IMAGE=$(realpath ../../elemental-*.raw 2>/dev/null)
    64  
    65    # Exit if RAW is not available
    66    [[ ! -f ${RAW_IMAGE} ]] \
    67      && echo "File ${RAW_IMAGE} not found! Exiting!" >&2 \
    68      && exit 1  
    69    
    70    # Duplicate the raw image depending on nodes number
    71    cp ${RAW_IMAGE} ${VM_NAME}/${VM_NAME}.img
    72  
    73    # We need to resize the raw image according to the HDD_SIZE
    74    /usr/bin/qemu-img resize ${VM_NAME}/${VM_NAME}.img ${HDD_SIZE}G
    75  
    76    INSTALL_FLAG+=" --noautoconsole"
    77  else
    78    # Create symlink for binary but only if it doesn't exist
    79    SYM_LINK=../../ipxe.efi
    80    if [[ ! -h ${SYM_LINK} ]]; then
    81      # Exit if binary is not available
    82      IPXE_BIN=$(realpath ../assets/ipxe-${ARCH}.efi)
    83      [[ ! -f ${IPXE_BIN} ]] \
    84        && echo "File ${IPXE_BIN} not found! Exiting!" >&2 \
    85        && exit 1
    86  
    87      # Force remove, to avoid issue with 'ln'
    88      # Only useful if an EFI file exists and it's not a symlink
    89      rm -f ${SYM_LINK}
    90      ln -s ${IPXE_BIN} ${SYM_LINK}
    91    fi
    92  
    93    # Force PXE boot and add noreboot flag (cannot be used with raw image)
    94    INSTALL_FLAG+=" --pxe --noreboot"
    95  fi
    96  
    97  # VM variables
    98  LOG_FILE=logs/bootstrap_${VM_NAME}.log
    99  CMD="sudo virt-install \
   100         --name ${VM_NAME} \
   101         --os-variant opensuse-unknown \
   102         --virt-type kvm \
   103         --machine q35 \
   104         --boot loader=${FW_CODE},loader.readonly=yes,loader.secure=yes,loader.type=pflash,nvram.template=${FW_VARS} \
   105         --features smm.state=yes \
   106         --vcpus ${VM_CPU:-4} \
   107         --cpu host \
   108         --disk path=${VM_NAME}/${VM_NAME}.img,bus=scsi,size=${HDD_SIZE} \
   109         --check disk_size=off \
   110         --graphics none \
   111         --serial pty \
   112         --console pty,target_type=virtio \
   113         --rng random \
   114         --tpm ${EMULATED_TPM} \
   115         --network network=default,bridge=virbr0,model=virtio,mac=${MAC} \
   116         ${INSTALL_FLAG}"
   117  
   118  # Create VM
   119  # But before, sync all and wait a little bit before executing virt-install
   120  # To try to avoid sporadic issue where sometimes the media is not found by virt-install
   121  sync \
   122    && sleep 20s \
   123    && script -E never -e -f -q -O ${LOG_FILE} -c "${CMD}" >/dev/null 2>&1