Linux 批量依赖库拷贝(ldd)

3 minute read

背景

如何将程序依赖的动态库拷贝到指定的目录?

例子

1、通过LDD查看依赖的动态库。

# ldd postgres  
        linux-vdso.so.1 =>  (0x00007ffcfc324000)  
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb6151ba000)  
        libxml2.so.2 => /lib64/libxml2.so.2 (0x00007fb614e4f000)  
        libpam.so.0 => /lib64/libpam.so.0 (0x00007fb614c40000)  
        libssl.so.10 => /lib64/libssl.so.10 (0x00007fb6149ce000)  
        libcrypto.so.10 => /lib64/libcrypto.so.10 (0x00007fb61456c000)  
        libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007fb61431f000)  
        librt.so.1 => /lib64/librt.so.1 (0x00007fb614117000)  
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fb613f12000)  
        libm.so.6 => /lib64/libm.so.6 (0x00007fb613c10000)  
        libldap-2.4.so.2 => /lib64/libldap-2.4.so.2 (0x00007fb6139bc000)  
        libicui18n.so.50 => /lib64/libicui18n.so.50 (0x00007fb6135bc000)  
        libicuuc.so.50 => /lib64/libicuuc.so.50 (0x00007fb613243000)  
        libsystemd.so.0 => /lib64/libsystemd.so.0 (0x00007fb61321b000)  
        libc.so.6 => /lib64/libc.so.6 (0x00007fb612e59000)  
        /lib64/ld-linux-x86-64.so.2 (0x00007fb6153e1000)  
        libz.so.1 => /lib64/libz.so.1 (0x00007fb612c43000)  
        liblzma.so.5 => /lib64/liblzma.so.5 (0x00007fb612a1d000)  
        libaudit.so.1 => /lib64/libaudit.so.1 (0x00007fb6127f4000)  
        libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007fb61250c000)  
        libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fb612308000)  
        libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007fb6120d4000)  
        libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007fb611ec6000)  
        libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fb611cc2000)  
        libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fb611aa7000)  
        liblber-2.4.so.2 => /lib64/liblber-2.4.so.2 (0x00007fb611898000)  
        libsasl2.so.3 => /lib64/libsasl2.so.3 (0x00007fb61167b000)  
        libssl3.so => /lib64/libssl3.so (0x00007fb61142e000)  
        libsmime3.so => /lib64/libsmime3.so (0x00007fb611207000)  
        libnss3.so => /lib64/libnss3.so (0x00007fb610edd000)  
        libnssutil3.so => /lib64/libnssutil3.so (0x00007fb610caf000)  
        libplds4.so => /lib64/libplds4.so (0x00007fb610aab000)  
        libplc4.so => /lib64/libplc4.so (0x00007fb6108a6000)  
        libnspr4.so => /lib64/libnspr4.so (0x00007fb610667000)  
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fb61035f000)  
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fb610149000)  
        libicudata.so.50 => /lib64/libicudata.so.50 (0x00007fb60eb74000)  
        libcap.so.2 => /lib64/libcap.so.2 (0x00007fb60e96f000)  
        libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fb60e748000)  
        libgcrypt.so.11 => /lib64/libgcrypt.so.11 (0x00007fb60e4c6000)  
        libgpg-error.so.0 => /lib64/libgpg-error.so.0 (0x00007fb60e2c1000)  
        libdw.so.1 => /lib64/libdw.so.1 (0x00007fb60e079000)  
        libcap-ng.so.0 => /lib64/libcap-ng.so.0 (0x00007fb60de72000)  
        libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fb60dc3b000)  
        libattr.so.1 => /lib64/libattr.so.1 (0x00007fb60da35000)  
        libpcre.so.1 => /lib64/libpcre.so.1 (0x00007fb60d7d3000)  
        libelf.so.1 => /lib64/libelf.so.1 (0x00007fb60d5bb000)  
        libbz2.so.1 => /lib64/libbz2.so.1 (0x00007fb60d3aa000)  
        libfreebl3.so => /lib64/libfreebl3.so (0x00007fb60d1a7000)  

2、写一个SHELL脚本,自动拷贝依赖的动态库。

vi test.sh  
  
deplist=$( ldd $1 | awk '{if (match($3,"/")){ print $3}}' )  
cp -L -n $deplist $2   
-L 表示拷贝文件(如果是软链,会拷贝到最终的文件)。  
-n 表示不覆盖已有文件。  

3、创建目标目录。

mkdir /tmp/lib  

4、将postgres依赖的动态库拷贝到目标目录

. ./test.sh /usr/pgsql-10/bin/postgres /tmp/lib/  
ll /tmp/lib  
  
total 39332  
-rwxr-xr-x 1 root root    19888 Oct 25 13:01 libattr.so.1  
-rwxr-xr-x 1 root root   122936 Oct 25 13:01 libaudit.so.1  
-rwxr-xr-x 1 root root    68192 Oct 25 13:01 libbz2.so.1  
-rwxr-xr-x 1 root root    23968 Oct 25 13:01 libcap-ng.so.0  
-rwxr-xr-x 1 root root    20024 Oct 25 13:01 libcap.so.2  
-rwxr-xr-x 1 root root    15848 Oct 25 13:01 libcom_err.so.2  
-rwxr-xr-x 1 root root  2512448 Oct 25 13:01 libcrypto.so.10  
-rwxr-xr-x 1 root root    41080 Oct 25 13:01 libcrypt.so.1  
-rwxr-xr-x 1 root root  2118128 Oct 25 13:01 libc.so.6  
-rwxr-xr-x 1 root root    19776 Oct 25 13:01 libdl.so.2  
-rwxr-xr-x 1 root root   301576 Oct 25 13:01 libdw.so.1  
-rwxr-xr-x 1 root root    99952 Oct 25 13:01 libelf.so.1  
-rwxr-xr-x 1 root root    11384 Oct 25 13:01 libfreebl3.so  
-rwxr-xr-x 1 root root    88720 Oct 25 13:01 libgcc_s.so.1  
-rwxr-xr-x 1 root root   535064 Oct 25 13:01 libgcrypt.so.11  
-rwxr-xr-x 1 root root    19384 Oct 25 13:01 libgpg-error.so.0  
-rwxr-xr-x 1 root root   320768 Oct 25 13:01 libgssapi_krb5.so.2  
-rwxr-xr-x 1 root root 20789896 Oct 25 13:01 libicudata.so.50  
-rwxr-xr-x 1 root root  2096056 Oct 25 13:01 libicui18n.so.50  
-rwxr-xr-x 1 root root  1539392 Oct 25 13:01 libicuuc.so.50  
-rwxr-xr-x 1 root root   210768 Oct 25 13:01 libk5crypto.so.3  
-rwxr-xr-x 1 root root    15688 Oct 25 13:01 libkeyutils.so.1  
-rwxr-xr-x 1 root root   963504 Oct 25 13:01 libkrb5.so.3  
-rwxr-xr-x 1 root root    58728 Oct 25 13:01 libkrb5support.so.0  
-rwxr-xr-x 1 root root    61888 Oct 25 13:01 liblber-2.4.so.2  
-rwxr-xr-x 1 root root   348400 Oct 25 13:01 libldap-2.4.so.2  
-rwxr-xr-x 1 root root   157424 Oct 25 13:01 liblzma.so.5  
-rwxr-xr-x 1 root root  1141928 Oct 25 13:01 libm.so.6  
-rwxr-xr-x 1 root root   251784 Oct 25 13:01 libnspr4.so  
-rwxr-xr-x 1 root root  1236800 Oct 25 13:01 libnss3.so  
-rwxr-xr-x 1 root root   186216 Oct 25 13:01 libnssutil3.so  
-rwxr-xr-x 1 root root    61672 Oct 25 13:01 libpam.so.0  
-rwxr-xr-x 1 root root   402384 Oct 25 13:01 libpcre.so.1  
-rwxr-xr-x 1 root root    20048 Oct 25 13:01 libplc4.so  
-rwxr-xr-x 1 root root    15744 Oct 25 13:01 libplds4.so  
-rwxr-xr-x 1 root root   143944 Oct 25 13:01 libpthread.so.0  
-rwxr-xr-x 1 root root   111080 Oct 25 13:01 libresolv.so.2  
-rwxr-xr-x 1 root root    44448 Oct 25 13:01 librt.so.1  
-rwxr-xr-x 1 root root   121328 Oct 25 13:01 libsasl2.so.3  
-rwxr-xr-x 1 root root   155744 Oct 25 13:01 libselinux.so.1  
-rwxr-xr-x 1 root root   164040 Oct 25 13:01 libsmime3.so  
-rwxr-xr-x 1 root root   315072 Oct 25 13:01 libssl3.so  
-rwxr-xr-x 1 root root   470336 Oct 25 13:01 libssl.so.10  
-rwxr-xr-x 1 root root   995840 Oct 25 13:01 libstdc++.so.6  
-rwxr-xr-x 1 root root   162552 Oct 25 13:01 libsystemd.so.0  
-rwxr-xr-x 1 root root  1509376 Oct 25 13:01 libxml2.so.2  
-rwxr-xr-x 1 root root    90664 Oct 25 13:01 libz.so.1  

Flag Counter

digoal’s 大量PostgreSQL文章入口