Guest User

Untitled

a guest
Apr 4th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Take one argument from the commandline: VM name
  4. if ! [ $# -eq 3 ]; then
  5. echo "Usage: $0 <node-name> <ram-MB> <vcpus>"
  6. exit 1
  7. fi
  8.  
  9. # Check if domain already exists
  10. virsh dominfo $1 > /dev/null 2>&1
  11. if [ "$?" -eq 0 ]; then
  12. echo -n "[WARNING] $1 already exists. "
  13. read -p "Do you want to overwrite $1 [y/N]? " -r
  14. if [[ $REPLY =~ ^[Yy]$ ]]; then
  15. echo ""
  16. else
  17. echo -e "\nNot overwriting $1. Exiting..."
  18. exit 1
  19. fi
  20. fi
  21.  
  22. # Directory to store images
  23. DIR=/virt/images
  24.  
  25. # Location of cloud image
  26. IMAGE=$DIR/rhel-7.4.qcow2
  27.  
  28. # Amount of RAM in MB
  29. MEM=$2
  30.  
  31. # Number of virtual CPUs
  32. CPUS=$3
  33.  
  34. DISK=$1.qcow2
  35.  
  36. BRIDGE=virbr0
  37.  
  38. USER_DATA=user-data
  39. META_DATA=meta-data
  40. CI_ISO=$1-cidata.iso
  41.  
  42. echo "$(date -R) Destroying the $1 domain (if it exists)..."
  43.  
  44. # Remove domain with the same name
  45. virsh destroy $1
  46. virsh undefine $1
  47.  
  48. echo "$(date -R) creating machine named $1 with $MEM MB of RAM and $CPUS vcpu(s) ...."
  49.  
  50. echo "$(date -R) creating cloud-init iso"
  51.  
  52. SSH_PUB_KEY=`cat ~/.ssh/id_rsa.pub`
  53.  
  54. cat > $USER_DATA << _EOF_
  55. #cloud-config
  56. ssh_pwauth: True
  57. ssh_authorized_keys:
  58. - $SSH_PUB_KEY
  59. chpasswd:
  60. list: |
  61. root:password
  62. cloud-user:password
  63. expire: False
  64. rh_subscription:
  65. username: XXXXXX
  66. password: XXXXXX
  67. auto-attach: True
  68. service-level: self-support
  69. _EOF_
  70.  
  71. cat > $META_DATA << _EOF_
  72. instance-id: $1
  73. local-hostname: $1
  74. _EOF_
  75.  
  76. genisoimage -output $CI_ISO -volid cidata -joliet -rock $USER_DATA $META_DATA
  77.  
  78. echo "$(date -R) Copying template image..."
  79.  
  80. qemu-img create -f qcow2 -o backing_file=$IMAGE /virt/runtime/$DISK
  81.  
  82. echo "$(date -R) Installing the domain and adjusting the configuration..."
  83.  
  84. echo "virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk
  85. $DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network
  86. bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel7 --noautoconsole"
  87.  
  88. virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk \
  89. $DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network \
  90. bridge=virbr0,model=virtio --os-type=linux --os-variant=rhel7 --noautoconsole
  91.  
  92.  
  93. MAC=$(virsh dumpxml $1 | awk -F\' '/mac address/ {print $2}')
  94. while true
  95. do
  96. IP=$(grep -B1 $MAC /var/lib/libvirt/dnsmasq/$BRIDGE.status | head \
  97. -n 1 | awk '{print $2}' | sed -e s/\"//g -e s/,//)
  98. if [ "$IP" = "" ]
  99. then
  100. sleep 1
  101. else
  102. break
  103. fi
  104. done
  105.  
  106.  
  107. # Eject cdrom
  108. echo "$(date -R) Cleaning up cloud-init..."
  109. virsh change-media $1 hda --eject --config
  110.  
  111. # Remove the unnecessary cloud init files
  112. rm $USER_DATA $CI_ISO $META_DATA
  113.  
  114. echo "$(date -R) DONE. SSH to $1 using $IP , with username 'cloud-user'."
Add Comment
Please, Sign In to add comment