iputuhariyadi

mikrotik-chr.sh

Dec 25th, 2017
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.90 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #vars
  4. version="nil"
  5. vmID="nil"
  6.  
  7. echo "############## Start of Script ##############
  8.  
  9. ## Checking if temp dir is available..."
  10. if [ -d /root/temp ]
  11. then
  12.     echo "-- Directory exists!"
  13. else
  14.     echo "-- Creating temp dir!"
  15.     mkdir /home/root/temp
  16. fi
  17. # Ask user for version
  18. echo "## Preparing for image download and VM creation!"
  19. read -p "Please input CHR version to deploy (6.38.2, 6.40.1, etc):" version
  20. # Check if image is available and download if needed
  21. if [ -f /root/temp/chr-$version.img ]
  22. then
  23.     echo "-- CHR image is available."
  24. else
  25.     echo "-- Downloading CHR $version image file."
  26.     cd  /root/temp
  27.     echo "---------------------------------------------------------------------------"
  28.     wget https://download2.mikrotik.com/routeros/$version/chr-$version.img.zip
  29.     unzip chr-$version.img.zip
  30.     echo "---------------------------------------------------------------------------"
  31. fi
  32. # List already existing VM's and ask for vmID
  33. echo "== Printing list of VM's on this hypervisor!"
  34. qm list
  35. echo ""
  36. read -p "Please Enter free vm ID to use:" vmID
  37. echo ""
  38. # Create storage dir for VM if needed.
  39. if [ -d /var/lib/vz/images/$vmID ]
  40. then
  41.     echo "-- VM Directory exists! Ideally try another vm ID!"
  42.     read -p "Please Enter free vm ID to use:" vmID
  43. else
  44.     echo "-- Creating VM image dir!"
  45.     mkdir /var/lib/vz/images/$vmID
  46. fi
  47. # Creating qcow2 image for CHR.
  48. echo "-- Converting image to qcow2 format "
  49. qemu-img convert \
  50.     -f raw \
  51.     -O qcow2 \
  52.     /root/temp/chr-$version.img \
  53.     /var/lib/vz/images/$vmID/vm-$vmID-disk-1.qcow2
  54. # Creating VM
  55. echo "-- Creating new CHR VM"
  56. qm create $vmID \
  57.   --name chr-$version \
  58.   --net0 virtio,bridge=vmbr0 \
  59.   --bootdisk virtio0 \
  60.   --ostype l26 \
  61.   --memory 256 \
  62.   --onboot no \
  63.   --sockets 1 \
  64.   --cores 1 \
  65.   --virtio0 local:$vmID/vm-$vmID-disk-1.qcow2
  66. echo "############## End of Script ##############"
Add Comment
Please, Sign In to add comment