4

I'm trying to alter some files inside an ".img" file.

Example:

logo-x.squashfs.img
romfs-x.squashfs.img
safeEnv.img
sign.img

For that, I extracted the ".img" using 7-Zip and made the changes needed. Archiving these altered files with 7-Zip is not possible though, so I searched a tool to do that but could not find one that is capable of creating the ".img" as it was originally.

I tried to use ImgBurn but it seems to create a different file from the original one. Also tried to use DD but could not make it work with files instead of volumes.

Please, how could I read the original ".img" file to learn its format and create a new one from the new files?

3 Answers 3

1

While you discovered that 7-Zip can turn squashFS images into individual files, the canonical tool to do so would probably be rdsquashfs:

# Unpack image `foo.img' into directory `foo':
rdsquashfs --unpack-root foo --unpack-path / foo.img

Then, after altering files within foo, you can repack using mksquashfs:

mksquashfs foo foo-updated.img

I believe using mksquashfs as above may lose some of the metadata from the original image file. If this matters for your use case, consider reading a description from the original image, and using that as a reference when creating the new image:

# Unpack image `foo.img' into directory `foo':
rdsquashfs --unpack-root foo --unpack-path / foo.img
# Read description from image `foo.img' to file `foo.desc`:
rdsquashfs --describe foo.img >foo.desc

# Create image `foo-updated.img' from directory `foo' and description:
gensquashfs --pack-file foo.desc --pack-dir foo foo-updated.img
0

If you do...

file logo-x.squashfs.img

And also...

sudo fdisk logo-x.squashfs.img

Then 'p' to print the partition information. That can be some help.

Also it might be better to just mount the .img file and add files to whatever partition in there you need to?

lsblk
sudo losetup --find logo-x.squashfs.img
lsblk

Now you can see which device block the .img file is at (e.g: /dev/loop6)

sudo mount /dev/loop6 /mnt
1
  • SquashFS filesystems are read-only, so mounting using a loop device is unlikely to be useful when the goal is to modify the image. Commented May 27, 2021 at 20:04
0

It is possible to create a QEMU machine, then load those images into the QEMU machine, where they will appear as a disk. Manipulate the files on that disk at will, and they will stay like that when you unmount the disk.

For squashes images, the process will be a bit different. You could use the unsquashfs tool. See this for detailed information.

Basically it's like this (for squashfs filesystems):

unsquashfs /live/image/livefs.squashfs
touch modified-this.file
mksquashfs squashfs-root/ livefs.squashfs -noappend -always-use-fragments
1
  • Have you actually tried this? As far as I know, SquashFS images are read-only, so mounting them, be it in an emulator or elsewhere, would not allow editing them. Commented May 28, 2021 at 21:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.