1.找到命令的源文件路径
[[email protected] ~]# which ls alias ls=‘ls --color=auto‘ /bin/ls [[email protected] ~]# which cat /bin/cat #有的命令使用了别名,所以有alias相关信息,要把alias相关的去除 [[email protected] ~]# [[email protected] ~]# which ls | grep -v "alias" #使用grep -v 去除alias /bin/ls [[email protected] ~]# which ls | grep -v "alias" | awk ‘{print $1}‘ #使用这个命令就能输出源文件路径 /bin/ls
2.找到命令依赖的文件
[[email protected] ~]# ldd /bin/ls #查看/bin/ls执行的时候依赖库,可以看到这儿有用的就是包含/lib64的 [[email protected] ~]# ldd /bin/ls | grep -o "/lib64.*" #grep -o只显示匹配的 [[email protected] ~]# ldd /bin/ls | grep -o "/lib64.*" | awk ‘{print $1}‘ #使用此命令就能输出命令依赖的库文件 #/bin/ls取自which ls | grep -v "alias" | awk ‘{print $1}‘
3.移植命令脚本
现在知道要移植一个命令,需要移植命令源文件和所依赖的库。bincp移植命令源文件,libcp对应移植依赖库文件,创建下面的脚本并执行。
dirname功能是取文件的路径名称。
移植完成后使用chroot测试。
[[email protected] tmp]# vi /tmp/cp1.sh #!/bin/bash # DEST=/mnt/sysroot #设置移植的目的地 libcp() { LIBPATH=`dirname ${1}` [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished." } bincp() { CMDPATH=`dirname ${1}` [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH [ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH for LIB in `ldd $1 | grep -o "/lib64.*" | awk ‘{print $1}‘`; do libcp $LIB done } read -p "Your command: " CMD until [ $CMD == ‘q‘ ]; do ! which $CMD && echo "Wrong command" && read -p "Input again:" CMD && continue COMMAND=`which $CMD | grep -v "alias" | awk ‘{print $1}‘` bincp $COMMAND echo "copy $COMMAND finished." read -p "Continue: " CMD done
时间: 2024-10-14 17:18:51