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
@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