- 论坛徽章:
- 0
|
原帖由 flw 于 2008-10-31 12:20 发表 ![]()
我曾经在本坛发表过一个 cpwithso,我很满意它。
找到了
贴一下到这里吧。方便查阅。
ps:copy so文件的确工作量挺大,使用脚本可以省很多劲。
--------------------------------------版权所有 FLW版----------------------------
原帖地址
http://linux.chinaunix.net/bbs/v ... 72894&page=1###
#!/usr/bin/perl
###############################################################################
# 程序功能和使用方法讲解
# 我们定制自己的 linux 系统的时候,一般情况都要在宿主操作系统上建一个目录,然后
# 把目标磁盘 mount 到这个目录,或者直接把这个目录作为自己的 linux 系统的 initrd
# 文件系统,然后往上面复制想要的东西。但是在复制应用程序的时候,需要一起复制应
# 用程序所依赖的 .so 文件,而且这些 .so 文件大多数又都是一些链接,目录也各不相
# 同,所以甚是头痛。本程序就是做这个用处的。
# 使用例子:
# cpwithso /bin/ls /mnt /usr/bin
# 这句话的意思是说,把 /bin/ls 这个程序复制到 /mnt 里的一个子目录中去。而且它
# 在新系统上的位置是 /usr/bin。复制的时候如果碰到相关的 .so 以及链接,都会一起
# 放到 /mnt 下相应的目录。链接也会一并建好。
#
# 作者: flw 20005.08
###############################################################################
use strict;
use warnings;
use File::Copy;
use File::Basename;
our @ARGV;
if ( scalar @ARGV < 3 ) {
print "Usage: $0 <src_file> <prefix/mount-point> <dest_path>\n\n",
"Example: $0 /bin/ls /mnt /usr/bin\n",
" It will be copy /bin/ls from current root-fs to /mnt/usr/bin,\n",
" and can't forget it's .so files.\n\n";
exit(-1);
}
my $srcFile = shift;
our $mountPoint = shift;
our $destPath = shift;
chomp( our $pwd = `pwd` );
our @needCopyFiles = ();
push @needCopyFiles, [ 'file', $srcFile, $destPath ];
my @lines = `/usr/bin/ldd $srcFile`;
foreach my $line (@lines){
if ( $line =~ /(\S+) \(0x[0-9a-fA-F]+\)/ ) {
&TakeSO( $1 );
}
}
foreach my $file ( @needCopyFiles ){
my $cmd;
if ( $file->[0] ne 'link' ){
unless ( -d ( $mountPoint . $file->[2] ) ){
$cmd = "mkdir -p $mountPoint" . $file->[2]; `$cmd`;
print $cmd, "\n";
}
$cmd = "cp " . $file->[1] . " $mountPoint" . $file->[2]; `$cmd`;
print $cmd, "\n";
}else{
unless ( -d ( $mountPoint . dirname($file->[2]) ) ){
$cmd = "mkdir -p $mountPoint" . dirname($file->[2]); `$cmd`;
print $cmd, "\n";
}
$cmd = "cd $mountPoint" . dirname($file->[2])
. "; ln -f -s " . $file->[1] . " " . basename($file->[2]); `$cmd`;
print $cmd, "\n";
}
}
sub TakeSO{
my $fileName = shift;
if ( $fileName !~ m{^/} ){
$fileName = $pwd . $fileName;
}
unless ( -e $fileName && -r $fileName ) {
print STDERR "Error! $fileName not exists or can't read it.\n";
return undef;
}
if ( -l $fileName ){
my $fileinfo = `/bin/ls -l $fileName`;
unless ( $fileinfo =~ /(\S+) -> (\S+)/ ){
print "Oddness! ls -l $fileName output:\n$fileinfo";
return undef;
}
if ( ! defined &TakeSO( dirname($1) . "/" . $2 ) ) {
return undef;
}
push( @needCopyFiles, [ 'link', $2, $fileName ] );
return $fileName;
}
elsif ( -f $fileName ){
push( @needCopyFiles, [ 'file', $fileName, dirname($fileName) ] );
return $fileName;
}
else{
print STDERR "Error! don't know the type of $fileName.\n";
return undef;
}
} |
[ 本帖最后由 amoyppa 于 2008-10-31 13:50 编辑 ] |
|