1

I'm fairly new to Linux and especially Embedded Linux.

I'm working on an already-developed project on an embedded device where the root filesystem type is SquashFS, the compressed read-only file system type.

When the kernel boots up, how does it know how to find and mount the root file system, especially when it is of SquashFS type, and not the normal EXT4 type? Thanks in advance.

2

There's two ways to do this:

  • You can pass the root device name to the kernel as a parameter (root) and the kernel must have (builtin) all the required modules to locate the storage device, identify and mount the file system for launching init. You can instruct the kernel which file system it is (with rootfstype) but I am not certain this is required;

  • Nowadays, the vast majority of systems use an initramfs, which is a small file system image glued to the kernel (or referenced by the boot loader) that prepares (if necessary) and mounts the root file system in userland before passing control to the init process in the actual root file system after executing a switch_root operation;

  • That's the part I don't get regarding initramfs. We still have to customize the initramfs and tell it how to mount the main root file system right? – Engineer999 May 29 '20 at 11:11
  • Yes, initramfs is userspace, so you're supposed to make it do what you need to switch into the root file system. In this environment you can pick up information passed on to the kernel, so you can use command line arguments on the bootloader and pick them up in the initramfs. – Pedro May 29 '20 at 12:22
  • Your build/deployment system should, produce or customise the initramfs as well as the kernel and root file system. – Pedro May 29 '20 at 12:24
1

There is a concept Magic Number which you can refer to.

list of magic numbers defined at magic.h header file

For example if you check the disk:

sudo file -Ls /dev/mapper/vg-home               
/dev/mapper/vg-home: Linux rev 1.0 ext4 filesystem data, UUID=fd8eb0e8-073d-4ae3-810a-87e8af0981d7 (needs journal recovery) (extents) (64bit) (large files) (huge files)

The file command match the type based on magic numbers. You can see these values by hexdump tools:

sudo hexdump /dev/mapper/vg-home -C  | grep "53 ef"
00000430  1b da d0 5e 49 00 ff ff  53 ef 01 00 01 00 00 00

here 0x53ef is the magic number of ext4 filesystem

For squashfs (magic number 0x73717368)

sudo hexdump /dev/somedisk -C  | head -n 1    
0000000  73 71 73 68 00 00 14 7e  01 ea 27 0d 00 01 00 00 |sqsh...~..'.....|
  • Thanks. So does the kernel by default always support squashfs? – Engineer999 May 29 '20 at 12:34
  • @Engineer999 SquashFS is distributed as a Linux kernel source patch (which enables SquashFS read support in your kernel), the mksquashfs tool, which creates squashed file systems (in a file or on a block device) and the unsquashfs tool, which extracts multiple files from an existing squashed file system. – binarysta May 29 '20 at 12:42
  • This is the explanation for how the kernel works out which filesystem it is looking at. – Pedro May 29 '20 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.