hakk

software development, devops, and other drivel
Tree lined path

Install OpenWRT x86 64 Using an A/B Partition Setup

A quick reference for OpenWRT Installation

Prepare a bootable Debian USB

You can use another distro if you’d like. I use Debian as a personal perference and also because it’s relatively quick to create a live USB.

Download a “standard” Debian ISO image from here https://cdimage.debian.org/debian-cd/current-live/amd64/iso-hybrid/.

At the time of writing, I’m using debian-live-12.2.0-amd64-standard.iso file. You may see a more recent version, but make sure you download the “-standard.iso” file. After downloading be sure to verify against the SHA256SUMS or SHA512SUMS.

Flash the image to a USB stick using dd

Before flashing make sure that the correct disk is selected.

fdisk -l

My USB is located at /dev/sdb so I’m going to write the image there:

dd if=/debian-live-12.2.0-amd64-standard.iso of=/dev/sdb bs=4M; sync;

Connect your Debian USB and boot

Be sure to connect your local machine to the machine that will be booting the Debian live USB and boot into the system.

Once in there install a few tools that will be needed:

# first switch from user to root
sudo su 
# update the available packages
apt update
# install some needed tools
apt install wget parted

Download OpenWRT image onto the Debian USB

Find the latest release that works for your board https://downloads.openwrt.org/releases/

In this example I will be using 22.03.05 and installing on a virtual machine.

wget https://downloads.openwrt.org/releases/23.05.0/targets/x86/64/openwrt-23.05.0-x86-64-generic-ext4-combined.img.gz

Flash the SSD drive with the OpenWRT image

In this step the combined ext4 OpenWRT image will be flashed to the disk drive; which will create two partitions. In the following steps I’ll show how to upgrade.

You’ll want to make sure that you find the proper disk to install on. If you’re on a physical device it’s likely your drive will be /dev/vda. But it’s easy to check:

root@debian:~# fdisk -l
Disk /dev/vda: 25 GiB, 26843545600 bytes, 52428800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop0: 980.32 MiB, 1027944448 bytes, 2007704 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

From the above step I can see that I need to install on /dev/vda.

root@debian:~# gzip -dc openwrt-22.03.5-x86-64-generic-ext4-combined.img.gz |dd of=/dev/vda bs=4M status=progress; sync;

You should see some output similar to:

gzip: openwrt-22.03.5-x86-64-generic-ext4-combined.img.gz: decompression OK, trailing garbage ignored
0+3855 records in
0+3855 records out
126353408 bytes (126 MB, 120 MiB) copied, 0.614046 s, 206 MB/s

Resize the OpenWRT partition

Check the current partition layout:

root@debian:~# parted /dev/vda print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 26.8GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      262kB   17.0MB  16.8MB  primary  ext2         boot
 2      17.3MB  126MB   109MB   primary  ext2

Depending on your disk size you’ll want to adjust the following command to only use a portion of your disk.

Resize vda2:

parted /dev/vda resizepart 2 10G
Information: You may need to update /etc/fstab.

You can print the partitions again to verify the correct sizing.

root@debian:~# parted /dev/vda print                                      
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 26.8GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      262kB   17.0MB  16.8MB  primary  ext2         boot
 2      17.3MB  10.0GB  9983MB  primary  ext2

Resize the filesystem on vda2

root@debian:~# resize2fs /dev/vda2 
resize2fs 1.47.0 (5-Feb-2023)
Resizing the filesystem on /dev/vda2 to 2437182 (4k) blocks.
The filesystem on /dev/vda2 is now 2437182 (4k) blocks long.

Create an additional partition

Next we’ll create an additional partition this will make upgrades easier.

root@debian:~# fdisk /dev/vda

Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 
First sector (33280-52428799, default 19531776): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (19531776-52428799, default 52428799): +15G

Created a new partition 3 of type 'Linux' and of size 15 GiB.

Command (m for help): p
Disk /dev/vda: 25 GiB, 26843545600 bytes, 52428800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbf7a017b

Device     Boot    Start      End  Sectors  Size Id Type
/dev/vda1  *         512    33279    32768   16M 83 Linux
/dev/vda2          33792 19531250 19497459  9.3G 83 Linux
/dev/vda3       19531776 50989055 31457280   15G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

All set, let’s boot into OpenWRT.

Upgrade

Let’s upgrade to a newer version! To 23.02.0 in this example:

After connecting to the router; we’ll have to make sure that a few packages are installed:

opkg install resize2fs fdisk blkid parted wget diffutils

After there packages are installed, let’s pull down the rootfs.

wget https://downloads.openwrt.org/releases/23.05.0/targets/x86/64/openwrt-23.05.0-x86-64-generic-ext4-rootfs.img.gz

Once downloaded and verified; write the root FS to /dev/vda3

gzip -dc openwrt-23.05.0-x86-64-generic-ext4-rootfs.img.gz | dd of=/dev/vda3 bs=4M; sync;

Output:

0+3324 records in
0+3324 records out
root@OpenWrt:~# resize2fs /dev/vda3
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/vda3 to 3932160 (4k) blocks.
The filesystem on /dev/vda3 is now 3932160 (4k) blocks long.

Let’s mount /dev/vda3 and copy or update the config files as needed:

mount /dev/vda3 /mnt
cd /mnt/etc

Next, using diff you can compare your config files and update on the new root FS.

Using sysupgrade it’s possible to get a list of config files that would be backed up normally. This list is controlled by /etc/sysupgrade.conf.

sysupgrade -l

Get a quick idea of all file differences?

sysupgrade -l | awk '{print "diff " $1 " /mnt"$1}' > backup-list
.txt

Get the new kernel:

wget https://downloads.openwrt.org/releases/23.05.0/targets/x86/64/openwrt-23.05.0-x86-64-generic-kernel.bin -O /boot/vmlinuz-23.05.0

Navigate into the /boot/grub directory to update the grub.cfg file to enable booting the updated kernel and the updated partition.

Get the partition id:

root@OpenWrt:/boot/grub# blkid /dev/vda3
/dev/vda3: LABEL="rootfs" UUID="ff313567-e9f1-5a5d-9895-3ba130b4a864" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="bf7a017b-03"

Here’s the full file:

Leave default set to 0. I’ve shortened the timeout to 2.

serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1 --rtscts=off
terminal_input console serial; terminal_output console serial

set default="0"
set timeout="2"
set root='(hd0,msdos1)'

menuentry "OpenWrt 23.05.0" {
   linux /boot/vmlinuz-23.05.0 root=PARTUUID=bf7a017b-03 rootwait   console=tty0 console=ttyS0,115200n8 noinitrd
}
menuentry "OpenWrt" {
   linux /boot/vmlinuz root=PARTUUID=bf7a017b-02 rootwait   console=tty0 console=ttyS0,115200n8 noinitrd
}
menuentry "OpenWrt (failsafe)" {
   linux /boot/vmlinuz failsafe=true root=PARTUUID=bf7a017b-02 rootwait   console=tty0 console=ttyS0,115200n8 noinitrd
}

Finally, reboot into the upgraded system.