Commit cbf03967 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

Add colors to logfile

Add colors to logfile
parents c1528249 983e6f82
Loading
Loading
Loading
Loading
+14 −14
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ if [ -f "${DATA}" ]; then

  if [ "$DATA_SIZE" -gt "$OLD_SIZE" ]; then

    echo "INFO: Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.."
    info "Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.."

    if [[ "${ALLOCATE}" == [Nn]* ]]; then

@@ -36,20 +36,20 @@ if [ -f "${DATA}" ]; then
      SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)

      if (( REQ > SPACE )); then
        echo "ERROR: Not enough free space to resize data disk to ${DISK_SIZE}."
        echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 84
        error "Not enough free space to resize data disk to ${DISK_SIZE}."
        error "Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 84
      fi

      # Resize file by allocating more space
      if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
        echo "ERROR: Could not allocate a file for the data disk." && exit 85
        error "Could not allocate a file for the data disk." && exit 85
      fi

      if [[ "${ALLOCATE}" == [Zz]* ]]; then

        GB=$(( (REQ + 1073741823)/1073741824 ))

        echo "INFO: Preallocating ${GB} GB of diskspace, please wait..."
        info "Preallocating ${GB} GB of diskspace, please wait..."
        dd if=/dev/urandom of="${DATA}" seek="${OLD_SIZE}" count="${REQ}" bs=1M iflag=count_bytes oflag=seek_bytes status=none

      fi
@@ -58,8 +58,8 @@ if [ -f "${DATA}" ]; then

  if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then

    echo "INFO: Shrinking existing disks is not supported yet!"
    echo "INFO: Creating backup of old drive in storage folder..."
    info "Shrinking existing disks is not supported yet!"
    info "Creating backup of old drive in storage folder..."

    mv -f "${DATA}" "${DATA}.bak"

@@ -79,19 +79,19 @@ if [ ! -f "${DATA}" ]; then
    SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)

    if (( DATA_SIZE > SPACE )); then
      echo "ERROR: Not enough free space to create a data disk of ${DISK_SIZE}."
      echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 86
      error "Not enough free space to create a data disk of ${DISK_SIZE}."
      error "Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 86
    fi

    # Create an empty file
    if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
      rm -f "${DATA}"
      echo "ERROR: Could not allocate a file for the data disk." && exit 87
      error "Could not allocate a file for the data disk." && exit 87
    fi

    if [[ "${ALLOCATE}" == [Zz]* ]]; then

      echo "INFO: Preallocating ${DISK_SIZE} of diskspace, please wait..."
      info "Preallocating ${DISK_SIZE} of diskspace, please wait..."
      dd if=/dev/urandom of="${DATA}" count="${DATA_SIZE}" bs=1M iflag=count_bytes status=none

    fi
@@ -99,7 +99,7 @@ if [ ! -f "${DATA}" ]; then

  # Check if file exists
  if [ ! -f "${DATA}" ]; then
    echo "ERROR: Data disk does not exist ($DATA)" && exit 88
    error "Data disk does not exist ($DATA)" && exit 88
  fi

fi
@@ -108,7 +108,7 @@ fi
SIZE=$(stat -c%s "${DATA}")

if [[ SIZE -ne DATA_SIZE ]]; then
  echo "ERROR: Data disk has the wrong size: ${SIZE}" && exit 89
  error "Virtual disk has the wrong size: ${SIZE}" && exit 89
fi

DISK_OPTS="\
+4 −4
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ set -Eeuo pipefail
TMP="/boot.img"
rm -f "$TMP"

echo "Downloading ${BOOT} as boot image..."
info "Downloading ${BOOT} as boot image..."

# Check if running with interactive TTY or redirected to docker log
if [ -t 1 ]; then
@@ -16,14 +16,14 @@ fi
[[ "${DEBUG}" == [Yy1]* ]] && set -x

{ wget "$BOOT" -O "$TMP" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || :
(( rc != 0 )) && echo "ERROR: Failed to download ${BOOT}, reason: $rc" && exit 60

[ ! -f "$TMP" ] && echo "Failed to download ${BOOT}" && exit 61
(( rc != 0 )) && error "Failed to download ${BOOT}, reason: $rc" && exit 60
[ ! -f "$TMP" ] && error "Failed to download ${BOOT}" && exit 61

SIZE=$(stat -c%s "$TMP")

if ((SIZE<100000)); then
  echo "Invalid ISO file: Size is smaller than 100 KB" && exit 62
  error "Invalid ISO file: Size is smaller than 100 KB" && exit 62
fi

FILE="$STORAGE/boot.img"
+21 −46
Original line number Diff line number Diff line
@@ -22,69 +22,43 @@ set -Eeuo pipefail

configureDHCP() {

  VM_NET_VLAN="${VM_NET_TAP}_vlan"
  GATEWAY=$(ip r | grep default | awk '{print $3}')
  NETWORK=$(ip -o route | grep "${VM_NET_DEV}" | grep -v default | awk '{print $1}')
  IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)

  [[ "${DEBUG}" == [Yy1]* ]] && set -x

  # Create a macvlan network to allow for communication between the host and the VM guest
  { ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge ; rc=$?; } || :

  if (( rc != 0 )); then
    echo "ERROR: Cannot create macvlan interface. Please make sure the network type is 'macvlan' and not 'ipvlan',"
    echo "ERROR: and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 15
  fi

  ip address add "${IP}" dev "${VM_NET_VLAN}"
  ip link set dev "${VM_NET_VLAN}" up

  ip route flush dev "${VM_NET_VLAN}"
  ip route del "${NETWORK}" dev "${VM_NET_DEV}"
  ip route add "${NETWORK}" dev "${VM_NET_VLAN}" metric 0
  
  # Create a macvtap network for the VM guest

  { ip link add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge ; rc=$?; } || :
  
  if (( rc != 0 )); then
    echo "ERROR: Capability NET_ADMIN has not been set most likely. Please add the "
    echo "ERROR: following docker setting to your container: --cap-add NET_ADMIN" && exit 16
    error "Cannot create macvtap interface. Please make sure the network type is 'macvlan' and not 'ipvlan',"
    error "and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 16
  fi

  ip link set "${VM_NET_TAP}" up

  { set +x; } 2>/dev/null

  TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex)
  TAP_PATH="/dev/tap${TAP_NR}"

  # Create dev file (there is no udev in container: need to be done manually)
  IFS=: read -r MAJOR MINOR < <(cat /sys/devices/virtual/net/"${VM_NET_TAP}"/tap*/dev)

  if (( MAJOR < 1)); then
     echo "ERROR: Cannot find: sys/devices/virtual/net/${VM_NET_TAP}" && exit 18
  fi
  (( MAJOR < 1)) && error "Cannot find: sys/devices/virtual/net/${VM_NET_TAP}" && exit 18

  [[ ! -e "${TAP_PATH}" ]] && [[ -e "/dev0/${TAP_PATH##*/}" ]] && ln -s "/dev0/${TAP_PATH##*/}" "${TAP_PATH}"

  if [[ ! -e "${TAP_PATH}" ]]; then
    { mknod "${TAP_PATH}" c "$MAJOR" "$MINOR" ; rc=$?; } || :
    (( rc != 0 )) && echo "ERROR: Cannot mknod: ${TAP_PATH} ($rc)" && exit 20
    (( rc != 0 )) && error "Cannot mknod: ${TAP_PATH} ($rc)" && exit 20
  fi

  { exec 30>>"$TAP_PATH"; rc=$?; } || :

  if (( rc != 0 )); then
    echo "ERROR: Cannot create TAP interface ($rc). Please add the following docker settings to your "
    echo "ERROR: container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21
    error "Cannot create TAP interface ($rc). Please add the following docker settings to your "
    error "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21
  fi

  { exec 40>>/dev/vhost-net; rc=$?; } || :

  if (( rc != 0 )); then
    echo "ERROR: VHOST can not be found ($rc). Please add the following "
    echo "ERROR: docker setting to your container: --device=/dev/vhost-net" && exit 22
    error "VHOST can not be found ($rc). Please add the following "
    error "docker setting to your container: --device=/dev/vhost-net" && exit 22
  fi

  NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30"
@@ -92,15 +66,16 @@ configureDHCP() {

configureNAT () {

  # Create a bridge with a static IP for the VM guest

  VM_NET_IP='20.20.20.21'
  [[ "${DEBUG}" == [Yy1]* ]] && set -x

  # Create bridge with static IP for the VM guest
  { ip link add dev dockerbridge type bridge ; rc=$?; } || :

  if (( rc != 0 )); then
    echo "ERROR: Capability NET_ADMIN has not been set most likely. Please add the "
    echo "ERROR: following docker setting to your container: --cap-add NET_ADMIN" && exit 23
    error "Capability NET_ADMIN has not been set most likely. Please add the "
    error "following docker setting to your container: --cap-add NET_ADMIN" && exit 23
  fi

  ip address add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
@@ -128,7 +103,7 @@ configureNAT () {
  if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
    { sysctl -w net.ipv4.ip_forward=1 ; rc=$?; } || :
    if (( rc != 0 )); then
      echo "ERROR: Please add the following docker setting to your container: --sysctl net.ipv4.ip_forward=1" && exit 24
      error "Please add the following docker setting to your container: --sysctl net.ipv4.ip_forward=1" && exit 24
    fi
  fi

@@ -178,6 +153,8 @@ configureNAT () {
  $DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}

  { set +x; } 2>/dev/null

  [[ "${DEBUG}" == [Yy1]* ]] && echo
}

# ######################################
@@ -191,7 +168,7 @@ if [ ! -c /dev/net/tun ]; then
  chmod 666 /dev/net/tun
fi

[ ! -c /dev/net/tun ] && echo "ERROR: TUN network interface not available..." && exit 85
[ ! -c /dev/net/tun ] && error "TUN network interface not available..." && exit 85

# Create the necessary file structure for /dev/vhost-net
if [ ! -c /dev/vhost-net ]; then
@@ -208,15 +185,15 @@ GATEWAY=$(ip r | grep default | awk '{print $3}')
if [[ "${DEBUG}" == [Yy1]* ]]; then

  IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
  echo "INFO: Container IP is ${IP} with gateway ${GATEWAY}" && echo
  info "Container IP is ${IP} with gateway ${GATEWAY}" && echo

fi

if [[ "${DHCP}" == [Yy1]* ]]; then

  if [[ "$GATEWAY" == "172."* ]]; then
    echo -n "ERROR: You cannot enable DHCP while the container is "
    echo "in a bridge network, only on a macvlan network!" && exit 86
    error "You cannot enable DHCP while the container is "
    error "in a bridge network, only on a macvlan network!" && exit 86
  fi

  # Configuration for DHCP IP
@@ -231,6 +208,4 @@ fi

NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"

[[ "${DEBUG}" == [Yy1]* ]] && echo && echo "Finished network setup.." && echo

return 0
+3 −3
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ _graceful_shutdown() {
  [ ! -f "${_QEMU_PID}" ] && return
  [ -f "${_QEMU_SHUTDOWN_COUNTER}" ] && return

  echo && echo "Received $1 signal, shutting down..."
  echo && info "Received $1 signal, shutting down..."
  echo 0 > "${_QEMU_SHUTDOWN_COUNTER}"

  # Send the shutdown (system_powerdown) command to the QMP monitor
@@ -41,13 +41,13 @@ _graceful_shutdown() {
    if echo 'info version'| nc -q 1 -w 1 localhost "${QEMU_MONPORT}" >/dev/null 2>&1 ; then

      sleep 1
      echo "Shutting down, waiting... ($(cat ${_QEMU_SHUTDOWN_COUNTER})/${QEMU_POWERDOWN_TIMEOUT})"
      info "Shutting down, waiting... ($(cat ${_QEMU_SHUTDOWN_COUNTER})/${QEMU_POWERDOWN_TIMEOUT})"

    fi

  done

  echo && echo "Quitting..."
  echo && echo "Quitting..."
  echo 'quit' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" >/dev/null 2>&1 || true

  return
+10 −7
Original line number Diff line number Diff line
@@ -10,16 +10,19 @@ set -Eeuo pipefail
: ${DISK_SIZE:='16G'}   # Initial data disk size
: ${RAM_SIZE:='512M'}   # Maximum RAM amount

echo "Starting QEMU for Docker v${VERSION}..."
trap 'echo >&2 "Error status $? for: ${BASH_COMMAND} (line $LINENO/$BASH_LINENO)"' ERR
echo "❯ Starting QEMU for Docker v${VERSION}..."

[ ! -f "/run/run.sh" ] && echo "ERROR: Script must run inside Docker container!" && exit 11
[ "$(id -u)" -ne "0" ] && echo "ERROR: Script must be executed with root privileges." && exit 12
info () { echo -e "\E[1;34m❯ \E[1;36m$1\E[0m" ; }
error () { echo -e >&2 "\E[1;31m❯ ERROR: $1\E[0m" ; }
trap 'error "Status $? while: ${BASH_COMMAND} (line $LINENO/$BASH_LINENO)"' ERR

[ ! -f "/run/run.sh" ] && error "Script must run inside Docker container!" && exit 11
[ "$(id -u)" -ne "0" ] && error "Script must be executed with root privileges." && exit 12

STORAGE="/storage"
KERNEL=$(uname -r | cut -b 1)

[ ! -d "$STORAGE" ] && echo "ERROR: Storage folder (${STORAGE}) not found!" && exit 13
[ ! -d "$STORAGE" ] && error "Storage folder (${STORAGE}) not found!" && exit 13

if [ ! -f "$STORAGE/boot.img" ]; then
  . /run/install.sh
@@ -46,7 +49,7 @@ else
fi

if [ -n "${KVM_ERR}" ]; then
  echo "ERROR: KVM acceleration not detected ${KVM_ERR}, please enable it."
  error "KVM acceleration not detected ${KVM_ERR}, see the FAQ about this."
  [[ "${DEBUG}" == [Yy1]* ]] && exit 88
else
  KVM_OPTS=",accel=kvm -enable-kvm -cpu host"