Create cpio file with different absolute directory

cpiou-boot

I am trying to modify a file system image packed with cpio. For that reason I first need to extract and later pack the image. As the image contains a whole file system all the files are given in absolute file names, so I can't directly pack and unpack it, since it would conflict with my machine's root system.

So when unpacking I used --no-absolute-filenames to unpack it to a working directory of my choice. Now I want to pack it again. If I just pack it i'd only get files like that:

/path/to/working/directory/file1
/path/to/working/directory/file2
/path/to/working/directory/file3

or

./file1
./file2
./file3

instead of

/file1
/file2
/file3

Does anyone know how I could get the desired output? Google didn't help me so far.

I really need absolute path names in the output file, because I am using it for an u-boot uImage file system image, and that requires the paths to be absolute, or it won't boot.

Best Answer

Use pax and its -s option to rename files as they are added to the archive. Pax is POSIX's replacement for the traditional utilities tar and cpio; some Linux distributions don't install it by default but it should always be available as a package.

pax -w -x cpio -s '~^[/]*~~' root-directory >archive.cpio
Related Question