
昨天我正在做一些實驗斯利塔茲。它使用多個 initrd.img 來儲存檔案/變更。
我想將其 initrd.gz 映像之一(這是 cpio 存檔)提取到一個資料夾,編輯/刪除它們,然後再次重新打包。
我使用了這段程式碼:
cat rootfs.img | cpio -idvm
然後所有文件都被提取到我的根文件系統。我的整個作業系統都損壞了。 (這是多麼尷尬的場面啊…)
我應該怎麼做才能安全且輕鬆地進行此類操作?克羅特? LXC? (VirtualBox是最後的手段)
答案1
使用相對路徑存檔
我建議不要在根級別運行此類命令/
。那是自找麻煩。我總是cpio -idvm
在自己的目錄中運行相關命令,然後使用mv
或cp
手動將文件放在需要的地方。
您也可以使用我在其他 U&L 問答中描述的方法,標題為:如何在 SliTaz Linux 中安裝 TazPkg,這也利用了cpio
.
使用絕對路徑存檔
如果存檔是使用絕對路徑建構的,您可以cpio
使用--no-absolute-filenames
開關阻止其提取到/
.
$ mkdir /tmp/cpio-root
$ cd /tmp/cpio-root
$ cat rootfs.img | cpio -idvm --no-absolute-filenames
參考
答案2
您可以使用該unp
實用程式來執行此操作。
unp
是一個用於解壓縮多種格式的實用程式。它的功能之一(-U
參數)是能夠查看存檔並查看它是否具有多個根元素。如果是,它將它們提取到一個目錄中。
例如:
$ 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
適用於許多不同的格式(包括cpio
)。它只是調用適當的實用程式來處理存檔:
# 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
輸出--help
顯示它可以做什麼:
# 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