Skip to content

Instantly share code, notes, and snippets.

@tai271828
Created November 21, 2018 01:21
Show Gist options
  • Save tai271828/89d4842fd73dbd9a99b603d12513092f to your computer and use it in GitHub Desktop.
Save tai271828/89d4842fd73dbd9a99b603d12513092f to your computer and use it in GitHub Desktop.
decompress and recompress the initrd image of Ubuntu Cosmic
#!/bin/bash
#sudo apt-get install binwalk
# $ binwalk initrd
#
#DECIMAL HEXADECIMAL DESCRIPTION
#--------------------------------------------------------------------------------
#0 0x0 ASCII cpio archive (SVR4 with no CRC), file name: ".", file name length: "0x00000002", file size: "0x00000000"
#112 0x70 ASCII cpio archive (SVR4 with no CRC), file name: "kernel", file name length: "0x00000007", file size: "0x00000000"
#232 0xE8 ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86", file name length: "0x0000000B", file size: "0x00000000"
#356 0x164 ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86/microcode", file name length: "0x00000015", file size: "0x00000000"
#488 0x1E8 ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86/microcode/AuthenticAMD.bin", file name length: "0x00000026", file size: "0x00006B2A"
#28072 0x6DA8 ASCII cpio archive (SVR4 with no CRC), file name: "TRAILER!!!", file name length: "0x0000000B", file size: "0x00000000"
#28672 0x7000 ASCII cpio archive (SVR4 with no CRC), file name: "kernel", file name length: "0x00000007", file size: "0x00000000"
#28792 0x7078 ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86", file name length: "0x0000000B", file size: "0x00000000"
#28916 0x70F4 ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86/microcode", file name length: "0x00000015", file size: "0x00000000"
#29048 0x7178 ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86/microcode/.enuineIntel.align.0123456789abc", file name length: "0x00000036", file size: "0x00000000"
#29212 0x721C ASCII cpio archive (SVR4 with no CRC), file name: "kernel/x86/microcode/GenuineIntel.bin", file name length: "0x00000026", file size: "0x00180C00"
#1605296 0x187EB0 ASCII cpio archive (SVR4 with no CRC), file name: "TRAILER!!!", file name length: "0x0000000B", file size: "0x00000000"
#1605632 0x188000 LZMA compressed data, properties: 0x5D, dictionary size: 8388608 bytes, uncompressed size: -1 bytes
mkdir working
cd working
# firstly get the amd and intel microcode
cpio -id < ../initrd
dd if=../initrd bs=28672 skip=1 | cpio -id
# and then get the root filesystem
#
# if you don't care about the file size you can archive in the following method
#
#dd if=../initrd bs=1605632 skip=1 | unlzma -c | cpio -id
#
#find | cpio -H newc -o > ../initrd.new
#
#-rw-rw-r-- 1 tai271828 tai271828 39M Nov 21 08:37 initrd
#-rw-rw-r-- 1 tai271828 tai271828 167M Nov 21 08:56 initrd.new
# or if you want to lz compress the archive you can
( mkdir rfs; cd rfs; dd if=../../initrd bs=1605632 skip=1 | unlzma -c | cpio -id )
# edit the microcode and the root filesystem
( cd kernel; find | cpio -H newc -o > ../kernel.cpio)
#
( cd rfs; find | cpio -H newc -o > ../rfs.cpio)
lzma -c ./rfs.cpio > ./rfs.cpio.lz
#concatenate these two cpio archives
cat ./kernel.cpio ./rfs.cpio.lz > ../initrd.new
# then it is mucher smaller
#
#-rw-rw-r-- 1 tai271828 tai271828 39M Nov 21 08:37 initrd
#-rw-rw-r-- 1 tai271828 tai271828 39M Nov 21 09:14 initrd.new
#
# $ binwalk initrd.new
#
#DECIMAL HEXADECIMAL DESCRIPTION
#--------------------------------------------------------------------------------
#0 0x0 ASCII cpio archive (SVR4 with no CRC), file name: ".", file name length: "0x00000002", file size: "0x00000000"
#112 0x70 ASCII cpio archive (SVR4 with no CRC), file name: "x86", file name length: "0x00000004", file size: "0x00000000"
#228 0xE4 ASCII cpio archive (SVR4 with no CRC), file name: "x86/microcode", file name length: "0x0000000E", file size: "0x00000000"
#352 0x160 ASCII cpio archive (SVR4 with no CRC), file name: "x86/microcode/GenuineIntel.bin", file name length: "0x0000001F", file size: "0x00180C00"
#1576432 0x180DF0 ASCII cpio archive (SVR4 with no CRC), file name: "x86/microcode/AuthenticAMD.bin", file name length: "0x0000001F", file size: "0x00006B2A"
#1604012 0x1879AC ASCII cpio archive (SVR4 with no CRC), file name: "x86/microcode/.enuineIntel.align.0123456789abc", file name length: "0x0000002F", file size: "0x00000000"
#1604172 0x187A4C ASCII cpio archive (SVR4 with no CRC), file name: "TRAILER!!!", file name length: "0x0000000B", file size: "0x00000000"
#1604608 0x187C00 LZMA compressed data, properties: 0x5D, dictionary size: 8388608 bytes, uncompressed size: -1 bytes
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment