Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Yesterday I was making some experiments on Slitaz. It uses multiple initrd.img's to store files/changes.

I wanted to extract one of its initrd.gz images (which is a cpio archive) to a folder, edit/remove them, repack again.

I used this code:

cat rootfs.img | cpio -idvm

Then all files are extracted to my root filesystem. My whole OS is corrupted. (What an emberrasing situation...)

What should I do to make such operations safely but in an easy way? Chroot? LXC? (VirtualBox is the last resort)

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

Archived with relative paths

I would advise against running that type of command at your root level, /. That's asking for trouble. I always run cpio -idvm related commands in their own directories, and then use mv or cp to put the files where they need to be manually.

You can also use the method I described in this other U&L Q&A titled: How do I install TazPkg in SliTaz Linux, which also makes use of cpio.

Archived with absolute paths

If the archive was built with absolute paths you can tell cpio with the --no-absolute-filenames switch to block it from extracting into /.

$ mkdir /tmp/cpio-root
$ cd /tmp/cpio-root
$ cat rootfs.img | cpio -idvm --no-absolute-filenames

References

share|improve this answer
    
No matter where my working directory is, cpio -idvm is always extracting with absolute filenames (beginning from /). --no-absolute-filenames solved my problem. I think my sendboxing requirement will need LXC or VirtualBox. Thank you. –  ceremcem Jun 16 at 14:19
add comment

You can use the unp utility for this.

unp is a utility for unpacking multiple formats. One of it's features (the -U argument) is the ability to look into the archive and see if it has multiple root elements. If it does, it extracts them into a directory.

For example:

$ echo $RANDOM > a
$ echo $RANDOM > b
$ tar -cf archive.tar a b
$ rm a b
$ unp -U archive.tar
$ ls -l a b archive
ls: cannot access a: No such file or directory
ls: cannot access b: No such file or directory
archive:
total 8
-rw-r--r-- 1 root root 5 Jun 15 03:16 a
-rw-r--r-- 1 root root 6 Jun 15 03:16 b

unp works with many different formats (including cpio). It just calls out to the appropriate utility for handling the archive:

# unp -s
Known archive formats and tools:
7z:           p7zip or p7zip-full
ace:          unace
ar,deb:       binutils
arj:          arj
bz2:          bzip2
cab:          cabextract
chm:          libchm-bin or archmage
cpio,afio:    cpio or afio
dat:          tnef
dms:          xdms
exe:          maybe orange or unzip or unrar or unarj or lha 
gz:           gzip
hqx:          macutils
lha,lzh:      lha
lz:           lzip
lzma:         xz-utils or lzma
lzo:          lzop
lzx:          unlzx
mbox:         formail and mpack
pmd:          ppmd
rar:          rar or unrar or unrar-free
rpm:          rpm2cpio and cpio
sea,sea.bin:  macutils
shar:         sharutils
tar:          tar
tar.bz2,tbz2: tar with bzip2
tar.lzip:     tar with lzip
tar.lzop,tzo: tar with lzop
tar.xz,txz:   tar with xz-utils
tar.z:        tar with compress
tgz,tar.gz:   tar with gzip
uu:           sharutils
xz:           xz-utils
zip,cbz,cbr,jar,war,ear,xpi,adf: unzip
zoo:          zoo

The --help output showing what it can do:

# unp --help

USAGE:
   /usr/bin/unp [ options ] file [ files... ]
   file: compressed file(s) to expand/extract

   Use -- [ ARGUMENTS ] to pass arguments to external programs, eg. some tar options:
   unp fastgl.tgz xmnt.tgz -- -C /tmp

   Options:
   -f Continue even if program availability checks fail or directory collision occurs
   -u Special helper mode.
      For most archive types:
      - create directory <filename without suffix>/
      - extract contents there
      For Debian/Ubuntu packages:
      - extract data.tar.gz after each operation in local directory
      - extract control.tar.gz into control/<package_version_arch>/
   -U Smart mode, acts like -u (see above) if archive contains multiple
      elements but if there is only one file/directory element then it's stored 
      in the current directory.
   -s Show the list of supported formats
   -v More verbosity
   -h Show this help
share|improve this answer
    
It's good to know that there is such a program simplifying extraction process (extracting multiple files into a directory is a must-have facility for me). But there is no tazpkg for this program. So it's not an option to use it in SliTaz (with ease, at least). –  ceremcem Jun 15 at 11:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.