Mounting filesystem image from physical memory in C - linux

I'm booting linux with my own init executable which is written in C.
As a part of my init process, I want to mount a custom filesystem image which was preloaded to the physical memory (let's say at address 0xFFFF0000).
The type of the filesystem image can be changed to whatever will work, as far as it has POSIX (like ext4).
How can I mount the image successfully entirely from physical memory using C code?
What I've tried:
Mounting from /dev/mem (as seen here: http://www.qnx.com/developers/docs/qnxcar2/index.jsp?topic=%2Fcom.qnx.doc.neutrino.technotes%2Ftopic%2Fmultiple_Mounting.html), it doesn't fail but I don't see the wanted files from the image in the target directory. Moreover, this method uses char device so ext4 doesn't work since it need a block device.
Building another image file in the rootfs and filling it with the raw data from 0xFFFF0000 and mounting it. It's dumb and won't save changes to the image itself upon restarting.
Note: I've enabled loop devices for my system if needed

Related

What are the limitations of updating kernel and device tree while running elinux?

I am trying to prepare for an event where i need to update a kernel and device tree file remotely on an embedded Linux device. I know where these objects are saved in storage, but i am not sure if my current approach to update them is optimal or even stable.
Right now the i know that:
The kernel is stored in a FAT32 partition which i can mount and see the kernel file uImage.
The device tree is stored in a section of storage accessible via a block device /dev/mmcblk1boot0 and an offset and span.
So my questions are:
Assuming Linux is up and running, is updating the kernel and device tree as simple as overwriting the
uImage file (cp new-uImage uImage) and the data at the DTB offset
(dd if=new.dtb of=/dev/mmcblk1boot0 seek=<offset>)?
Does the Kernel look at these storage location after boot?
Are there more common ways to accomplish the same result?

How do I boot from an .hddimg file?

After running BitBake on several different recipe files, BitBake generates a file of type '.hddimg'. I haven't been able to find a clear explanation on what this file is used for, the closest that I have found is some speculation on the mailing list here. The author Paul states that:
the image isn't an image of a regular bootable system drive, but is a "live
image" of a smaller system that can either boot the real system from a
virtualized file system in RAM whose image is read from a single file in the
first level, or it can install the real system to a different drive.
The 'bootimg.bbclass' is what generates the .hddimg, and in the opening comments it is written that:
A .hddimg file [is] an msdos filesystem containing syslinux, a kernel, an initrd and a rootfs image. These can be written to harddisks directly and also booted on USB flash disks (write them there with dd).
Which appears to corroborate with what Paul wrote, but still leaves a lot of ambiguity on how to go about booting from this file (at least to a greenhorn like me).
Well, the doc says "write them there with dd". So:
dd if=/path/to/your/hddimg of=/path/to/raw/usb/device
so, if you have the file as my.hddimg and the usb flash disk appears as /dev/sdg
dd if=/home/karobar/my.hddimg of=/dev/sdg
As it's name implies, it's an image, so needs to be written as such. The actual file system is inside of the rootfs file, which is similarly an image!
Once you have that on the usb stick, the usb stick itself should be bootable. Depending on what you're trying to do this may not be the easiest kind of output from bitbake to work with.

Why is the bulk of the kernel image only ~1.5MB in RAM after loaded?

According to this picture from The Kernel Boot Process:
The bulk of the kernel image is only ~1.5MB. I thought the kernel image should be a fairy large binary image file in order to support the hardware (or is this kernel module?) and various kernel functions, since the source is pretty large. Also, where's the initrd?
First the kernel is compressed. Then in most desktop environment, the kernel is built with a minimal set of statically linked modules. The initrd image is loaded from the hard drive, from the /boot folder (see your grub configuration) and is used to detect your desktop hardware to load the appropriate modules, to be able to mount a root file system. The initrd image contains this sets of modules. Imagine a RAID, LVM or an exotic file system that cannot be recognized by the bootloader. This is why the boot partition is most of time in ext2/3/(4?). Then when the right modules are loaded, the root file system is swapped from initrd image with the one on the hard drive (pivot_root). Then the rest of the hardware modules are loaded from the "none initrd" filesystem, mostly with the help of udev.
Hope this helps!
P.S. Correct me if I'm wrong!

Direct copy Ramdisk image to Ramdisk device

Right, so I've got a Ramdisk image on a mounted device at, say, /mnt/sda1/Ramdisk.img . I want to copy the raw data directly to a ramdisk device at /dev/ram0, in such a way that there will be no need to use mke2fs: I could mount the image and device and find that both are identical.
Ideally this would use only linux commands.
My ideas so far: Mounting the image on a loopback and using basic IO to put the data from one device into the other. I could probably do this with C.
Also, somehow finding the physical address of the ramdisk device and using linux or C commands to put the file directly into the memory.
Thanks in advance.
P.S: I know that the kernel somehow flags memory which was used for ramdisks so the system does not reclaim it. Anyone know where these flags are?
Any kind of copy command is going to require a partition and filesystem at the destination. However, dd can work at the disk or partition level, by just specifying the disk (eg /dev/sda) or partition (eg (dev/sda1) level. So in theory, you could do
dd if=ramdisk_image_file of=/dev/ram0

Linux-Based Firmware, how to implement a good way to update?

I'm developing a linux-based appliance using an alix 2d13.
I've developed a script that takes care of creating an image file, creating the partitions, installing the boot loader (syslinux), the kernel and the initrd and, that takes care to put root filesystem files into the right partition.
Configuration files are on tmpfs filesystem and gets created on system startup by a software that reads an XML file that resides on an own partition.
I'm looking a way to update the filesystem and i've considered two solutions:
the firmware update is a compressed file that could contain kernel, initrd and/or the rootfs partition, in this way, on reboot, initrd will takes care to dd the rootfs image to the right partition;
the firmware update is a compressed file that could contain two tar archives, one for the boot and one for the root filesystem.
Every solution has its own advantages:
- a filesystem image will let me to delete any unused files but needs a lot of time and it will kill the compact flash memory fastly;
- an archive is smaller, needs less time for update, but i'll have the caos on the root filesystem in short time.
An alternative solution could be to put a file list and to put a pre/post update script into the tar archive, so any file that doesn't reside into the file list will be deleted.
What do you think?
I used the following approach. It was somewhat based on the paper "Building Murphy-compatible embedded Linux systems," available here. I used the versions.conf stuff described in that paper, not the cfgsh stuff.
Use a boot kernel whose job is to loop-back mount the "main" root file system. If you need a newer kernel, then kexec into that newer kernel right after you loop-back mount it. I chose to put the boot kernel's complete init in initramfs, along with busybox and kexec (both statically linked), and my init was a simple shell script that I wrote.
One or more "main OS" root file systems exist on an "OS image" file system as disk image files. The boot kernel chooses one of these based on a versions.conf file. I only maintain two main OS image files, the current and fall-back file. If the current one fails (more on failure detection later), then the boot kernel boots the fall-back. If both fail or there is no fall-back, the boot kernel provides a shell.
System config is on a separate partition. This normally isn't upgraded, but there's no reason it couldn't be.
There are four total partitions: boot, OS image, config, and data. The data partition is for user application stuff that is intended for frequent writing. boot is never mounted read/write. OS image is only (re-)mounted read/write during upgrades. config is only mounted read/write when config stuff needs to change (hopefully never). data is always mounted read/write.
The disk image files each contain a full Linux system, including a kernel, init scripts, user programs (e.g. busybox, product applications), and a default config that is copied to the config partition on the first boot. The files are whatever size is necessary to fit everything in them. As long I allowed enough room for growth so that the OS image partition is always big enough to fit three main OS image files (during an upgrade, I don't delete the old fall-back until the new one is extracted), I can allow for the main OS image to grow as needed. These image files are always (loop-back) mounted read-only. Using these files also takes out the pain of dealing with failures of upgrading individual files within a rootfs.
Upgrades are done by transferring a self-extracting tarball to a tmpfs. The beginning of this script remounts the OS image read/write, then extracts the new main OS image to the OS image file system, and then updates the versions.conf file (using the rename method described in the "murphy" paper). After this is done, I touch a stamp file indicating an upgrade has happened, then reboot.
The boot kernel looks for this stamp file. If it finds it, it moves it to another stamp file, then boots the new main OS image file. The main OS image file is expected to remove the stamp file when it starts successfully. If it doesn't, the watchdog will trigger a reboot, and then the boot kernel will see this and detect a failure.
You will note there are a few possible points of failure during an upgrade: syncing the versions.conf during the upgrade, and touching/removing the stamp files (three instances). I couldn't find a way to reduce these further and achieve everything I wanted. If anyone has a better suggestion, I'd love to hear it. File system errors or power failures while writing the OS image could also occur, but I'm hoping the ext3 file system will provide some chance of surviving in that case.
You can have a seperate partition for update(Say Side1/Side2).
The existing kernel,rootfs is in Side1, then put the update in Side2 and switch.
By this you can reduce wear leveling and increase the life but the device gets costlier.
You can quick format the partitions before extracting the tar files. Or go with the image solution but use the smallest possible image and after dd do a filesystem resize (although that is not necessary for readonly storage)

Resources