search

Inspect or change a rootfs UBI Image file

If you want to take a look inside a UBI image file before flashing it or maybe change a single file inside the image then how do go about that?  I'll show you how to do it for GoFlex Home ubi images.

Background

Maybe you don't trust the firmware I made available with an admin account already set up and that's OK because you shouldn't trust random people on the internet. So you could download the stock firmware directly from the official
support.goflexhome.hipserv.com site (update: you can't do that any more as Seagate has redirected the link) and add your own ssh private key to SSH in with that (though you will still have to work out how to stop the web server redirecting to the Registration page).

Or maybe you just want to inspect a file inside the firmware without flashing it. That's the situation I was in where I wanted to confirm that /etc/sysconfig/network-scripts/ifcfg-eth0 was configured for dhcp rather than static IP in the firmware file I made available for download, without flashing it to my own GoFlex Home.


Procedure

You need a laptop with Linux (I use Ubuntu-Mate on an old eeePC laptop) and you will probably need to install mtd-utils. 
Commands are  in blue output is  in white 

(become root)
sudo -s

(install mtd-utils)
apt-get install mtd-utils (on Ubuntu Mate)

(simulate a NAND flash device with 3 partitions like the GoFlex Home)
modprobe nandsim first_id_byte=0x98 second_id_byte=0xda third_id_byte=0x00 fourth_id_byte=0x15 parts=8,48

(initialize the flash device partition mtd2)
flash_erase /dev/mtd2 0 0

Erasing 128 Kibyte @ f8e0000 -- 100 % complete 

(load the ubi image - change path to image and image filename to suit)
ubiformat /dev/mtd2 -s 512 -O 2048 -f path_to_image/image_filename.ubi

ubiformat: mtd2 (nand), size 261095424 bytes (249.0 MiB), 1992 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 2048 bytes
libscan: scanning eraseblock 1991 -- 100 % complete  
ubiformat: 1992 eraseblocks are supposedly empty
ubiformat: flashing eraseblock 1393 -- 100 % complete  
ubiformat: formatting eraseblock 1991 -- 100 % complete 

(load the ubi kernel module)
modprobe ubi

(attach the device mtd2 to the ubi device)
ubiattach /dev/ubi_ctrl -m 2 -O 2048

UBI device number 0, total 1992 LEBs (252936192 bytes, 241.2 MiB), available 0 LEBs (0 bytes), LEB size 126976 bytes (124.0 KiB)

(mount the ubi filesystem - if the mountpoint doesn't already exist create it first with mkdir /mnt/ubifs)
mount -t ubifs /dev/ubi0_0 /mnt/ubifs


Now you can inspect and modify the firmware rootfs files in /mnt/ubifs.

To save any changes to a new image file follow the procedure on "How to make your own GoFlex Home firmware backup" from  the "make a zlib compressed ubifs image in the Downloads folder" step.