1821245 Members
2698 Online
109632 Solutions
New Discussion юеВ

Re: how copy a symlink ?

 
SOLVED
Go to solution
totoperdu
Frequent Advisor

how copy a symlink ?

hello,

I want to copy the file /lib/ld-linux.so.2 but it's not easy because it's a symlink.

The real problem is to automatically copy the target of this symlink (it's for chrooting).

ls -l gives me:
/lib/ld-linux.so.2 -> ld-2.3.4.so

in fact, i want to copy the target (ld-2.3.4.so) with the symlink (ld-linux.so.2).
how can i do that ?

help needed.

--
Cheers,
Cedrick Gaillard

5 REPLIES 5
Steven E. Protter
Exalted Contributor

Re: how copy a symlink ?

Shalom Cedrik,

You can try cp -R the directoy in which the symlink is in. That should work. If it does not, you may need to re-create the link with the ln -s commmand.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
totoperdu
Frequent Advisor

Re: how copy a symlink ?

it will be too simplistic, i want a minimal chroot environment and i'd like to search and copy the needed libraries automatically.

An example for useradd who returns the needed librairies who are symlink whith the real target:
root@toto ~]# ldd -v /usr/sbin/useradd |grep "=>" |awk -F"=>" '{print $2}' |awk '{print $1}' |sort -u |xargs ls -l|grep " -> " |awk '{print $9" "$11}'
/lib/ld-linux.so.2 ld-2.3.4.so
/lib/libaudit.so.0 libaudit.so.0.0.0
/lib/libcrypt.so.1 libcrypt-2.3.4.so
/lib/tls/libc.so.6 libc-2.3.4.so

i don't know how to use this result for concatenate the basename and the last field for integrates it in a script, i'm definitively not a guru ;)

--
Cheers,
Cedrick Gaillard
Mike Stroyan
Honored Contributor
Solution

Re: how copy a symlink ?

The readlink command will let you find the value of a symbolic link. The -f option will expand to a full path. But that option also follows through multiple symlinks. If you see a program using a library through multiple symlinks then you would need to copy every step in the symlink chain into the chroot. This script handles that by following one symlink at a time and adding a directory name for relative paths.

$ cat libs.sh
#!/bin/bash
for program in "$@"
do
echo "$program"
ldd -v "$program" | (
while read f
do
if [[ "$f" =~ ' => [^ ]' ]]
then
f="${f#?* => }"
f="${f% ?*}"
echo "$f"
while [[ -h "$f" ]]
do
dir="$(dirname "$f")"
f="$(readlink "$f")"
if [[ ! "$f" =~ '^/' ]]
then
f="$dir/$f"
fi
echo " $f"
done
fi
done
)
done

You can do the copy itself with "cp -d" to make a new symbolic link that matches a current one.
totoperdu
Frequent Advisor

Re: how copy a symlink ?

a big thanks Mike ;)

your script is too complicated for my knowledge but the -f flag for readlink seems to be enough for what i want to do, very interesting parameter.

thanks again.

--
Cedrick Gaillard
totoperdu
Frequent Advisor

Re: how copy a symlink ?

with 'readlink -f' i can do:
# export bin2chr="/usr/sbin/adduser" homechr="/home/newroot"
# mkdir -p `dirname ${homechr}${bin2chr}`
# cp -av $bin2chr ${homechr}${bin2chr}
# for i in `ldd -v ${bin2chr} |grep "=>" |awk -F"=>" '{print $2}' |\
# awk '{print $1}' |sort -u`; do dirdest="${homechr}`readlink -f $i`"; \
# mkdir -p `dirname $dirdest`; cp -av `readlink -f $i` $dirdest; done
# for i in `ldd -v ${bin2chr} |grep "=>" |awk -F"=>" '{print $2}' |\
# awk '{print $1}' |sort -u`; do cp -av $i ${homechr}${i}; done

thanks the help you have gived me :)

--
Cheers,
Cedrick Gaillard