aboutsummaryrefslogtreecommitdiff
path: root/common/xbps-src/shutils/chroot.sh
blob: 7e28f1120dd105b2cb97db6a81cd53ec3a740e31 (plain) (blame)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# vim: set ts=4 sw=4 et:

# FIXME: $XBPS_FFLAGS is not set when chroot_init() is run
# It is set in common/build-profiles/bootstrap.sh but lost somewhere?
chroot_init() {
    mkdir -p $XBPS_MASTERDIR/etc/xbps

    cat > $XBPS_MASTERDIR/etc/xbps/xbps-src.conf <<_EOF
# Generated configuration file by xbps-src, DO NOT EDIT!
$(grep -E '^XBPS_.*' "$XBPS_CONFIG_FILE")
XBPS_MASTERDIR=/
XBPS_CFLAGS="$XBPS_CFLAGS"
XBPS_CXXFLAGS="$XBPS_CXXFLAGS"
XBPS_FFLAGS="-fPIC -pipe"
XBPS_CPPFLAGS="$XBPS_CPPFLAGS"
XBPS_LDFLAGS="$XBPS_LDFLAGS"
XBPS_HOSTDIR=/host
# End of configuration file.
_EOF

    # Create custom script to start the chroot bash shell.
    cat > $XBPS_MASTERDIR/bin/xbps-shell <<_EOF
#!/bin/sh

XBPS_SRC_VERSION="$XBPS_SRC_VERSION"

. /etc/xbps/xbps-src.conf

PATH=/void-packages:/usr/bin

exec env -i -- SHELL=/bin/sh PATH="\$PATH" DISTCC_HOSTS="\$XBPS_DISTCC_HOSTS" DISTCC_DIR="/host/distcc" \
    ${XBPS_ARCH+XBPS_ARCH=$XBPS_ARCH} ${XBPS_CHECK_PKGS+XBPS_CHECK_PKGS=$XBPS_CHECK_PKGS} \
    CCACHE_DIR="/host/ccache" IN_CHROOT=1 LC_COLLATE=C LANG=en_US.UTF-8 TERM=linux HOME="/tmp" \
    PS1="[\u@$XBPS_MASTERDIR \W]$ " /bin/bash +h
_EOF

    chmod 755 $XBPS_MASTERDIR/bin/xbps-shell

    cp -f /etc/resolv.conf $XBPS_MASTERDIR/etc

    # Update xbps alternative repository if set.
    mkdir -p $XBPS_MASTERDIR/etc/xbps.d
    if [ -n "$XBPS_ALT_REPOSITORY" ]; then
        ( \
            echo "repository=/host/binpkgs/${XBPS_ALT_REPOSITORY}"; \
            echo "repository=/host/binpkgs/${XBPS_ALT_REPOSITORY}/nonfree"; \
            echo "repository=/host/binpkgs/${XBPS_ALT_REPOSITORY}/debug"; \
            ) > $XBPS_MASTERDIR/etc/xbps.d/00-repository-alternative.conf
        if [ "$XBPS_MACHINE" = "x86_64" ]; then
            ( \
                echo "repository=/host/binpkgs/${XBPS_ALT_REPOSITORY}/multilib"; \
                echo "repository=/host/binpkgs/${XBPS_ALT_REPOSITORY}/multilib/nonfree"; \
            ) >> $XBPS_MASTERDIR/etc/xbps.d/00-repository-alternative.conf
        fi
    else
        rm -f $XBPS_MASTERDIR/etc/xbps.d/00-repository-alternative.conf
    fi
}

chroot_prepare() {
    local f=

    if [ -f $XBPS_MASTERDIR/.xbps_chroot_init ]; then
        return 0
    elif [ ! -f $XBPS_MASTERDIR/bin/bash ]; then
        msg_error "Bootstrap not installed in $XBPS_MASTERDIR, can't continue.\n"
    fi

    # Create some required files.
    if [ -f /etc/localtime ]; then
        cp -f /etc/localtime $XBPS_MASTERDIR/etc
    elif [ -f /usr/share/zoneinfo/UTC ]; then
        cp -f /usr/share/zoneinfo/UTC $XBPS_MASTERDIR/etc/localtime
    fi

    for f in dev sys proc host boot; do
        [ ! -d $XBPS_MASTERDIR/$f ] && mkdir -p $XBPS_MASTERDIR/$f
    done

    # Copy /etc/passwd and /etc/group from base-files.
    cp -f $XBPS_SRCPKGDIR/base-files/files/passwd $XBPS_MASTERDIR/etc
    echo "$(whoami):x:$(id -u):$(id -g):$(whoami) user:/tmp:/bin/xbps-shell" \
        >> $XBPS_MASTERDIR/etc/passwd
    cp -f $XBPS_SRCPKGDIR/base-files/files/group $XBPS_MASTERDIR/etc
    echo "$(whoami):x:$(id -g):" >> $XBPS_MASTERDIR/etc/group

    # Copy /etc/hosts from base-files.
    cp -f $XBPS_SRCPKGDIR/base-files/files/hosts $XBPS_MASTERDIR/etc

    mkdir -p $XBPS_MASTERDIR/etc/xbps.d
    echo "syslog=false" >> $XBPS_MASTERDIR/etc/xbps.d/xbps.conf
    echo "cachedir=/host/repocache" >> $XBPS_MASTERDIR/etc/xbps.d/xbps.conf
    ln -sf /dev/null $XBPS_MASTERDIR/etc/xbps.d/00-repository-main.conf

    # Prepare default locale: en_US.UTF-8.
    if [ -s ${XBPS_MASTERDIR}/etc/default/libc-locales ]; then
        echo 'en_US.UTF-8 UTF-8' >> ${XBPS_MASTERDIR}/etc/default/libc-locales
    fi

    touch -f $XBPS_MASTERDIR/.xbps_chroot_init
    [ -n "$1" ] && echo $1 >> $XBPS_MASTERDIR/.xbps_chroot_init

    return 0
}

chroot_sync_repos() {
    local f=

    # Copy xbps configuration files to the masterdir.
    install -Dm644 ${XBPS_DISTDIR}/etc/xbps.conf \
        ${XBPS_MASTERDIR}/etc/xbps.d/00-xbps-src.conf
    install -Dm644 ${XBPS_DISTDIR}/etc/repos-local.conf \
        ${XBPS_MASTERDIR}/etc/xbps.d/10-repository-local.conf
    install -Dm644 ${XBPS_DISTDIR}/etc/repos-remote.conf \
        ${XBPS_MASTERDIR}/etc/xbps.d/20-repository-remote.conf

    if [ "$XBPS_MACHINE" = "x86_64" ]; then
        install -Dm644 ${XBPS_DISTDIR}/etc/repos-local-x86_64.conf \
            ${XBPS_MASTERDIR}/etc/xbps.d/12-repository-local-x86_64.conf
        install -Dm644 ${XBPS_DISTDIR}/etc/repos-remote-x86_64.conf \
            ${XBPS_MASTERDIR}/etc/xbps.d/22-repository-remote-x86_64.conf
    fi

    # if -N is set, get rid of remote repos from x86_64 (glibc).
    if [ -n "$XBPS_SKIP_REMOTEREPOS" ]; then
        rm -f ${XBPS_MASTERDIR}/etc/xbps.d/20-repository-remote.conf
        rm -f ${XBPS_MASTERDIR}/etc/xbps.d/22-repository-remote-x86_64.conf
    fi

    # Copy host repos to the cross root.
    if [ -n "$XBPS_CROSS_BUILD" ]; then
        rm -rf $XBPS_MASTERDIR/$XBPS_CROSS_BASE/etc/xbps.d
        mkdir -p $XBPS_MASTERDIR/$XBPS_CROSS_BASE/etc/xbps.d
        cp ${XBPS_MASTERDIR}/etc/xbps.d/*.conf \
            $XBPS_MASTERDIR/$XBPS_CROSS_BASE/etc/xbps.d
        rm -f $XBPS_MASTERDIR/$XBPS_CROSS_BASE/etc/xbps.d/*-x86_64.conf
    fi

    if [ -z "$XBPS_SKIP_REMOTEREPOS" ]; then
        # Make sure to sync index for remote repositories.
        xbps-install -r $XBPS_MASTERDIR -S
    fi

    if [ -n "$XBPS_CROSS_BUILD" ]; then
        # Copy host keys to the target rootdir.
        mkdir -p $XBPS_MASTERDIR/$XBPS_CROSS_BASE/var/db/xbps/keys
        cp $XBPS_MASTERDIR/var/db/xbps/keys/*.plist \
            $XBPS_MASTERDIR/$XBPS_CROSS_BASE/var/db/xbps/keys
        # Make sure to sync index for remote repositories.
        if [ -z "$XBPS_SKIP_REMOTEREPOS" ]; then
            env -- XBPS_TARGET_ARCH=$XBPS_TARGET_MACHINE \
                xbps-install -r $XBPS_MASTERDIR/$XBPS_CROSS_BASE -S
        fi
    fi

    return 0
}

chroot_handler() {
    local action="$1" pkg="$2" rv=0 arg= _envargs=

    if [ -n "$IN_CHROOT" -o -z "$CHROOT_READY" ]; then
        return 0
    fi
    if [ ! -d $XBPS_MASTERDIR/void-packages ]; then
        mkdir -p $XBPS_MASTERDIR/void-packages
    fi

    [ -z "$action" -a -z "$pkg" ] && return 1

    case "$action" in
        fetch|extract|patch|configure|build|check|install|pkg|bootstrap-update|chroot)
            chroot_prepare || return $?
            chroot_init || return $?
            chroot_sync_repos || return $?
            ;;
    esac

    if [ "$action" = "chroot" ]; then
        $XBPS_COMMONDIR/chroot-style/${XBPS_CHROOT_CMD:=uunshare}.sh \
            $XBPS_MASTERDIR $XBPS_DISTDIR "$XBPS_HOSTDIR" "$XBPS_CHROOT_CMD_ARGS" /bin/xbps-shell
        rv=$?
    else
        env -i -- PATH="/usr/bin" SHELL=/bin/sh \
            HOME=/tmp IN_CHROOT=1 LC_COLLATE=C LANG=en_US.UTF-8 \
            SOURCE_DATE_EPOCH="$SOURCE_DATE_EPOCH" \
            XBPS_ALLOW_CHROOT_BREAKOUT="$XBPS_ALLOW_CHROOT_BREAKOUT" \
            $XBPS_COMMONDIR/chroot-style/${XBPS_CHROOT_CMD:=uunshare}.sh \
            $XBPS_MASTERDIR $XBPS_DISTDIR "$XBPS_HOSTDIR" "$XBPS_CHROOT_CMD_ARGS" \
            /void-packages/xbps-src $XBPS_OPTIONS $action $pkg
        rv=$?
    fi

    return $rv
}