#!/bin/bash ## found on http://wiki.gentoo-italia.net/index.php/Initramfs:_La_cowboy_way_e_i_segreti_di_genkernel 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 | awk ' $2 == "blocks" {print $1}') 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