RouterOS MikroTik boot sequence…

Purpose of this blog

I have always wonder how Mikrotik have implemented their  good work in RouterOS, based on Linux. Fast impressed web interface, and a excellent work of Winbox.exe loader to communicate with Mikrotik router, even without any ip number. I know they use some new netfilter modules and also mysqlLite to handle almost all parameters/settings. In web they use a heavy java framework to render all web stuff.

They have not publish any extension to Linux kernel. Or anything at all how all work. I will try to answer some of this  questions, examine how actual code work at least for myself.

Let’s start to look at boot sequence until finally fire up /sbin/init.

This version of Routeros 5.20, is using  Linux kernel 2.6.35   and uClibc-

Disk partions

File system for root is ext3 for version 5.x,6.x, older RouterOS have ext2. Boot partion have always ext2. Support for scsi disks exists in init code, but not supported by kernel ?


Examine /etc

In /etc/lilo.conf  have:


and modules.conf have:

depfile=/lib/modules/`uname -r`/modules.dep
path=/lib/modules/`uname -r`
path[pcmcia]=/lib/modules/`uname -r`
path[net]=/lib/modules/`uname -r`
path[ipv4]=/lib/modules/`uname -r`
path[misc]=/lib/modules/`uname -r

In /etc/rc.d we have

-rwxrwxr-x 1 root root 145 Aug 13 2012 rc.install
-rwxrwxr-x 1 root root 1687 Aug 13 2012 rc.mount
-rwxrwxr-x 1 root root 442 Aug 13 2012 rc.postinstall
-rwxrwxr-x 1 root root 365 Aug 13 2012 rc.start
-rwxr-xr-x 1 root root 1108 Aug 13 2012 rc.stop
-rwxrwxr-x 1 root root 2227 Aug 13 2012 rc.sysinit
drwxrwxr-x 2 root root 1024 Nov 28 2012 run.d

Some well knows parameter is settings to kernel and mount /ram, /sys, etc with rc.sysinit file:

# /etc/rc.d/rc.sysinit - run once at boot time

export TERM=linux

# Set the hostname.
/bin/hostname localhost

/bin/mount -t devpts devpts /dev/pts
/bin/mkdir -p /ram
/bin/mount -t tmpfs tmpfs /ram
/bin/mount -t sysfs sysfs /sys

if [ -x /sbin/nandfix ]; then
  /sbin/insmod flash >/dev/null
  /sbin/nandfix -sid
  if [ $? -eq 33 ]; then
    # reboot
    kill -INT 1
    echo "finishing upgrade"
    exit 1

# Clean out /etc.
/bin/rm -f /etc/mtab~ /fastboot /etc/nologin
/bin/rm -f /var/log/* /var/run/*
/bin/rm -f /var/lock/*
: >/var/run/utmp

#make device nodes that get modified
/bin/mknod /ram/ptmx c 5 2

# Cleanup tmp files
/bin/rm -rf /tmp/*

# Cleanup core files
/bin/rm -rf /core

# Turn on routing
echo "1" >/proc/sys/net/ipv4/ip_forward

# Do not remove all ips from interface when primary is deleted
echo "1" >/proc/sys/net/ipv4/conf/all/promote_secondaries

echo "1" >/proc/sys/net/ipv4/conf/default/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/default/arp_announce

# GRE keepalives
echo "1" >/proc/sys/net/ipv4/conf/all/accept_local

# enable first half of RPS
if [ -f /proc/sys/net/core/rps_sock_flow_entries ]; then
echo "8192" >/proc/sys/net/core/rps_sock_flow_entries

# Set maximum opened file limit
echo "32768" >/proc/sys/fs/file-max

# Set maximum join limit
echo "10000" >/proc/sys/net/ipv4/igmp_max_memberships

# Set maximum memory for socket options
echo "1024000" >/proc/sys/net/core/optmem_max

# Don't allow xfrm lookup to block
echo "1" >/proc/sys/net/core/xfrm_larval_drop

# allow 10 icmp error respones per second per host
echo "10" >/proc/sys/net/ipv4/icmp_ratelimit

if [ ! -f /BRANDED ] && [ "$board" != "vm" ]; then
 cp -f /nova/etc/url.orig /nova/etc/url
 cp -f /home/web/index2.html.orig /home/web/index2.html
 cp -f /nova/lib/console/logo.txt.orig /nova/lib/console/logo.txt
 cp -f /nova/etc/logo.orig /nova/etc/logo
 echo >/nova/etc/manual-url
 rm -rf /var/post/logo

# set initial hostname
if [ ! -f /etc/ident ]; then
  echo -n $(cat /nova/etc/logo) >>/etc/ident


# fix some perms
chmod a+x /var 2>/dev/null
chmod a+x /var/pdb 2>/dev/null
chmod a+x /var/pdb/system 2>/dev/null

exit 0

Linux INITial RAM Disk (initrd)

Mikrotik, Linux boot process stage1

Master Boot Record read a sector of 512 bytes,

In this stage sid=xxxxxxxxxxxxxxxxxxxxx value is included early in boot sequence. Later in stage2 this sid= value are located by init loader and processed stored in initrd.rgz.

Mikrotik, Linux boot process stage2

Mikrotik use Linux® initial RAM disk (initrd) for a temporary root file system in a standard way from boot directory. In boot directory exists vmlinuz, vmlinuz-smp, initrd.rgz, memtest.bin,map.



Unpack  initrd.rgz with:

gunzip -d -c < initrd.rgz > initrd

cpio -i –make-directories < initrd

tree -A give:

├── dev
│   ├── console
│   ├── hda
│   ├── hdb
│   ├── hdc
│   ├── hdd
│   ├── ram0
│   ├── sda
│   ├── sdb
│   ├── sdc
│   ├── sdd
│   └── xvda
├── init                        ; Disassembled in next 4 parts
└── sysroot