Skip to content

Instantly share code, notes, and snippets.

@Huzver
Forked from stroebs/make-chr.sh
Last active December 4, 2023 05:30
Show Gist options
  • Save Huzver/4f77fc4c66e0c125d21c91ada90ff725 to your computer and use it in GitHub Desktop.
Save Huzver/4f77fc4c66e0c125d21c91ada90ff725 to your computer and use it in GitHub Desktop.
Install Mikrotik CHR on a Digital Ocean droplet
#!/bin/bash
#
# Digital Ocean Ubuntu 16.04.6 Droplet
# Please run these command before running the script
# apt update && apt full-upgrade -y && apt install -y qemu-utils pv
#
# wget script
#
# Run script
# chmod +x make-chr.sh
# ./make-chr.sh
#
# Init Settings and Variables
echo "==== GET VARIABLES ===="
PARTITION=`mount | grep 'on / type' | cut -d" " -f1`
DISK=${PARTITION::-1}
echo "Disk: $DISK"
# Standard IPV4
ADDRESS_ONE=`ip -4 addr show eth0 | grep global | cut -d' ' -f 6 | head -n 1`
ADDRESS_TWO=`ip -4 addr show eth0 | grep global | cut -d' ' -f 6 | head -n 2 | tail -n1`
echo "IPv4 address one: $ADDRESS_ONE"
echo "IPv4 address two: $ADDRESS_TWO"
# Gateway
GATEWAY=`ip -4 route list | grep default | cut -d' ' -f 3`
echo "IPv4 gateway: $GATEWAY"
# DNS
DNS_ONE=`cat /etc/resolv.conf | grep nameserver | cut -d' ' -f 6 | head -n 1`
DNS_TWO=`cat /etc/resolv.conf | grep nameserver | cut -d' ' -f 6 | head -n 2 | tail -n1`
echo "DNS address one: $DNS_ONE"
echo "DNS address two: $DNS_TWO"
# Password
PASSWORD="passw0rd"
echo "==== END VARIABLES ===="
echo ""
echo ""
# Work
echo "==== START WORK ===="
echo "STEP 1: get firmware ..."
wget https://download.mikrotik.com/routeros/6.43.14/chr-6.43.14.img.zip -O chr.img.zip
sleep 3
echo "STEP 2: unzip firmware ..."
gunzip -c chr.img.zip > chr.img
sleep 3
echo "STEP 3: convert firmware to qcow2 ..."
qemu-img convert chr.img -O qcow2 chr.qcow2
sleep 3
echo "STEP 4: modprobe nbd ..."
modprobe nbd
sleep 5
echo "STEP 5: qemu-nbd ..."
qemu-nbd -c /dev/nbd0 chr.qcow2
sleep 5
echo "STEP 6: give some time for qemu-nbd to be ready ..."
partx -a /dev/nbd0
sleep 5
echo "STEP 7: mount /dev/nbd0p1 ..."
mount /dev/nbd0p1 /mnt
sleep 5
echo "STEP 8: generate autorun.scr into chr ..."
echo "/ip dhcp-client disable 0
/ip address add address=$ADDRESS_ONE interface=[/interface ethernet find where name=ether1]
/ip address add address=$ADDRESS_TWO interface=[/interface ethernet find where name=ether1]
/ip route add gateway=$GATEWAY
/ip service set port=12022 [find where name=ssh]
/ip service set port=52458 [find where name=winbox]
/ip service disable 0,1,2,4,5,7
/ip dns set servers=$DNS_ONE,$DNS_TWO
/system package disable [find where name=ipv6]
/system package disable [find where name=wireless]
/system package disable [find where name=ups]
/system package disable [find where name=hotspot]
/system package disable [find where name=dude]
/user set 0 name=root password=$PASSWORD
" > /mnt/rw/autorun.scr
sleep 3
echo "STEP 9: sync ..."
sync
sleep 5
echo "STEP 10: umount /mnt ..."
umount /mnt
sleep 5
echo "STEP 11: mount tmpfs ..."
mount -t tmpfs tmpfs /mnt
sleep 5
echo "STEP 12: compressing to gzip, this can take several minutes"
pv /dev/nbd0 | gzip > /mnt/chr-extended.gz
sleep 5
echo "STEP 13: kill qemu-nbd ..."
killall qemu-nbd
sleep 5
echo "STEP 14: warming up sleep ..."
echo u > /proc/sysrq-trigger
sleep 5
echo "STEP 15: writing raw image zcat, this will take time ..."
zcat /mnt/chr-extended.gz | pv | dd of=$DISK
sleep 5 || true
echo "STEP 16: sync disk ..."
echo s > /proc/sysrq-trigger
sleep 5
echo "STEP 17: ok, reboot !"
echo b > /proc/sysrq-trigger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment