Skip to content

Instantly share code, notes, and snippets.

@zztopper
Forked from stroebs/make-chr.sh
Last active August 31, 2019 03:20
Show Gist options
  • Select an option

  • Save zztopper/9276df8d3a04f63a6bdf886a862bf32f to your computer and use it in GitHub Desktop.

Select an option

Save zztopper/9276df8d3a04f63a6bdf886a862bf32f to your computer and use it in GitHub Desktop.
Install Mikrotik CHR on generic KVM/VMWare virtual machine
#!/bin/bash
#Setting variables
PARTITION=`mount | grep 'on / type' | cut -d" " -f1`
DISK=${PARTITION::-1}
echo "Disk: $DISK"
ADDRESS=`ip -4 addr show eth0 | grep global | cut -d' ' -f 6 | head -n 1`
echo "IPv4 address: $ADDRESS\n"
GATEWAY=`ip -4 route list | grep default | cut -d' ' -f 3`
echo "IPv4 gateway: $GATEWAY\n"
ADDRESS6=`ip -6 addr show eth0 | grep global | grep inet6 | cut -d' ' -f 6 | head -n 1`
echo "IPv6 address: $ADDRESS6\n"
GATEWAY6=`ip -6 route list | grep default | cut -d' ' -f 3`
echo "IPv6 gateway: $GATEWAY6\n"
PASSWORD="passw0rd"
#Begin installing
wget https://download2.mikrotik.com/routeros/6.42.1/chr-6.42.1.img.zip -O chr.img.zip
gunzip -c chr.img.zip > chr.img
qemu-img convert chr.img -O qcow2 chr.qcow2
modprobe nbd
qemu-nbd -c /dev/nbd0 chr.qcow2
echo "Give some time for qemu-nbd to be ready"
sleep 2
partx -a /dev/nbd0
mount /dev/nbd0p2 /mnt
echo "/ip dhcp-client disable 0
/ip address add address=$ADDRESS interface=[/interface ethernet find where name=ether1]
/ip route add gateway=$GATEWAY
/ip service disable 0,1,2,4,5,7
/ip service set port=12022 [find where name=ssh]
/ip dns set servers=1.1.1.1
/system package enable [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]
/user set 0 name=root password=$PASSWORD
" > /mnt/rw/autorun.scr
rm -rf /mnt/var/pdb/dude
rm -f /mnt/var/pdb/ipv6/disabled
sync
umount /mnt
sleep 1
echo "Compressing to gzip, this can take several minutes"
mount -t tmpfs tmpfs /mnt
pv /dev/nbd0 | gzip > /mnt/chr-extended.gz
sleep 1
killall qemu-nbd
sleep 1
echo u > /proc/sysrq-trigger
echo "Warming up sleep"
sleep 1
echo "Writing raw image, this will take time"
zcat /mnt/chr-extended.gz | pv | dd of=$DISK
echo "Sleep 5 seconds (if lucky)"
sleep 5 || true
echo "sync disk"
echo s > /proc/sysrq-trigger
echo "Ok, reboot"
echo b > /proc/sysrq-trigger
@PavelAlves
Copy link

Hello,

Thanks for the script. It worked both on Google Cloud and on an other small VPS seller. Ubuntu 16.04 LTS 64bit.

Only thing is that, one has to execute the original script first "https://gist.github.com/stroebs/54fc09734a3911e91eeeb43434f117df", it reboots with no changes to the OS, and then execute this script. Executing this script before the original one, resulted in an error on line 53 and rebooting with no change to the OS. I suspect it needs to update repositories and install dependencies but this script removed that section.

Also, on Google Cloud it was able to set CHR's IP, Gateway and DNS, while on the other company it wasn't, luckily they had noVNC so I was able access it as if I was local, since it had no internet connectivity, to configure it manually via command line. In any case, I suspect I could have restored the VPS and set the IP, Gateway and DNS manually on the script anyways.

Thanks.

@stroebs
Copy link

stroebs commented Aug 31, 2019

@PavelAlves - I've updated the original script, which now actually works. Maybe you can spot why mine has to be run first?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment