File: chroot.fakechroot.sh

package info (click to toggle)
fakechroot 2.20.1%2Bds-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,904 kB
  • sloc: ansic: 7,688; sh: 1,917; makefile: 375; perl: 191; java: 5
file content (147 lines) | stat: -rw-r--r-- 6,543 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!@SHELL@

# chroot
#
# Wrapper for chroot command which sets additional LD_LIBRARY_PATH for fake
# chroot environment.  It copies original LD_LIBRARY_PATH and adds prefix to
# each directory for this variable.
#
# (c) 2011, 2013, 2016 Piotr Roszatycki <dexter@debian.org>, LGPL


fakechroot_chroot_load_ldsoconf () {
    fakechroot_chroot_files="$1"
    fakechroot_chroot_newroot="$2"

    for fakechroot_chroot_file in `eval echo $fakechroot_chroot_newroot$fakechroot_chroot_files`; do
        fakechroot_chroot_file="${fakechroot_chroot_file#$fakechroot_chroot_newroot}"

        sed -e 's/#.*//' -e '/^ *$/d' "$fakechroot_chroot_newroot$fakechroot_chroot_file" 2>/dev/null | while read fakechroot_chroot_line; do
            case "$fakechroot_chroot_line" in
                include*)
                    fakechroot_chroot_include=`echo "$fakechroot_chroot_line" | sed -e 's/^include  *//' -e 's/ *$//'`
                    for fakechroot_chroot_incfile in `eval echo $fakechroot_chroot_newroot$fakechroot_chroot_include`; do
                        fakechroot_chroot_incfile="${fakechroot_chroot_incfile#$fakechroot_chroot_newroot}"
                        fakechroot_chroot_load_ldsoconf "$fakechroot_chroot_incfile" "$fakechroot_chroot_newroot"
                    done
                    ;;
                *)
                    echo "$fakechroot_chroot_newroot$fakechroot_chroot_line"
                    ;;
            esac
        done

    done
}


fakechroot_chroot_chroot="${FAKECHROOT_CMD_ORIG:-chroot}"

fakechroot_chroot_base="$FAKECHROOT_BASE_ORIG"

# records the content of LD_LIBRARY_PATH at first chroot invocation
if [ -z "$fakechroot_chroot_base" -a -n "$LD_LIBRARY_PATH" ]; then
    FAKECHROOT_LDLIBPATH="$LD_LIBRARY_PATH"
    export FAKECHROOT_LDLIBPATH
fi

fakechroot_chroot_n=0
for fakechroot_chroot_opt in "$@"; do
    fakechroot_chroot_n=$(($fakechroot_chroot_n + 1))
    case "$fakechroot_chroot_opt" in
        -*)
            continue
            ;;
        *)
            fakechroot_chroot_requested_newroot="$fakechroot_chroot_opt"
            break
            ;;
    esac
done

# absolute paths in fakechroot_chroot_opt are relative to FAKECHROOT_BASE_ORIG
if [ "${fakechroot_chroot_requested_newroot#/}" != "$fakechroot_chroot_requested_newroot" ]; then
    fakechroot_chroot_newroot="${fakechroot_chroot_base}${fakechroot_chroot_requested_newroot}"
else
    fakechroot_chroot_newroot="$fakechroot_chroot_requested_newroot"
fi

if [ -d "$fakechroot_chroot_newroot" ]; then
    fakechroot_chroot_newroot=`cd "$fakechroot_chroot_newroot"; pwd -P`

    fakechroot_chroot_paths=

    # append newroot to each directory from original LD_LIBRARY_PATH
    fakechroot_chroot_IFS_bak="$IFS" IFS=:
    for fakechroot_chroot_d in $FAKECHROOT_LDLIBPATH; do
        fakechroot_chroot_paths="${fakechroot_chroot_paths:+$fakechroot_chroot_paths:}$fakechroot_chroot_newroot/${fakechroot_chroot_d#/}"
    done
    IFS="$fakechroot_chroot_IFS_bak"

    # append newroot to each directory from new /etc/ld.so.conf
    fakechroot_chroot_paths_ldsoconf=""
    if [ -f "$fakechroot_chroot_newroot/etc/ld.so.conf" ]; then
        fakechroot_chroot_paths_ldsoconf=`fakechroot_chroot_load_ldsoconf "/etc/ld.so.conf" "$fakechroot_chroot_newroot" | while read fakechroot_chroot_line; do printf ":%s" "$fakechroot_chroot_line"; done`
    elif [ -d "$fakechroot_chroot_newroot/etc/ld.so.conf.d" ]; then
        fakechroot_chroot_paths_ldsoconf=`fakechroot_chroot_load_ldsoconf "/etc/ld.so.conf.d/*" "$fakechroot_chroot_newroot" | while read fakechroot_chroot_line; do printf ":%s" "$fakechroot_chroot_line"; done`
    fi
    fakechroot_chroot_paths_ldsoconf="${fakechroot_chroot_paths_ldsoconf#:}"

    # append newroot to extra directories because some important commands use runpath
    fakechroot_chroot_IFS_bak="$IFS" IFS=:
    fakechroot_chroot_paths_extra=""
    for fakechroot_chroot_d in ${FAKECHROOT_EXTRA_LIBRARY_PATH:-/lib/systemd:/usr/lib/man-db}; do
        if [ -d "$fakechroot_chroot_newroot$fakechroot_chroot_d" ]; then
            fakechroot_chroot_paths_extra="$fakechroot_chroot_paths_extra${fakechroot_chroot_paths_extra:+:}$fakechroot_chroot_newroot$fakechroot_chroot_d"
        fi
    done
    IFS="$fakechroot_chroot_IFS_bak"

    fakechroot_chroot_paths="$fakechroot_chroot_paths${fakechroot_chroot_paths_ldsoconf:+:$fakechroot_chroot_paths_ldsoconf}${fakechroot_chroot_paths_extra:+:$fakechroot_chroot_paths_extra}${FAKECHROOT_LDLIBPATH:+:$FAKECHROOT_LDLIBPATH}"
    fakechroot_chroot_paths="${fakechroot_chroot_paths#:}"
fi

# correct newroot if we chroot into the root
if [ -n "$FAKECHROOT_BASE_ORIG" -a "$fakechroot_chroot_newroot" = "$FAKECHROOT_BASE_ORIG" ]; then
    fakechroot_chroot_final_newroot="/"
else
    fakechroot_chroot_final_newroot="${fakechroot_chroot_newroot#$FAKECHROOT_BASE_ORIG}"
fi

# call real chroot
if [ -n "$fakechroot_chroot_newroot" ] && [ $fakechroot_chroot_n -le $# ]; then
    if ( test "$1" = "${@:1:$((1+0))}" ) 2>/dev/null; then
        # shell with arrays and built-in expr
        env -u FAKECHROOT_BASE_ORIG FAKECHROOT_CMD_ORIG= LD_LIBRARY_PATH="$fakechroot_chroot_paths" FAKECHROOT_BASE="$fakechroot_chroot_base" \
            "$fakechroot_chroot_chroot" "${@:1:$(($fakechroot_chroot_n - 1))}" "$fakechroot_chroot_final_newroot" "${@:$(($fakechroot_chroot_n + 1))}"
        exit $?
    else
        # POSIX shell
        fakechroot_chroot_args=$#
        for fakechroot_chroot_i in `@SEQ@ 1 $fakechroot_chroot_args`; do
            if [ $fakechroot_chroot_i = $fakechroot_chroot_n ]; then
                fakechroot_chroot_arg="$fakechroot_chroot_final_newroot"
                eval fakechroot_chroot_arg_$fakechroot_chroot_i=\"\$fakechroot_chroot_arg\"
            else
                eval fakechroot_chroot_arg_$fakechroot_chroot_i=\"\$1\"
            fi
            shift
        done

        set --

        for fakechroot_chroot_i in `@SEQ@ 1 $fakechroot_chroot_args`; do
            eval fakechroot_chroot_arg=\"\$fakechroot_chroot_arg_$fakechroot_chroot_i\"
            set -- "$@" "$fakechroot_chroot_arg"
        done

        env -u FAKECHROOT_BASE_ORIG FAKECHROOT_CMD_ORIG= LD_LIBRARY_PATH="$fakechroot_chroot_paths" FAKECHROOT_BASE="$fakechroot_chroot_base" \
            "$fakechroot_chroot_chroot" "$@"
        exit $?
    fi
else
    # original arguments
    env -u FAKECHROOT_BASE_ORIG FAKECHROOT_CMD_ORIG= LD_LIBRARY_PATH="$fakechroot_chroot_paths" FAKECHROOT_BASE="$fakechroot_chroot_base" \
        "$fakechroot_chroot_chroot" "$@"
    exit $?
fi