Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # VPS2RouterOS
  4. #
  5. # !!!!!!!!!PLEASE READ ALL THE FOLLOWING TEXT BEFORE RUNNING THIS!!!!!!!!!!!
  6. #
  7. # Please UPDATE IMMEDIATELY after installation. The script will install a
  8. # relatively old version of RouterOS, which have some major bugs.
  9. #
  10. # You can use `wget go.swineson.me/vps2routeros` to download this script
  11. # if you have trouble in copy/paste. If you use `curl`, remember to add `-L`
  12. # flag.
  13. #
  14. # For detailed explaination of this script see my blog post (in Chinese):
  15. # https://blog.swineson.me/install-routeros-on-any-ubuntu-vps/
  16. # script modified from:
  17. # https://www.digitalocean.com/community/questions/installing-mikrotik-routeros
  18. #
  19. # This will download and install RouterOS on your x86_64 device
  20. # (works with most physical device / VM / VPS)
  21. # Please start with a recent version of Ubuntu (16.04+). Debian will not work.
  22. #
  23. # CAUTION: ALL PREVIOUS DATA IS LOST INSTANTLY!
  24. # PLEASE REMEMBER YOUR NETWORK CONFIGURATION!
  25. # (If you need IPv6, remember your gateway link-local address too)
  26. # IT IS RECOMMENDED TO HAVE CONSOLE ACCESS TO THE MACHINE.
  27. # You need a bash (or equivalent) to run this script -- /bin/sh won't work!
  28. #
  29. # First change the settings below if needed. Then run this script.
  30. #
  31. # Note: after "write disk" staging started, you may see your console stuck
  32. # or in a mess. This is normal since the kernel disappeared while it is running.
  33. # If this happens, wait 5 minutes and reset that device's power (if it is a VM,
  34. # reboot it from control panel), and RouterOS should boot.
  35. #
  36. # After installation, use user: admin password: (empty) to login, change password
  37. # fast, then go to /system packages and upgrade to the newest version.
  38.  
  39. set -eu
  40.  
  41. # ======================= please change these =================================
  42. # your network interface to internet
  43. # this is used to auto configure IPv4 after reboot
  44. # (this may not work for every device)
  45. # eth0 for most devices, ens3 for Vultr
  46. # you can use `ip addr` or `ifconfig` to find out this
  47. MAIN_INTERFACE=ens18
  48.  
  49. # get IPv4 address in IP-CIDR format
  50. # do not modify unless you know what you are doing
  51. ADDRESS=`ip addr show $MAIN_INTERFACE | grep global | cut -d' ' -f 6 | head -n 1`
  52.  
  53. # get gateway IP
  54. # do not modify unless you know what you are doing
  55. GATEWAY=`ip route list | grep default | cut -d' ' -f 3`
  56.  
  57. # HDD device (not partition)
  58. # May not be compatible with SCSI drives; see official document of RouterOS CHR
  59. # you can use `lsblk` to find out this
  60. DISK=/dev/sda
  61.  
  62. # Note: you can customize commands to be executed when RouterOS initializes.
  63. # Search `Auto configure script` below
  64. # do not modify that unless you know what you are doing
  65.  
  66. # ======================= no need to modify below ============================
  67.  
  68. # check if this script is running under root
  69. if [[ $EUID -ne 0 ]]; then
  70. echo "This script must be run as root" 1>&2
  71. exit 1
  72. fi
  73.  
  74. echo "installing packages"
  75. apt update -y
  76. apt install -y qemu-utils pv psmisc
  77.  
  78. echo "download image"
  79. wget https://download2.mikrotik.com/routeros/6.39/chr-6.39.img.zip -O chr.img.zip
  80.  
  81. echo "unzip image"
  82. gunzip -c chr.img.zip > chr.img
  83.  
  84. echo "convert image"
  85. qemu-img convert chr.img -O qcow2 chr.qcow2
  86. qemu-img resize chr.qcow2 `fdisk $DISK -l | head -n 1 | cut -d',' -f 2 | cut -d' ' -f 2`
  87.  
  88. echo "mount image"
  89. modprobe nbd
  90. qemu-nbd -c /dev/nbd0 chr.qcow2
  91. echo "waiting qemu-nbd"
  92. sleep 5
  93. partprobe /dev/nbd0
  94. mount /dev/nbd0p2 /mnt
  95.  
  96. echo "write init script"
  97. cat > /mnt/rw/autorun.scr <<EOF
  98. # Auto configure script on RouterOS first boot
  99. # feel free to customize it if you really need
  100. /ip address add address=$ADDRESS interface=[/interface ethernet find where name=ether1]
  101. /ip route add gateway=$GATEWAY
  102. /ip service disable telnet
  103. /ip dns set servers=8.8.8.8,8.8.4.4
  104. EOF
  105.  
  106. echo "unmount image"
  107. umount /mnt
  108.  
  109. echo "resize partition"
  110. echo -e 'd\n2\nn\np\n2\n65537\n\nw\n' | fdisk /dev/nbd0
  111. e2fsck -f -y /dev/nbd0p2 || true
  112. resize2fs /dev/nbd0p2
  113. sleep 5
  114.  
  115. echo "move image to RAM (this will take a lot of time)"
  116. mount -t tmpfs tmpfs /mnt
  117. pv /dev/nbd0 | gzip > /mnt/chr-extended.gz
  118. sleep 5
  119.  
  120. echo "stop qemu-nbd"
  121. killall qemu-nbd
  122. sleep 5
  123. echo u > /proc/sysrq-trigger
  124. sleep 5
  125.  
  126. echo "write disk"
  127. zcat /mnt/chr-extended.gz | pv > $DISK
  128.  
  129. echo "sync disk"
  130. echo s > /proc/sysrq-trigger
  131.  
  132. echo "wait a while"
  133. sleep 5 || echo "please wait 5 seconds and execute\n\techo b > /proc/sysrq-trigger\nmanually, or hard reset device"
  134.  
  135. echo "rebooting"
  136. echo b > /proc/sysrq-trigger
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement