0

I know that they are two different things, but what is the difference between the actual Linux kernel and the rootFS file system especially in terms of location in memory and updates?

Regarding partitioning, why is it that the kernel and rootFS are nearly always on different partitions? Won't the kernel code be stored within the rootFS itself? So how are they on different partitions in memory?

Now regarding updating, I've been looking into an OTA update framework which claims to do a full kernel image update. It uses two separate partitions for rootFS. If there is a problem updating one rootFS partition, it can fall back to the working rootFS partition which makes sense. However, how is this actually updating the kernel tho? I don't understand.

Engineer999
  • 3,683
  • 6
  • 33
  • 71

1 Answers1

4

I know that they are two different things, but what is the difference between the actual Linux kernel and the rootFS file system especially in terms of location in memory and updates?

Kernel is usually one image file (like zImage). In ARM systems kernel also needs device tree file, but let's avoid it for now. RootFS, in turn, is a file system that contains all files in your /, like binaries (init, bash), config files (/etc), user home directory, and so on. Sometimes RootFs contains kernel image file, sometimes it doesn't, depends on your particular system.

Regarding partitioning, why is it that the kernel and rootFS are nearly always on different partitions? Won't the kernel code be stored within the rootFS itself? So how are they on different partitions in memory?

The key to your questions is to think about bootloader (like U-Boot or GRUB). Once you understand how OS boot process works in bootloader, the answer will automatically reveal itself. As you mentioned, different partitioning schemes exist, which leads to difference in boot process. Actually, there is a lot of different boot schemes out there. Let's review some of them, and hopefully it'll explain what you want to know.

  1. Kernel and rootfs on different partitions. In this case, bootloader usually reads kernel image to RAM, passing rootfs partition via kernel cmdline (via root= parameter). Then bootloader starts kernel execution, and kernel is mounting rootfs from partition specified in root= parameter.
  2. Kernel image is located inside rootfs. In this case, bootloader ought to know where exactly kernel image is located (e.g. in /boot/zImage). Bootloader knows rootfs FS format (e.g. ext4), reads /boot/zImage from rootfs to RAM. Then execution continues as in previous item.
  3. Kernel image and rootfs are passed via network (e.g. TFTP). In such case, sometimes, rootfs is being placed into RAM and mounted as ramdisk (from RAM). No persistent storage is used in such case, and any changes to rootfs will be lost after reboot. Another network case is when rootfs is mounted via NFS, then persistent storage on the server will be used (transparently viewed by the user).

Now regarding updating, I've been looking into an OTA update framework which claims to do a full kernel image update. It uses two separate partitions for rootFS. If there is a problem updating one rootFS partition, it can fall back to the working rootFS partition which makes sense. However, how is this actually updating the kernel tho? I don't understand.

In terms of updates, it's not that different which scheme to use, (1) or (2). What are you talking about is called (at least in Android) A/B Seamless Updates, meaning that two partitions (A and B) are used for storing the same image (e.g. old rootfs and new rootfs). You need to understand that it's ok to update just rootfs without kernel. There is a rule in kernel development that reads like this: "We don't break userspace". Which means you can rely on different versions of kernel to run the same userspace, or you can rely on one kernel version to run different userspaces.

So it's more like architecture question: do you want to update kernel in your system at all? If yes, then you need to provide two different partitions for kernel and two partitions for rootfs. Or alternatively you can put kernel image and rootfs in the same partition (for example see Android boot image format), and provide second partition for updates.

Sam Protsenko
  • 14,045
  • 4
  • 59
  • 75
  • Thanks. It's a very good answer. This part tho "You need to understand that it's ok to update just rootfs without kernel. There is a rule in kernel development that reads like this: "We don't break userspace" I'm guessing so, from the example i've been looking at for the OTA, if it claims to do a full kernel update by just updating the Rootfs partition, then it must be using scenario 2 that you mention - kernel is actually stored within the Rootfs – Engineer999 Jan 07 '19 at 22:52
  • Just another question. If the Rootfs is stored on a different partition to the kernel as in scenario 1 you mention, then how are we able to see the kernel code inside the Rootfs when mounted? Or are we? This confuses me. – Engineer999 Jan 07 '19 at 22:54
  • @Engineer999 In such case you won't be able to see kernel image file in mounted RootFS, of course. But why would you want to? Kernel will be in RAM, where it's supposed to be, and I don't see any particular reason why you'd like to read kernel image file in your working system. Also, you always can read kernel image from kernel partition using `dd` command, for example. Or if kernel partition is `ext4` partition, for example, you can just mount it and read kernel image file from there. – Sam Protsenko Jan 08 '19 at 12:18
  • But isn't the kernel first stored on a partition in flash? At bootup i guess the kernel will be copied into RAM, but will the complete kernel fit in RAM? Doesn't this get swapped in and out of the flash memory? I guess this is another discussion relating to virtual memory etc. ? – Engineer999 Jan 08 '19 at 13:16
  • Yep, all points you touched are relevant. Kernel image is stored in the flash memory. Bootloader reads it into RAM, where it's being executed by CPU. Kernel image is not that big, it can fit into RAM just fine (kernel modules is a different story though, hence they usually reside in rootfs). Kernel pages can't be swapped out. Even pages that can be swapped (user-space), they are swapping into swap partition, and from there -- back to RAM. Storage partition is not used during swapping process anyway. You can find relevant question about kernel pages swapping on SO. – Sam Protsenko Jan 08 '19 at 16:37
  • Ok thanks, Will the code for the kernel modules also be brought into RAM, or would the CPU just read the instructions from flash etc. ? What about userspace applications? Sorry, many questions )) I'm just keen to understand all the details. – Engineer999 Jan 08 '19 at 20:21
  • CPU can't execute instructions from regular flash memory (it's block-accessed, and CPU need to be able to access memory by words). CPU runs code only from RAM (almost always), and on very rare systems it can run code from something else (like NOR-flash). Kernel modules are basically files, so they must be stored on some FS, probably on RootFS in your case. Once you load kernel module (e.g. with `insmod` command), it will be added into kernel footprint in RAM, never swapped out. – Sam Protsenko Jan 08 '19 at 22:34
  • When we write to something in the Rootfs, we write directly to the flash memory? Is anything brought into RAM first? – Engineer999 Jan 09 '19 at 07:16
  • It's starting to resemble an interview :-D If actual partition from flash was mounted (not a ramdisk), then any write we do goes to the flash memory, but via temporary place in RAM called `buffers/caches` (see `free` command output). I suggest you searching for those kind of questions on SO, or ask a new question, for each one you'd like to know. The topic shifted too far from original point. – Sam Protsenko Jan 09 '19 at 10:20