Openwrt Notes

Last modified by Leon Poon on 2019/11/02 17:53

 

initramfs-kernel

kernel runs /init.

/init is a script which sets INITRAMFS=1 and then execs /sbin/init.

/sbin/init is ELF comes from procd package. What it does (as pid=1):

  • early (different from procd early):
    • mount /proc, /sysfs, and create /dev nodes, create /tmp subdirs etc
    • set PATH
  • kmodloader in subprocess (waits until done)
  • preinit
    • plugd in subprocess (seem to deal with firmware)
    • /etc/preinit in subprocess
  • when /etc/preinit is done then it execvp the "real" /sbin/procd

procd states:

  • early (first state):
    • coldplug (remount /dev, etc)
      • start udevtrigger in subproc
      • when udevtrigger completes, hotplug
      • when hotplug completes, next state (ubus)
  • ubus:
    • start attempts to connect to ubs
    • start ubus as service
    • once connection's done then next state (init)
  • init - read /etc/inittab and run:
    • respawn - things that need to keep rerunning once quitted
    • askconsole
    • askfirst - the "Press Enter to activate this console" message
    • sysinit - (usually /etc/init.d/rcS with S) and then next state (running)
  • running - waits for shutdown signal (usr1/usr2) or reboot (int/term)
  • shutdown
    • runs shutdown command per inittab (also /etc/init.d/rcS but with K)
    • when done next state (halt)
  • halt - kill everything and either enter sleep loop or reboots

Modules

cp -va ./build_dir/target-*/linux-*/linux-4*/modules.builtin ./staging_dir/target-*/root-*/lib/modules/4*
# tar -C ./staging_dir/target-*/root-*/ -vcjf modules.tar.bz2 lib/modules

Debian

debootstrap --variant=minbase --include locales,sudo,sysvinit-core,sysvinit-utils,vim-nox,screen,wget,ntp,bridge-utils,ifupdown --exclude=nano,vim-tiny --foreign stretch $PWD
chroot $PWD debootstrap/debootstrap --second-stage
qemu-debootstrap --include=locales,sudo,sysvinit-core,sysvinit-utils,vim-nox,screen,wget,ntp,bridge-utils,ifupdown --exclude=nano,vim-tiny,systemd,systemd-sysv --arch mipsel stretch $PWD 

Do not install openssh-server during bootstrap otherwise it will bring in systemd!

Things to do after debootstrap:

  • get rid of systemd
  • mknod /dev/console
  • inittab serial console
  • dpkg-reconfigure locales 
  • set hostname
  • set root password (or .ssh authorized_keys)
  • fstab
  • /etc/network/interfaces
  • apt-get install apt-utils udev
  • apt-get install  --no-install-recommends openssh-server (check that it is without libpam-systemd and systemd!!)
  • apt-get install net-tools iputils-ping traceroute netcat file ntp openssh-server gcc lvm2
    • net-tools for ifconfig
    • gcc for?
  • echo vm.min_free_kbytes=16384 > /etc/sysctl.d/min_free_kbytes.conf
  • apt-get install kmod (depmod, etc)
  • kernel modules ( tar  --owner=0 --group=0  -C ./staging_dir/target-*/root-*/ -vcjf - lib/modules | ssh TARGET_IP tar -C /chroot -vxjf - )
  • depmod
  • inittab - serial console
  • read-only root

Needs CONFIG_DEVTMPFS=y in kernel.

mkdir -pv /mnt &&
mknod /dev/sda b 8 0 &&
cryptsetup open /dev/sda usb &&
vgchange -ay vol_grp1 &&
mount /dev/vol_grp1/logical_vol1 /mnt -o ro
chroot /mnt /bin/bash
mount proc /proc -t proc
exec switch_root /mnt /sbin/init
ip addr add dev eth0 IP/NM
ip link set eth0 up
ip route add default via GW

Network

sed -re 's/^boot_run_hook/#\0/g' -i /etc/preinit
PREINIT=1 . /etc/preinit
preinit_config_board    # set up the switch and lan ports on eth0.2, and create ip link
json_select switch
json_select switch0
json_add_boolean reset 0   # do not reset when setting up wan ports on eth0.1
json_select ..
json_select ..
preinit_config_switch switch0 eth0.1   # set up wan ports
preinit_ip_config eth0.1               # create ip link
ip address flush dev eth0    # del default address
for pi_ifname in eth0.1 eth0.2; do
    ip -4 address flush dev $pi_ifname    # del default address
   ip link set dev $pi_ifname down
done

UBI

define Build/append-ubi
        sh $(TOPDIR)/scripts/ubinize-image.sh \
                $(if $(UBOOTENV_IN_UBI),--uboot-env) \
                $(if $(KERNEL_IN_UBI),--kernel $(IMAGE_KERNEL)) \
                $(foreach part,$(UBINIZE_PARTS),--part $(part)) \
                $(IMAGE_ROOTFS) \
                $@.tmp \
                -p $(BLOCKSIZE:%k=%KiB) -m $(PAGESIZE) \
                $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE)) \
                $(if $(VID_HDR_OFFSET),-O $(VID_HDR_OFFSET)) \
                $(UBINIZE_OPTS)
        cat $@.tmp >> $@
        rm $@.tmp
endef