Well, I had this often repeated problem of searching for how to extract an initrd image only to find that it is basically as cpio+gzip archive but, over a period of time forgetting it so that I come back searching for this again:-) Finally, I made up my mind to put in what all it takes to extract an initrd image in Linux and re-packing it after customization (not covered here, as customization depends on the need), here.
Unpacking the initrd image:
1. Create an empty directory /tmp/initrd (this is an example; you are free to use any other directory name) and cd into it, using the following command
(Replace /tmp/initrd with your directory name, if using a different directory name)
2. Extract the initrd image using the following command
Now, the contents of the initrd image will be there in /tmp/initrd (or whatever directory you have used). Go ahead and do the necessary changes and you may follow the steps below to re-pack it into an initrd image again.
Packing the (custom) initrd image:
1. Get into the directory where you have extracted the initrd image (/tmp/initrd in this example), using the command
(Replace /tmp/initrd with your directory name, if using a different directory name)
2. Pack the initrd image by using the command
(Replace /tmp/initrd with your directory name, if using a different directory name)
This will get you the (re)packed initrd image as /tmp/initrd.img (in this example). Now your custom initrd image is ready.
Unpacking the initrd image:
1. Create an empty directory /tmp/initrd (this is an example; you are free to use any other directory name) and cd into it, using the following command
mkdir -p /tmp/initrd; cd /tmp/initrd;
(Replace /tmp/initrd with your directory name, if using a different directory name)
2. Extract the initrd image using the following command
gunzip absolute-path-to-initrd-image | cpio -i —make-directories
Now, the contents of the initrd image will be there in /tmp/initrd (or whatever directory you have used). Go ahead and do the necessary changes and you may follow the steps below to re-pack it into an initrd image again.
Packing the (custom) initrd image:
1. Get into the directory where you have extracted the initrd image (/tmp/initrd in this example), using the command
cd /tmp/initrd
(Replace /tmp/initrd with your directory name, if using a different directory name)
2. Pack the initrd image by using the command
find ./ | cpio -H newc -o > /tmp/initrd.cpio && gzip /tmp/initrd.cpio && mv /tmp/initrd.cpio.gz /tmp/initrd.img
(Replace /tmp/initrd with your directory name, if using a different directory name)
This will get you the (re)packed initrd image as /tmp/initrd.img (in this example). Now your custom initrd image is ready.