Adam Gandelman | 22ec45e | 2014-10-16 17:41:22 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # **create-nodes** |
| 4 | |
| 5 | # Creates baremetal poseur nodes for ironic testing purposes |
| 6 | |
| 7 | set -ex |
| 8 | |
Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 9 | # Keep track of the DevStack directory |
Adam Gandelman | 22ec45e | 2014-10-16 17:41:22 -0700 | [diff] [blame] | 10 | TOP_DIR=$(cd $(dirname "$0")/.. && pwd) |
| 11 | |
| 12 | NAME=$1 |
| 13 | CPU=$2 |
| 14 | MEM=$(( 1024 * $3 )) |
Dean Troyer | dc97cb7 | 2015-03-28 08:20:50 -0500 | [diff] [blame] | 15 | # Extra G to allow fuzz for partition table : flavor size and registered size |
Adam Gandelman | 22ec45e | 2014-10-16 17:41:22 -0700 | [diff] [blame] | 16 | # need to be different to actual size. |
| 17 | DISK=$(( $4 + 1)) |
| 18 | |
| 19 | case $5 in |
| 20 | i386) ARCH='i686' ;; |
| 21 | amd64) ARCH='x86_64' ;; |
| 22 | *) echo "Unsupported arch $4!" ; exit 1 ;; |
| 23 | esac |
| 24 | |
| 25 | BRIDGE=$6 |
| 26 | EMULATOR=$7 |
| 27 | LOGDIR=$8 |
| 28 | |
| 29 | LIBVIRT_NIC_DRIVER=${LIBVIRT_NIC_DRIVER:-"e1000"} |
| 30 | LIBVIRT_STORAGE_POOL=${LIBVIRT_STORAGE_POOL:-"default"} |
| 31 | LIBVIRT_CONNECT_URI=${LIBVIRT_CONNECT_URI:-"qemu:///system"} |
| 32 | |
| 33 | export VIRSH_DEFAULT_CONNECT_URI=$LIBVIRT_CONNECT_URI |
| 34 | |
| 35 | if ! virsh pool-list --all | grep -q $LIBVIRT_STORAGE_POOL; then |
| 36 | virsh pool-define-as --name $LIBVIRT_STORAGE_POOL dir --target /var/lib/libvirt/images >&2 |
| 37 | virsh pool-autostart $LIBVIRT_STORAGE_POOL >&2 |
| 38 | virsh pool-start $LIBVIRT_STORAGE_POOL >&2 |
| 39 | fi |
| 40 | |
| 41 | pool_state=$(virsh pool-info $LIBVIRT_STORAGE_POOL | grep State | awk '{ print $2 }') |
| 42 | if [ "$pool_state" != "running" ] ; then |
| 43 | [ ! -d /var/lib/libvirt/images ] && sudo mkdir /var/lib/libvirt/images |
| 44 | virsh pool-start $LIBVIRT_STORAGE_POOL >&2 |
| 45 | fi |
| 46 | |
| 47 | if [ -n "$LOGDIR" ] ; then |
| 48 | mkdir -p "$LOGDIR" |
| 49 | fi |
| 50 | |
| 51 | PREALLOC= |
| 52 | if [ -f /etc/debian_version ]; then |
| 53 | PREALLOC="--prealloc-metadata" |
| 54 | fi |
| 55 | |
| 56 | if [ -n "$LOGDIR" ] ; then |
| 57 | VM_LOGGING="--console-log $LOGDIR/${NAME}_console.log" |
| 58 | else |
| 59 | VM_LOGGING="" |
| 60 | fi |
| 61 | VOL_NAME="${NAME}.qcow2" |
| 62 | |
| 63 | if ! virsh list --all | grep -q $NAME; then |
| 64 | virsh vol-list --pool $LIBVIRT_STORAGE_POOL | grep -q $VOL_NAME && |
| 65 | virsh vol-delete $VOL_NAME --pool $LIBVIRT_STORAGE_POOL >&2 |
| 66 | virsh vol-create-as $LIBVIRT_STORAGE_POOL ${VOL_NAME} ${DISK}G --format qcow2 $PREALLOC >&2 |
| 67 | volume_path=$(virsh vol-path --pool $LIBVIRT_STORAGE_POOL $VOL_NAME) |
| 68 | # Pre-touch the VM to set +C, as it can only be set on empty files. |
| 69 | sudo touch "$volume_path" |
| 70 | sudo chattr +C "$volume_path" || true |
| 71 | $TOP_DIR/scripts/configure-vm \ |
| 72 | --bootdev network --name $NAME --image "$volume_path" \ |
| 73 | --arch $ARCH --cpus $CPU --memory $MEM --libvirt-nic-driver $LIBVIRT_NIC_DRIVER \ |
| 74 | --emulator $EMULATOR --network $BRIDGE $VM_LOGGING >&2 |
| 75 | |
| 76 | fi |
| 77 | |
| 78 | # echo mac |
| 79 | virsh dumpxml $NAME | grep "mac address" | head -1 | cut -d\' -f2 |