Skip to content

Instantly share code, notes, and snippets.

@ergoz
Forked from PavelAlves/make-chr.sh
Created March 12, 2026 22:09
Show Gist options
  • Select an option

  • Save ergoz/0708e5c26228dfc2f29bcb72c33d18be to your computer and use it in GitHub Desktop.

Select an option

Save ergoz/0708e5c26228dfc2f29bcb72c33d18be 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
apt-get update
apt install -y qemu-utils pv
qemu-img convert chr.img -O qcow2 chr.qcow2
qemu-img resize chr.qcow2 `fdisk /dev/vda -l | head -n 1 | cut -d',' -f 2 | cut -d' ' -f 2`
modprobe nbd
qemu-nbd -c /dev/nbd0 chr.qcow2
echo "Give some time for qemu-nbd to be ready"
sleep 5
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 5
echo "Compressing to gzip, this can take several minutes"
mount -t tmpfs tmpfs /mnt
pv /dev/nbd0 | gzip > /mnt/chr-extended.gz
sleep 5
killall qemu-nbd
sleep 5
echo u > /proc/sysrq-trigger
echo "Warming up sleep"
sleep 5
echo "Writing raw image, this will take time"
zcat /mnt/chr-extended.gz | pv | dd of=$DISK
echo "Sleep 10 seconds (if lucky)"
sleep 10 || true
echo "sync disk"
echo s > /proc/sysrq-trigger
echo "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