Edit files in .itb

Apolgies if this answer exists - I don't even know what to search for and my searches aren't revealing to me.

I have an image - it is name.itb.

I want to extract the root squashfs and modify a configuration file and the put the root squashfs back (replace it) in the itb.

I am using dd to do this along with binwalk.

When I put it back together - sysugrade throws the dreaded metadata missing error.

How can I figure out where the metadata is in the orignal itb and make sure I put it back correctly on reassembly?

Specifically I am doing this:

offset=$(binwalk $input_fname | grep 'Squashfs filesystem' | awk '{print $1}')
size=$(binwalk $input_fname | grep 'Squashfs filesystem' | awk -F ', size: ' '{print $2}' | awk -F ' bytes' '{print $1}')

dd if="$input_fname" of="$output_fname" bs=1 skip="$offset" count="$size"
unsquashfs -d "$unsquashed_dir" "$output_fname"

modify my configuration file in /etc/config/whateverfile

put it back together

offset=$(binwalk $input_fname | grep 'Squashfs filesystem' | awk '{print $1}')
size=$(binwalk $input_fname | grep 'Squashfs filesystem' | awk -F ', size: ' '{print $2}' | awk -F ' bytes' '{print $1}')
blocksize=$(binwalk $input_fname | grep 'Squashfs filesystem' | awk -F ', blocksize: ' '{print $2}' | awk -F ' bytes' '{print $1}')

sudo mksquashfs "$unsquashed_dir" "$squashfs_fname" -b $blocksize -comp xz

dd if="$input_fname" of="$header_fname" bs=1 count="$offset"
dd if="$input_fname" of="$footer_fname" bs=1 skip=$(($offset + $size))

cat "$header_fname" "$squashfs_fname" "$footer_fname" > "$output_fname"

There's no to extract or edit files contained in a binary image such as yours and safely reassemble it for use.

Bummer - so what could be a strategy to have a unique thing in a config file for each router the firmware is deployed to? Certainly we wouldn't want to build it each time for each router?

You could use the Firmware Selector to add your unique configs.

See: https://firmware-selector.openwrt.org

More information here:

and here:

Actually, that was the original option before the Firmware Selector. I think there's a [local] image builder that works as well - but it requires download and setup on a local machine.

1 Like

Awesome thanks for the detailed replies. I think those options are interesting.

Is it true that both of those options rely on a static file inside the itb file itself?

So just like my file I want to change after image build it just can't be done. Would that be an accurate statement?

I ask because the example in one case -

set dhcp.@host[-1].mac='a1:b2:c3:d4:e5:f6'

Would be something I personally wouldn't build every router I build with the same dhcp host mac. So maybe it is just a bad example - or I don't understand something correctly yet.

Yes, the compiled image can't be changed.
A major reason is that the file system is usually compressed, so you should first expand it, then modify it and then repack it and finally again cook up the final image using exactly the necessary compression formats and possible IDs and the OpenWrt metadata (calculated correctly after the changes).

In practice we are talking about a full rebuild of the image. (Not recompiling all packages, but just the final image itself.)

1 Like

Well for what it is worth - the firmware selector is very cool and I am thankful to those who built it. I have a few more questions but they seem distantly related so I'll close this thread as complete with the latest answer. Appriciate your inputs!

If you have several similar routers that you want to burn a bit different tailored settings into firmware, you might look into the imagebuilder and "custom files" (or even the full toolchain and custom files).

It would be pretty easy to have an otherwise compiled components ready, and then just have a script that loops for each router

  • first tweaks the included config file for a router,
  • then runs the imagebuilder with that config file included,
    then loops to the next router.


This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.