{"id":4180,"date":"2015-01-27T19:34:41","date_gmt":"2015-01-27T11:34:41","guid":{"rendered":"http:\/\/rmohan.com\/?p=4180"},"modified":"2015-01-27T19:37:08","modified_gmt":"2015-01-27T11:37:08","slug":"extract-contents-of-initrd","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=4180","title":{"rendered":"Extract contents of initrd"},"content":{"rendered":"<p>How to extract and view contents of initrd<\/p>\n<p>Depending on your version of linux, the \/boot\/initrd may be one of two formats:<\/p>\n<p>cpio archive<br \/>\next2 filesystem data<br \/>\nThe easiest way to determine what you&#8217;re working with is by using the file command.  Here are examples showing the output for each file type listed in the above bullet points:<\/p>\n<p>[root@linux ~]# file \/boot\/initramfs-2.6.32-220.17.1.el6.x86_64.img<br \/>\n\/boot\/initramfs-2.6.32-220.17.1.el6.x86_64.img: gzip compressed data, from Unix, last modified: Thu Jun 28 18:33:36 2012, max compression<\/p>\n<p>[root@linux ~]# file \/boot\/initramfs64-catalyst<br \/>\ninitramfs64-catalyst: gzip compressed data, from Unix, max compression<br \/>\nTo be sure what you&#8217;re dealing with, supply the -z option to the file command as this will attempt to look inside the compressed files to see what the contents are:<\/p>\n<p>[root@linux ~]# file -z \/boot\/initrd-2.6.9-023stab053.2-smp.img<br \/>\n\/boot\/initrd-2.6.9-023stab053.2-smp.img: Linux rev 1.0 ext2 filesystem data (gzip compressed data, from Unix, max compression)<br \/>\n[root@linux ~]# file -z \/boot\/initramfs64-catalyst<br \/>\ninitramfs64-catalyst: ASCII cpio archive (SVR4 with no CRC) (gzip compressed data, from Unix, max compression)<br \/>\nMount ext2 filesystem data (gzip compressed) initrd contents<\/p>\n<p>I like to work with copies of the initrd, so I created a dir in \/root\/initrd and work with it there:<\/p>\n<p>[root@linux root]# mkdir \/root\/initrd; cd \/root\/initrd<br \/>\n[root@linux initrd]# cp \/boot\/initrd-2.6.9-023stab053.2-smp.img .<br \/>\nSome online documents indicate the initrd should be renamed with the .gz extension so that gunzip can be used to extract the compressed file, but most linux distros ship with the handy zcat command, which saves us a step from having to rename files.  This is the method I prefer:<\/p>\n<p>[root@linux initrd]# zcat initrd-2.6.9-023stab053.2-smp.img > initrd-2.6.9-023stab053.2-smp<br \/>\n[root@linux initrd]# file -z initrd-2.6.9-023stab053.2-smp<br \/>\ninitrd-2.6.9-023stab053.2-smp: Linux rev 1.0 ext2 filesystem data<br \/>\nNow it&#8217;s ready to be mounted as a loop device:<\/p>\n<p>[root@linux initrd]# mount -o loop .\/initrd-2.6.9-023stab053.2-smp \/mnt<br \/>\n[root@linux initrd]# ls -l \/mnt<br \/>\ntotal 9<br \/>\ndrwxr-xr-x  2 root root 1024 Jun 25 15:40 bin<br \/>\ndrwxr-xr-x  2 root root 1024 Jun 25 15:40 dev<br \/>\ndrwxr-xr-x  4 root root 1024 Jun 25 15:40 etc<br \/>\ndrwxr-xr-x  2 root root 1024 Jun 25 15:40 lib<br \/>\n-rwxr-xr-x  1 root root  869 Jun 25 15:40 linuxrc<br \/>\ndrwxr-xr-x  2 root root 1024 Jun 25 15:40 loopfs<br \/>\ndrwxr-xr-x  2 root root 1024 Jun 25 15:40 proc<br \/>\nlrwxrwxrwx  1 root root    3 Jun 25 15:40 sbin -> bin<br \/>\ndrwxr-xr-x  2 root root 1024 Jun 25 15:40 sys<br \/>\ndrwxr-xr-x  2 root root 1024 Jun 25 15:40 sysroot<br \/>\nExtract cpio archive initrd contents<\/p>\n<p>[root@linux initrd]# cp \/boot\/initramfs64-catalyst .<br \/>\n[root@linux initrd]# zcat initramfs64-catalyst | cpio -id<br \/>\n[root@vpsrep tmp]# ls -l<br \/>\ntotal 387232<br \/>\ndrwxr-xr-x   2 root root      4096 Jun 26 21:57 bin<br \/>\ndrwxr-xr-x   2 root root      4096 Jun 26 21:57 boot<br \/>\ndrwxr-xr-x   3 root root      4096 Jun 26 21:58 dev<br \/>\ndrwxr-xr-x  32 root root      4096 Jun 26 21:58 etc<br \/>\ndrwxr-x&#8212;   5 root root      4096 Jun 26 21:58 evolution<br \/>\ndrwxr-xr-x   2 root root      4096 Jun 26 21:58 home<br \/>\nlrwxrwxrwx   1 root root        15 Jun 26 21:58 init -> \/evolution\/init<br \/>\n-rw-r&#8211;r&#8211;   1 root root 396057600 Jun 26 21:55 initramfs64-catalyst.cpio<br \/>\nlrwxrwxrwx   1 root root         5 Jun 26 21:58 lib -> lib64<br \/>\ndrwxr-xr-x   2 root root      4096 Jun 26 21:58 lib32<br \/>\ndrwxr-xr-x   9 root root      4096 Jun 26 21:58 lib64<br \/>\ndrwxr-xr-x   2 root root      4096 Jun 26 21:58 media<br \/>\ndrwxr-xr-x   2 root root      4096 Jun 26 21:58 mnt<br \/>\ndrwxr-xr-x   2 root root      4096 Jun 26 21:58 opt<br \/>\ndrwxr-xr-x   2 root root      4096 Jun 26 21:58 proc<br \/>\ndrwx&#8212;&#8212;   2 root root      4096 Jun 26 21:58 root<br \/>\ndrwxr-xr-x   2 root root      4096 Jun 26 21:58 sbin<br \/>\ndrwxr-xr-x   2 root root      4096 Jun 26 21:58 sys<br \/>\ndrwxrwxrwt   2 root root      4096 Jun 26 21:58 tmp<br \/>\ndrwxr-xr-x  12 root root      4096 Jun 26 21:58 usr<br \/>\ndrwxr-xr-x   9 root root      4096 Jun 26 21:58 var<br \/>\nNote: the &#8211;verbose option may be supplied to list the files to the console as they are being extracted, or adding v to the options in the example above, as in cpio -idv<\/p>\n<p>cpio -itv will simply list the contents without extracting them.<\/p>\n<p>The -itv options are short hand for &#8211;extract &#8211;list &#8211;verbose<br \/>\nThe -idv options are short hand for &#8211;extract &#8211;make-directories &#8211;verbose<\/p>\n<p>Repackaging<\/p>\n<p>If some some reason a change needs to be made to the initrd and it needs to be buttoned back up, this is how to do it:<\/p>\n<p>ext2 filesystem data<\/p>\n<p>copy whatever files you want to whereever the initrd ext2 filesystem was mounted.  Once finished, umount the filesystem, gzip it (which will give it the .gz extension), then rename it to the original initrd file that was copied or some other name like initrd-2.6.9-023stab053.2-smp-mpath.img.  If a new name is given, be sure to update the boot loader configuration files like \/boot\/grub\/grub.conf.<\/p>\n<p>cpio archive<\/p>\n<p>From within the directory the contents were extracted, first remove or move the .cpio file to another parent directory outside to ensure it&#8217;s not part of the new cpio archive then recreate the archive:<\/p>\n<p>find . | cpio &#8211;quiet -c -o | gzip -9 -n > ..\/initramfs64-catalyst<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to extract and view contents of initrd<\/p>\n<p>Depending on your version of linux, the \/boot\/initrd may be one of two formats:<\/p>\n<p>cpio archive ext2 filesystem data The easiest way to determine what you&#8217;re working with is by using the file command. Here are examples showing the output for each file type listed in the [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,17],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4180"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4180"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4180\/revisions"}],"predecessor-version":[{"id":4182,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4180\/revisions\/4182"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}