# Contents of initramfs

## wmakowski

I wanted to take a look at what is in initramfs-genkernel-x86-2.6.12-gentoo-r6 included on the LiveCD.  So I unzipped it and then used cpio to extract the archive.  The command used to extract was:

```
cpio -idv < initramfs-genkernel-x86-2.6.12-gentoo-r6
```

After extracting I found a basic root directory structure with only a few files.

```
drwxr-xr-x  11 root     root            0 Aug  9 02:18 .

drwxr-xr-x   2 root     root            0 Aug  9 02:18 sys

drwxr-xr-x   2 root     root            0 Aug  9 02:18 dev

crw-------   1 root     root       4,   1 Aug  9 02:18 dev/tty1

crw-rw----   1 root     root       5,   1 Aug  9 02:18 dev/console

crw-rw----   1 root     root       1,   3 Aug  9 02:18 dev/null

drwxr-xr-x   3 root     root            0 Aug  9 02:18 var

drwxr-xr-x   3 root     root            0 Aug  9 02:18 var/lock

drwxr-xr-x   2 root     root            0 Aug  9 02:18 var/lock/dmraid

drwxr-xr-x   4 root     root            0 Aug  9 02:18 usr

drwxr-xr-x   2 root     root            0 Aug  9 02:18 usr/bin

drwxr-xr-x   2 root     root            0 Aug  9 02:18 usr/sbin

drwxr-xr-x   2 root     root            0 Aug  9 02:18 bin

drwxr-xr-x   2 root     root            0 Aug  9 02:18 temp

drwxr-xr-x   2 root     root            0 Aug  9 02:18 sbin

drwxr-xr-x   2 root     root            0 Aug  9 02:18 etc

-rw-r--r--   1 root     root           93 Aug  9 02:18 etc/fstab

drwxr-xr-x   2 root     root            0 Aug  9 02:18 proc
```

The initramfs-genkernel-x86-2.6.12-gentoo-r6 file is roughly 28MB unzipped and I was expecting to find a lot more content.  Does anyone see an error in how I extracted the archive?

Bill

----------

## Antimatter

I was actually wondering the very same thing myself, i managed to get my initramfs open and got the very same results as you did, from 2 different initramfs, one was 14 MB, other was 28 MB so i have no idea what's going on here.

hey i think i've found the answer for both of our questions here Unpacking Genkernel or gentoo.igz initramfs | Strange result

----------

## wmakowski

Antimatter, 

An excellent find, and it explains the layout of the archive.  Thanks for sharing.

Bill

----------

## widan

Another script to unpack an initramfs:

```
#!/bin/bash

if [[ $# -ne 2 ]]; then

    echo "Usage: $0 initramfs directory"

    exit 1

fi

mkdir -p "$2"

if file "$1" | grep gzip > /dev/null 2>&1; then

    echo "Initramfs is gzip compressed, unpacking first"

    cp "$1" .temp.gz

    gunzip .temp.gz

else

    cp "$1" .temp

fi

if file .temp | grep ext2 > /dev/null 2>&1; then

    echo

    echo "This is an initrd (ext2 filesystem), not an initramfs"

    echo

    echo "Those are ext2 filesystem images, that can be mounted on loopback:"

    echo

    echo "  # mount -o loop $1-unpacked $2"

    echo

    mv .temp $1-unpacked

    exit

fi

compno=1

while [[ -s .temp ]]; do

    blocks=$(cpio -t -H newc < .temp 2>&1 > /dev/null | sed "s/[^0-9]*//g")

    dd if=.temp of=.$compno.cpio bs=512 count=$blocks 2> /dev/null

    dd if=.temp of=.scratch bs=512 skip=$blocks 2> /dev/null

    mv .scratch .temp

    echo "Extracting component #$compno ($blocks blocks)"

    pushd "$2" > /dev/null 2>&1

    cpio -i -m -H newc < ../.$compno.cpio > /dev/null 2>&1

    popd > /dev/null 2>&1

    rm .$compno.cpio

    compno=$((compno+1))

done

rm .temp
```

And to rebuild it when you're done modifying it, you just need to do that:

```
# cd irfs-dir

# find . -print | cpio -o -H newc > ../initramfs

# cd ..

(if you want)

# gzip initramfs
```

Edit: Added detection of old-style initrd images, that don't need this script as they are just gzipped ext2 filesystems.Last edited by widan on Tue Feb 07, 2006 11:22 pm; edited 1 time in total

----------

## Antimatter

Thanks you! the c code that i linked to in the other thread end up not working at all, so i was about to post that it didn't work when i ran over the above script, and that script works great, so i finally can open the damn initramfs file so i can check out how its built and modify it if needed

----------

## modified_bessel

```
blocks=$(cpio -t -H newc < .temp 2>&1 > /dev/null | sed "s/[^0-9]*//g")
```

returns a null string; any ideas??

Thanks!

----------

## widan

What does that command return (you will need to add an exit command in the loop so you can get the .temp file) ?

```
cpio -t -H newc < .temp
```

----------

## modified_bessel

Code segment:

```
compno=1

while [[ -s .temp ]]; do

    test=$(cpio -t -H newc < .temp) #new

    echo "test=$test"  #new

    exit #new

```

Console response:

```
localhost testplace # ./igz-unzipper.sh gentoo.igz irfs-dir

Initramfs is gzip compressed, unpacking first

cpio: premature end of file

test=

localhost testplace #  
```

wherein gentoo.igz (2472813 bytes) is the initrd image from the 2005.0 CD, irfs-dir is an empty directory in the same working directory.  If the initrd is uncompressed into another image file before using the script, the script detects the new file (6746112 bytes) correctly, does not try to unzip it, but gives the same result for the above code(s).  

Thanks for your previous prompt reply, and excuse my tardiness!

----------

## modified_bessel

Addendum....

....  the expansion work file  .temp in both cases contains 6746112 bytes, as expected, but clearly not in the required format????

Thanks!

m_b

----------

## widan

Some older images are really an initrd (compressed ext2 filesystems) and not an initramfs (cpio archive). These don't need this script and can be mounted as loopback filesystems after gunzipping. They can then be modified in place, there is no special procedure to rebuild them (except to remember to unmount the image, and gzip it again if you want).

The one on the 2005.0 x86 minimal LiveCD is clearly an initrd. I edited the post with the script to add a check for that kind of image.

----------

## modified_bessel

Thanks for the very relevant observation; my problem has clearly been working on different CD's, all containing identical file names, but indeed different file types on each!  

All the best!

m_b

----------

## Skandal

I'm working with the initramfs found on 2005.1-minimal-install-amd64

When I recompress the initramfs using

```
# cd irfs-dir

# find . -print | cpio -o -H newc > ../initramfs

# cd ..

(if you want)

# gzip initramfs
```

If I decompress the image using your script again the script no longer lists

several components, just one big one. The kernel panics if I use the image on my install medium. 

I've been searching the internet for information about this, but haven't found any other compression method.

----------

## widan

 *Skandal wrote:*   

> If I decompress the image using your script again the script no longer lists several components, just one big one.

 

This is normal. The original initramfs is a concatenation of several cpio archives. I rebuild it as only one cpio archive, but I don't think it matters for the kernel. The method given on the Linux kernel mailing list (this article, step 3) is exactly the same.

 *Skandal wrote:*   

> The kernel panics if I use the image on my install medium.

 

What kind of error message(s) do you have (the panic itself, and also error messages that happened just before) ?

----------

## Skandal

The kernel wasn't able to find /init the last time

but ofcourse it was all my fault  :Embarassed: 

I suspect that I must have decompressed the archives the wrong way myself the last time, and recompressed the archieve again. 

Now I have a runaway loop modprobe problem instead.However, I have found a new release I didn't know about earlier, so I'm going to retry the procedure with the new release. 

Thanks for your help btw=)

----------

