Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Kernel & Hardware
  • Search

Unpacking Genkernel or gentoo.igz initramfs | Strange result

Kernel not recognizing your hardware? Problems with power management or PCMCIA? What hardware is compatible with Gentoo? See here. (Only for kernels supported by Gentoo.)
Post Reply
Advanced search
14 posts • Page 1 of 1
Author
Message
travisx
n00b
n00b
Posts: 1
Joined: Mon Sep 19, 2005 6:32 am

Unpacking Genkernel or gentoo.igz initramfs | Strange result

  • Quote

Post by travisx » Tue Sep 20, 2005 8:53 pm

I am trying to unpack the 2005.1 initramfs (gentoo.igz) so that I can tweak it for use on a LiveUSB, but I am not having much success.

Strangely, when I attempt to use cpio to unpack the gentoo.igz initramfs file (or any file made by Genkernel, actually), I get a directory structure with a handful of files (like fstab), but it is missing obviously necessary files such as linuxrc and init. The total quantity of files unpacked is less than 4k, while the gzipped gentoo.igz file is 17M.

I've really been banging my head against the wall on this. I assume that I must be doing something wrong, because the initramfs works correctly in operation.

Scouring the internet and the Gentoo forums has proved only that others have the same unanswered question.

Because everything is more clear in code:

Code: Select all

# gunzip -c gentoo.igz | cpio -H newc -t
.
etc
etc/fstab
bin
sys
var
var/lock
var/lock/dmraid
usr
usr/bin
usr/sbin
dev
dev/null
dev/tty1
dev/console
temp
proc
sbin
5 blocks
I've tried this on two very different and up to date Gentoo systems, but I get the same result.

This is going to seriously frustrate me until someone lets me in on the (probably trivial) thing that I'm doing wrong.

Thanks,

-- Travis
Top
irondog
l33t
l33t
User avatar
Posts: 715
Joined: Mon Jul 07, 2003 1:41 pm
Location: Voor mijn TV. Achter mijn pc.

  • Quote

Post by irondog » Sat Sep 24, 2005 1:27 am

Genkernels initramfs is a file consisting of several gzipped cpio archives concatenated to each other. It's not so easy to extract it.

It tried to do it in C. This is the result. It's quite unpolished, but just works for me.

Code: Select all

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>


static unsigned char magick[]="070701000000000000000000000000000000000000000100000000000000000000000000000000000000000000000
00000000b00000000TRAILER!!!";

static int src, dest;
int c[100];

int rec_read(int matches, int x) {
        int n_read;
        n_read = read(src, &c[x], 1);

        if(n_read != 1)
                return -1;

        if(c[x] == magick[matches]) {
                if ( matches != strlen(magick) - 1) {
                        rec_read(matches + 1, x + 1);
                } else {
                        printf("MAGIC FOUND\n");


                        do {
                                n_read = read(src, &c, 1);
                        } while( n_read == 1 && c[0] != '0');
                        lseek(src, -1, SEEK_CUR);

                        rec_read(0, 0);
                }
        } else {
                write(dest, &c[x - matches] , 1);
                lseek(src, 0 - matches, SEEK_CUR);
                return 0;
        }
}
int main () {
        int c, n_read;
        src = open("in", O_RDONLY);
        dest = open("out", O_RDWR | O_CREAT, 0666);

        while (rec_read(0, 0) != -1);

        lseek(dest, -1, SEEK_CUR);
        write(dest, &magick, strlen(magick));
//        write(dest, "\0\0\0\0\0", 5);

        close(src);
        close(dest);
}
       
Use it like this:

Code: Select all

$ zcat /boot/initramfs.gz > ./in
$ ./split-iniramfs
$ cpio -i -H newc < out
Alle dingen moeten onzin zijn.
Top
capira
Tux's lil' helper
Tux's lil' helper
User avatar
Posts: 147
Joined: Mon May 02, 2005 9:31 pm
Location: Spain

  • Quote

Post by capira » Tue Oct 04, 2005 3:48 pm

Thanks for the answer irondog!

I just wonder if anybody knows how to pack initramfs again ...I have to add a delay in linuxrc in order to give time to the udev module to create the /dev/sda* entry.

Is the linuxrc script placed in anywhere in the kernel dir or genkernel dir so I had just to add that line once and let genkernel do the rest?

Thanks in advance.

PS. All this is because I want boot gentoo from a external USB hard disk
Top
capira
Tux's lil' helper
Tux's lil' helper
User avatar
Posts: 147
Joined: Mon May 02, 2005 9:31 pm
Location: Spain

  • Quote

Post by capira » Sat Oct 08, 2005 8:48 am

The way of repacking the files into a initramfs file is explained here.
Top
jong357
n00b
n00b
Posts: 7
Joined: Tue Mar 09, 2004 4:43 pm

  • Quote

Post by jong357 » Mon Oct 17, 2005 12:56 am

Sorry to be ignorant here but could someone clarify further on how to do this. I'm having the same issue. That looks like a C file to me that you would want to compile into a binary called split-initramfs.... I'm not having much luck with it.

Code: Select all

$ gcc -o split-iniramfs split-iniramfs.c

split-iniramfs.c:7: error: missing terminating " character
split-iniramfs.c:8:1: invalid suffix "b00000000TRAILER" on integer constant
split-iniramfs.c:8: error: parse error before '!' token
split-iniramfs.c:8: error: missing terminating " character
split-iniramfs.c: In function `rec_read':
split-iniramfs.c:15: error: `src' undeclared (first use in this function)
split-iniramfs.c:15: error: (Each undeclared identifier is reported only once
split-iniramfs.c:15: error: for each function it appears in.)
split-iniramfs.c:35: error: `dest' undeclared (first use in this function)
split-iniramfs.c: In function `main':
split-iniramfs.c:42: error: `src' undeclared (first use in this function)
split-iniramfs.c:43: error: `dest' undeclared (first use in this function)
Thanks for any and all help.... I've been "banging" my head on this one for a while as well and this post is the only thing I can find on the web concerning how to handle this type of initrd.
Top
jong357
n00b
n00b
Posts: 7
Joined: Tue Mar 09, 2004 4:43 pm

  • Quote

Post by jong357 » Mon Oct 17, 2005 5:11 am

I got it... When I copied the above C code from irondog's post, the very long line at the top somehow got a space inserted in there somewhere.

syntax highlighting gave it away after I stared at it long enough... This damn initramfs image really burned me out. Haven't tried messing with it yet but atleast the binary compiled.... :wink:
Top
piavlo
Tux's lil' helper
Tux's lil' helper
Posts: 141
Joined: Tue Jun 21, 2005 4:17 pm

  • Quote

Post by piavlo » Fri Nov 11, 2005 1:39 pm

i get an error cpio: premature end of file then extracting the cpio archive repacked by the split-initramfs code
both in x86 amd amd64 2005.1 livecd gentoo.igz files.
Top
capira
Tux's lil' helper
Tux's lil' helper
User avatar
Posts: 147
Joined: Mon May 02, 2005 9:31 pm
Location: Spain

  • Quote

Post by capira » Fri Nov 11, 2005 3:32 pm

Well, I have the same problem here but I everything seems to work fine.
Top
wmd
n00b
n00b
Posts: 1
Joined: Fri Mar 03, 2006 6:05 pm

how I unpacked it in perl

  • Quote

Post by wmd » Fri Mar 03, 2006 6:15 pm

For what ever reason your C didn't work for me. So I wrote this sloppy code.

mv gentoo.igz gentoo-image.gz
gunzip gentoo-image.gz
usage: ./unigz.pl gentoo-image

Code: Select all

#!/usr/bin/perl -w
#

use strict;

use File::Slurp qw(slurp);

my $ifile = slurp($ARGV[0], binmode=> ':raw');
my $newfile;
my $i=1;
my @newfiles = split(/TRAILER!!!/,$ifile);
`mkdir $ARGV[0].dir`;

foreach $newfile (@newfiles)
{
        open F, "> ./$ARGV[0].dir/$i";
        print F $newfile;
        print F "TRAILER!!!\0";
        close F;
        `cd $ARGV[0].dir; cpio -i -H newc < $i; rm $i; cd ..`;
        $i++;
}
I got a few errors, but no file was missing besides the devices. (such as dev/console)
Top
beep
n00b
n00b
Posts: 34
Joined: Thu Jan 20, 2005 3:58 pm

  • Quote

Post by beep » Mon Mar 06, 2006 3:18 pm

hi,

i tried with the new 2006.0 image and found out that the magick has been change to

Code: Select all

07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!!


cheers,
::beep
Top
druggo
Guru
Guru
Posts: 342
Joined: Wed Sep 24, 2003 9:21 am
Location: Hangzhou
Contact:
Contact druggo
Website

Re: how I unpacked it in perl

  • Quote

Post by druggo » Tue Aug 22, 2006 8:04 am

wmd wrote:For what ever reason your C didn't work for me. So I wrote this sloppy code.

mv gentoo.igz gentoo-image.gz
gunzip gentoo-image.gz
usage: ./unigz.pl gentoo-image

I got a few errors, but no file was missing besides the devices. (such as dev/console)
great !
AMD FX-8300 / Loongson-2f (lemote-yeeloong)
Top
Offpist2000
n00b
n00b
Posts: 5
Joined: Fri Dec 15, 2006 2:35 pm

split-initramfs

  • Quote

Post by Offpist2000 » Fri Dec 15, 2006 2:42 pm

To many error when compiling the C-code. I used the following
with good result:

Start in an empty directory, replace the TRAILER after each cpio archive
with some nonsense, and unpack everuthing

zcat /tmp/usb-live/gentoo.igz | sed -e 's/TRAILER/notTRAILER/g' | cpio -idv

Just ignore the cpio errors.

Do the modifications and the put it pack together:

find * | cpio --quiet --dereference -o -H newc | gzip -9 > /tmp/usb-live/gentoo.igz

/Offpist2000
Top
tap3w0rm
n00b
n00b
Posts: 1
Joined: Thu Apr 12, 2007 9:42 pm

ok for those having problems

  • Quote

Post by tap3w0rm » Thu Apr 12, 2007 9:47 pm

here is a fix to the problems everyone was having. This also included the magic that allows the use on 2006.0 and 2006.1 files

i added a few includes and put in the correct magic. This shoudl compile for everyone.

Code: Select all

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
static unsigned char magick[]="07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!!";

static int src, dest;
int c[100];

int rec_read(int matches, int x) {
        int n_read;
        n_read = read(src, &c[x], 1);

        if(n_read != 1)
                return -1;

        if(c[x] == magick[matches]) {
                if ( matches != strlen(magick) - 1) {
                        rec_read(matches + 1, x + 1);
                } else {
                        printf("MAGIC FOUND\n");


                        do {
                                n_read = read(src, &c, 1);
                        } while( n_read == 1 && c[0] != '0');
                        lseek(src, -1, SEEK_CUR);

                        rec_read(0, 0);
                }
        } else {
                write(dest, &c[x - matches] , 1);
                lseek(src, 0 - matches, SEEK_CUR);
                return 0;
        }
}
int main () {
        int c, n_read;
        src = open("in", O_RDONLY);
        dest = open("out", O_RDWR | O_CREAT, 0666);

        while (rec_read(0, 0) != -1);

        lseek(dest, -1, SEEK_CUR);
        write(dest, &magick, strlen(magick));
//        write(dest, "\0\0\0\0\0", 5);

        close(src);
        close(dest);
}
Top
cerise8192
n00b
n00b
User avatar
Posts: 19
Joined: Thu Aug 21, 2003 5:51 am
Location: Santa Cruz, CA, USA
Contact:
Contact cerise8192
Website

  • Quote

Post by cerise8192 » Wed Apr 25, 2007 8:46 pm

I actually couldn't get either version of the C or perl to work, so I hacked up the perl.

Gunzip the .igz and run it as the only argument to this script and voila!

Code: Select all

#!/usr/bin/perl -w
#

use strict;

use File::Slurp qw(slurp);

my $ifile = slurp($ARGV[0], binmode=> ':raw');
my $newfile;
my $i=1;
my @newfiles = split(/TRAILER!!!/,$ifile);
`mkdir $ARGV[0].dir`;

foreach $newfile (@newfiles)
{
        $newfile =~ s/^[\0]*//;
        if($newfile){
           open F, "> ./$ARGV[0].dir/$i";
           $newfile = $newfile . "TRAILER!!!";
           for(my $null=22*16+21;$null>0;$null--){
              $newfile = $newfile . "\0";
           }
           print F $newfile;
           close F;
           print "$i\n";
           `cd $ARGV[0].dir; cpio -i -H newc < $i; rm $i; cd ..`;
           $i++;
        }
}
littlegreenmen.armory.com is a free shell account and anonymous game server. telnet/ssh in as user "request" to get an account.
Top
Post Reply

14 posts • Page 1 of 1

Return to “Kernel & Hardware”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy