Slashdot Mirror


Discover the Anatomy of initrd

IdaAshley writes "The Linux initial RAM disk (initrd) is a temporary root file system that is mounted during system boot to support the two-state boot process. It contains various executables and drivers that permit the real root file system to be mounted, after which the initrd RAM disk is unmounted and its memory freed. In this article explore the initial RAM disk for Linux 2.6, including its creation and use in the Linux kernel. In many embedded Linux systems, the initrd is the final root file system."

41 comments

  1. Great tutorial by gigne · · Score: 5, Informative

    This looks to be a pretty good tutorial.

    As well as mkinitrd, there are some cool tools coming along that help build an initrd.
    Here is one I have used, and although it's very early in it's development cycle.
    http://yaird.alioth.debian.org/

    Are there any more that are actually easy to use?

    --
    Signature v3.0, now with 42% less memory usage.
    1. Re:Great tutorial by Crussy · · Score: 3, Informative

      Arch linux is beginning to use Mkinitcpio. It is for the most part easy to install and use.

    2. Re:Great tutorial by kuitang · · Score: 1

      Much easier and leaner than the original mkinitcpio from my experience.

      --
      Don't believe in miracles -- rely on them.
    3. Re:Great tutorial by lintux · · Score: 1

      Also, for at least the initrds meant for embedded purposes, buildroot can be nice.

  2. More trouble... by misleb · · Score: 3, Interesting

    Is it just me, or does maintaining initrd seem like more trouble than it is worth? I guess it is good for generic distributions which want to support every hardware config under the sun and keep the kernel small, but if you know the (set of) hardware you are going to be running a given kernel on, you might as well just blow that initrd crap away. Whenever I recompile a kernel, initrd is the first thing to go. All you need in the kernel to make initrd irrelevent is disk and filesystem (and maybe software RAID modules) support. You can keep everything else as modules.

    -matthew

    --
    "THERE IS NO JUSTICE, THERE IS ONLY ME." -Death
    1. Re:More trouble... by Anonymous Coward · · Score: 0

      > Whenever I recompile a kernel, initrd is the first thing to go.

      Ditto. Why hassle with crap you don't need to deal with? Everything in the article looks like a big pain in the ass.

    2. Re:More trouble... by Anonymous Coward · · Score: 0

      Me too. Although I keep wondering if I'm missing on some cool debian functionality by not using the default initrd setup. Anyone know?

    3. Re:More trouble... by doti · · Score: 2, Informative

      If you want to know better, install a LFS system.
      I did it once, and even used it on my machine at home for many months, before I went back to Slackware.

      It's really instructive, if you want to know how Linux is organized.
      I, for one, feel unconfortable having lots of software running in my system that I don't know what it is, what's their purpose, and if I can turn it off nor not. LFS made me know which is which. Also, it gives great understanding of the boot process.
      I highly recommend it: http://www.linuxfromscratch.org/

      --
      factor 966971: 966971
    4. Re:More trouble... by Anonymous Coward · · Score: 0

      # Create the init file
      cat >> /mnt/initrd/linuxrc EOF
      #!/bin/ash
      echo
      echo "Simple initrd is active"
      echo
      mount -t proc /proc /proc
      mount -t sysfs none /sys /bin/ash --login
      EOF

      ash? do they mean bash?

      I think he meant 'mount -t proc none /proc'

      and ash again...

      Personally, I don't need an initrd. I could use one if I wanted, but the whole point of compiling the kernel is to get hardware specific. If I were to write a LiveCD, I'd definately use an initrd of some fashion. My experiences with initrd is that is slows down the boot process by about 10-15 seconds.

    5. Re:More trouble... by munpfazy · · Score: 1
      Is it just me, or does maintaining initrd seem like more trouble than it is worth? I guess it is good for generic distributions which want to support every hardware config under the sun and keep the kernel small, but if you know the (set of) hardware you are going to be running a given kernel on, you might as well just blow that initrd crap away. Whenever I recompile a kernel, initrd is the first thing to go. All you need in the kernel to make initrd irrelevant is disk and filesystem (and maybe software RAID modules) support. You can keep everything else as modules.


      That's certainly my impression. Anyone know of any reason why someone might want to use initrd on a stock system (eg. x86 desktop machine with hard drives)?

      I've always just put appropriate filesystem support into the kernel and skipped it.

      I suppose if you wanted to encrypt your root directory or do some exotic network booting it might be useful - but for an ordinary desktop it just seems like one extra step without purpose.
    6. Re:More trouble... by xiox · · Score: 2, Interesting

      We use initrd do to diskless booting. The initrd does dhcp and mounts an nfs partition as root and switches over to that. This is quite cool as the initrd can be quite intelligent, for instance using different root disks depending on which computer it runs on.

    7. Re:More trouble... by squiggleslash · · Score: 2, Informative

      ash is a self contained Bourne shell clone which is easily statically linked. bash is (a) relatively large (most compiled versions wiegh in in the megabytes) and generally not usually that self-contained. So yeah, he probably did mean ash, not bash.

      The initrds I've seen have used other shells such as nash (RedHat/Federoa) and dash (recent Debian kernels), bash really is overkill, especially when you consider you want the initrd to be as small as possible.

      --
      You are not alone. This is not normal. None of this is normal.
    8. Re:More trouble... by sjames · · Score: 1

      Whenever I recompile a kernel, initrd is the first thing to go. All you need in the kernel to make initrd irrelevent is disk and filesystem (and maybe software RAID modules) support.

      That's going to be harder and harder to do. For example, one of these days, in-kernel RAID detection is going away in favor of a userspace tool (in initrd). It's already that way with LVM.

      This makes good sense in general in light of the trent towards more and more hot-plugable devices. For instance, RAID. Without hot-plug, it makes perfect sense to do the probing in kernel inside __init code which is thrown out once the kernel init is complete. However, with hot-plugging, it may be necessary to detect new RAID devices at any time. We can either keep the init code in RAM perminantly (you can't swap out parts of the kernel), duplicate functionality by doing the job in-kernel initially and then depending on userspace to do exactly the same thing for hot-plugged devices, or just let userspace handle it and use an initrd to deal with having the root fs on RAID.

      As others have pointed out, there are other cases where device setup is WAY more complex than anything we would want to put in the kernel.

      Using initrd/initramfs allows booting to be as simple or complex as necessary. In most cases, everything can be in initramfs. In very complex cases, booting can be done in multiple steps where the initramfs provides just enough to get the next boot stage mounted up and ready.

    9. Re:More trouble... by jesboat · · Score: 1

      As this post's sibling has pointed out, ash, the minimalist shell component of BusyBox, is what the article's author intended to use.

      Furthermore, the proc filesystem, like most (all?) virtual filesystems, doesn't care what the device name is. Most people use "none", for clarity, but /proc should work just as well.

    10. Re:More trouble... by jesboat · · Score: 1

      initrd's are also useful if you want to use fancy disk formats (RAID/LVM/EVMS/etc.) Some desktop users will want to use an initrd.

      The point of an initrds (now, at least) is to pull out the work of finding the root filesystem from the kernel and get it into userspace, which is much easier to maintain. Eventually, even "regular" root devices/filesystems will be mounted by an initrd, and the kernel will be that much smaller and more maintainable.

      (I'm using "initrd" as a synonym for "initramfs", even though they're not actually synonymous.)

  3. Re:OMFG! by misleb · · Score: 0, Offtopic

    You're still a coward.

    --
    "THERE IS NO JUSTICE, THERE IS ONLY ME." -Death
  4. Initramfs? by qbwiz · · Score: 2, Interesting

    I thought that initramfs was the hotness? Why would we want to use initrd over initramfs (or vice versa, for that matter)?

    --
    Ewige Blumenkraft.
    1. Re:Initramfs? by fimbulvetr · · Score: 4, Informative

      You're right. Initramfs is better in many ways to initrd, and doesn't have the crazy limitations.

      Initramfs, IIRC, has these advantages:

      It doesn't need block level drivers compiled into the kernel to read itself like initrd does.
      It uses the kernel cache area as its file storage area so you don't have to allocate space with a ramdisk.
      The mem it used to store files with temporarily can be returned to the kernel after the kernel has booted.
      No artificial size limit.

      All in all, it's a much better alternative.

    2. Re:Initramfs? by KagatoLNX · · Score: 1

      Well, initramfs is very awesome. However, you can only have one initramfs per kernel, so initrd allows for a bit more modularity.

      I think it depends on your application (and bootloader), really.

      --
      I think Mauve has the most RAM. --PHB (Dilbert Comic)
    3. Re:Initramfs? by DarkDust · · Score: 2, Informative

      It has another advantage: it's compiled into the kernel, so you only need to load one image (good for embedded systems).

      It has a huge disadvantage: it's compiled into the kernel, so you can't easily change it (bad for desktop systems).

    4. Re:Initramfs? by Wonko+the+Sane · · Score: 1

      I may be wrong, but I think that newer kernels always use an initramfs even if you don't realize it. If you search the linux-kernel archives for "early userspace" they talk about moving kinit into userspace. At some point in the future most (all?) of the boot code will be moved into userspace. The plan seems to be that the kernel build a default initramfs for you that is functionally identical to the current behavior. If you want to you can customize it via various methods.

    5. Re:Initramfs? by sjames · · Score: 1

      An initramfs CAN be compiled into the kernel, but usually isn't. Normally, grub loads the gzipped cpio into ram and points to it just like a gzipped ramdisk. The kernel uncompresses it into the ramfs and releases the memory used for the archive.

      On Fedora and others, the mkinitrd utility actually creates an initramfs these days. Try zcat /boot/initrd-whatever | cpio -it.

  5. More More More!!! by Anonymous Coward · · Score: 5, Insightful
    More of these types of articles on Slashdot, please. This is "News I Can Use".



    Stick to our techie roots.

    1. Re:More More More!!! by Anonymous Coward · · Score: 0

      I agree,I love seeing things that I use everyday.

  6. F*ck that, give us more politics! by spun · · Score: 3, Funny

    We want more politics, thinly disguised corporate press releases, and pseudoscience articles, please!

    --
    - None can love freedom heartily, but good men; the rest love not freedom, but license. -- John Milton
  7. Nontrivial Boot Environments by KagatoLNX · · Score: 3, Interesting

    There are definitely times where it is required.

    Take, for example, booting onto a root on a SAN--specifically a Coraid Ethernet SAN.

    During boot, you must bring up the ethernet interface, load the AoE module, probe for the SAN, wait for it to quiesce, then enable clustering, join the cluster, join the fence domain, allow the cluster to quiesce, load the DLM, enable CLVM, probe and activate everything, then mount your root GFS (or OCFS2 if you can get that working).

    This would not be possible without a good initrd.

    Also, not so obviously, distros aren't the only people who deploy a single kernel on lots of hardware. When you have 300 old Dells, 150 old gateways, 75 custom built boxen, and a handful of laptops, maintaining a single kernel and initrd beats maintaining ten of them. Not your use case, but definitely important.

    --
    I think Mauve has the most RAM. --PHB (Dilbert Comic)
    1. Re:Nontrivial Boot Environments by Anonymous Coward · · Score: 0

      If you're netbooting, I can see why you'd need to pass down an initrd to get started, but otherwise why do you have to have an initrd at all? Why can't Linux just boot and use the kernel modules in the /lib/modules/whatever directory, like it does when it's booted and running? I hate having to make initrd's for debian and such just to get SATA drivers to work at bootup.

    2. Re:Nontrivial Boot Environments by someone300 · · Score: 1

      Well, you need to have support compiled into the kernel to be able to mount the root filesystem before you can actually load the modules. If IDE Hard drive, ext3 and whatever is compiled into the kernel and it can mount / and load everything else from /lib/modules/... but I'm guessing that Debian therefore has SATA support compiled as loadable modules.

      The reason an initrd works is because it's the bootloader's responsibility to load the initrd, and one would hope that the bootloader has support for the hard drive it's running from.

    3. Re:Nontrivial Boot Environments by Eivind · · Score: 1
      There are definitely times where it is required.

      Yes. But those "times" are unlikely to add up to even 0.1% of the times where initrd are employed.

      All distros I know off set up a initrd-thing even for bog-standard root on locally attached (ide|sata|scsi) devices.

      I don't see the point of it. Yes. It makes perfect sense in some scenarios. It does however *not* make any sense whatsoever in most standard, simple, cases. Why it's nevertheless done beats me.

    4. Re:Nontrivial Boot Environments by Anonymous Coward · · Score: 0

      Restoring backups to different hardware is another case in point where an initrd is the best option. Think beyond the desktop more towards enterprise systems.

    5. Re:Nontrivial Boot Environments by KagatoLNX · · Score: 1

      Not with initramfs, actually. Cramfs is also quite tiny.

      --
      I think Mauve has the most RAM. --PHB (Dilbert Comic)
    6. Re:Nontrivial Boot Environments by someone300 · · Score: 1

      To support them at the time the kernel is loaded they still need to be enabled in the .config (CONFIG_CRAMFS). Initramfs afaik is always used in the kernel since it contains the code for finding the root partition. Modification of this needs to be done at compile time and so can be a bit difficult for most times where an initrd would be simple.

      Just like anything else that is supported at boot, it is actually compiled into the kernel.. in theory it could contain loadable modules too but I think that would be a real pain to set up. If CONFIG_CRAMFS was a module, for example, you'd need to use an initrd to load it if your root filesystem was of type cramfs.

  8. initramfs vs. initrd by ColonelPanic · · Score: 2, Interesting

    We use both in our port of Linux to proprietary Cray architectures.

    Initramfs is synthesized from the stock page and dentry caches. The nice thing about
    initramfs is that RAM gets released to the dynamic pool when an inode is deleted. So
    it's a good choice for files that need to be present at boot time but not kept around,
    like kernel modules. The bad thing: each file occupies at least one page, and that's
    64KiB here. An initramfs with lots of little files can waste beaucoup bytes.

    RAM "disks" are devices that occupy statically allocated memory. Storage doesn't get
    reclaimed, but no storage is wasted by rounding up file sizes. An initrd is an image
    of a RAM disk, possibly compressed.

    Note that a cpio archive, possibly compressed, that's loaded in place of an initrd image
    gets automatically
    expanded and copied into the initramfs.

    Confusingly, the kernel also has its own compressed cpio archive within its own text containing
    the initial content of the initramfs. At a minimum, it holds /, /dev, and /dev/console.

    If you're using an architecture with small pages (read: x86), use initramfs. Otherwise,
    you may need a clever hybrid solution, such as populating initramfs from a compressed cpio
    archive and then moving some of its content into a RAM disk.

    --
    "Skill shows through where genius wears thin." -Wittgenstein || Religion: uniting aviation and architecture.
    1. Re:initramfs vs. initrd by RedDirt · · Score: 1
      Confusingly, the kernel also has its own compressed cpio archive within its own text containing the initial content of the initramfs. At a minimum, it holds /, /dev, and /dev/console.
      Interesting! I didn't realize that the kernel provided that. Neat!
       
      I read somewhere else that if you're using cpio format archives, you can provide a list of them to the bootloader they'll all be extracted to the initramfs. (syslinux at least claims this - I don't know about others and I haven't tried it personally on any of 'em.) That sounds to me like an interesting way to patch or enhance the functionality of a "stock" initrd without having to re-roll it. Have you (or any one of the legion of people brighter than I) played with that any?
      --
      James
  9. Options for fast booting by wall0159 · · Score: 5, Interesting


    I don't know much about this, but am curious.

    Why, once the system is booted cleanly, can't it save its state in initrd, and just load the state the next time without going through the boot process? Would that result in fast booting?

    1. Re:Options for fast booting by LiquidFire_HK · · Score: 2, Insightful

      I don't know much about it, either, but I will attempt to answer your question.

      Most of the boot time is "wasted" in loading modules into memory, loading needed services (daemons, scripts to set up the environment, etc.). The time required to load up the initrd image is minimal, and few other things are "created" during boot (maybe /dev device nodes, but I think those are also saved in a tarball).

      What you are proposing would offer little speed increase: you still have to load everything into memory. And if you propose saving what's in memory to an image to be loaded on boot, this already exists.

  10. Mkinitramfs by Sensible+Clod · · Score: 1
    Mkinitramfs looks good. I've used it on FC5 on an x86-64, and it works out of the box*.


    * I had a very small problem getting it to work the first time, but that problem has been fixed since then.

    --

    The difference between spam and poop is that you don't have to dig through septic tanks looking for real food. -- Me
  11. Bad for desktop systems? by Anonymous Coward · · Score: 0

    Why? I may be missing something, but the only time I've ever modified my initrd is when rebuilding the kernel.

  12. Do not use those instructions! by sjames · · Score: 1

    Unfortunatly, the article starts well by explaining initramfs (using a compressed cpio rather than a block device image), but then leads the reader down the initrd path.

    Initramfs is intended to superceed initrd for several good reasons.

    For one, it's more efficient. Files in an initramfs are a special case of filesystem where the file is loaded into the generic file cache but has no backing storage (that is, it doesn't live on a block device). OTOH, an initrd is a simple image of a block device. It has a fixed number of blocks defined at creation time. On top of that it has filesystem formatting.

    For one thing, access to the file takes a much simpler path through the code in the case of initramfs. At a minimum, an initrd requires one more filesystem to be built in to the kernel. Initramfs itself is just a thin layer over the generic filesystem code, so is much smaller and simpler.

    An initramfs image will be exactly the size that is needed, never more or less. There is no need to guess what size block device will be needed to exactly hold everything you need in advance. There is no need to go with anything larger 'just in case'.

    As for memory usage, once an initramfs is mounted, if you rm a file, that memory is immediatly freed (assuming nothing is holding the file open). With initrd, you can rm a file but it won't help you, it just means you now have more wasted blocks held in RAM.

    Initramfs can grow as necessary (memory permitting). For example if the boot process fails, you can log WHY to a file and drop to a root shell for debugging. An initrd can be writable, but can never be any larger than it was created to be. If you MIGHT need to write, you will have to build the initrd large enough to cover the worst case anyone might encounter in debugging and hope that's not too big for machines with less RAM to accomodate.

    Once you get a swap partition mounted from somewhere, your initramfs can be swapped out as needed, so can grow larger than RAM if necessary. With an initrd, it's stuck right where it is until you unmount it.

    It seems to me that the reasons to keep the old initrd support around are down to support for legacy uses and as long as we have the code and it's not too much trouble, why not?

    So, in the shell script from the article, instead of creating /tmp/ramdisk.img and mounting it with -oloop, just create a directory and copy (or hardlink) what you want more or less as the script does. Then make a compressed cpio archive of it as the initramfs image. Possibly like cd initrd; find . |cpio -H newc -o |gzip -9 >../initrd.img.gz