aboutsummaryrefslogtreecommitdiff
path: root/contrib/cpt-chbuild
blob: 7a71c33a4b6d40bded3ce93e08cbdb5e431db8e8 (plain)
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
#!/bin/sh -e
# Create/destroy temporary chroots

## SYNOPSIS:
## .Nm
## .Op Fl r
## .Op Ar pkg...

## DESCRIPTION:
## .Nm
## creates a temporary chroot using the rootfs tarball of Carbs Linux. If the
## tarball does
## .Em NOT
## exist in the user's cache directory, it will download it from the Carbs Linux
## website. If any packages are given as arguments,
## .Nm
## will install those packages to this temporary chroot. If the
## .Fl r
## flag is given,
## .Nm
## will remove the rootfs tarball and directory to download it again.

parser_definition() {
    setup REST help:usage -- "usage: ${0##*/} [-r] [pkg...]"
    flag redownload -r        hidden:1
    global_options silent
}

# shellcheck source=../src/cpt-lib
. cpt-lib

die() {
    rm -f carbs-rootfs.tar.xz carbs-rootfs.tar.xz.sum

    log "$@"
    log "Re-run 'cpt-chbuild' to try again."

    exit 1
}

case "$(uname -m)" in i*86) arch=i686; esac
url="https://dl.carbslinux.org/releases/${arch:-$(uname -m)}/carbs-rootfs.tar.xz"

cd "${cac_dir:?}"

# Remove the existing tarball and the chroot directory, so that they can be
# downloaded again.
[ "$redownload" ] && as_root rm -rf carbs-rootfs.tar.xz \
                                    carbs-rootfs.tar.xz.sum \
                                    carbs-chroot

[ -f carbs-rootfs.tar.xz ] || {
    log "Downloading chroot tarball"
    pkg_download "$url"
}

[ -f carbs-rootfs.tar.xz.sum ] || {
    log "Downloading checksums"
    pkg_download "$url.sha256" carbs-rootfs.tar.xz.sum
}

# We don't want to create the rootfs as a non-priviliged user, because there may
# arise certain problems if the files inside the chroot don't belong to root.
[ "$uid" = 0 ] || {
    as_root "$0" "$@"
    exit $?
}

[ -d carbs-chroot ] || {
    log "Verifying checksums"
    sh256 carbs-rootfs.tar.xz | diff - carbs-rootfs.tar.xz.sum ||
        die "Checksum verification failed"

    log "Extracting chroot"
    mkdir -p carbs-chroot
    (cd carbs-chroot; xz -cd ../carbs-rootfs.tar.xz | pax -r)
}

mkdir -p "${tmp_dir:?}"

log "Creating temporary chroot"
mkdir -p "${chr_dir:=$tmp_dir/chroot}"
rsync -a carbs-chroot/ "$chr_dir"

[ "$1" ] && {
    log "Installing extra packages"
    CPT_ROOT=$chr_dir cpt-install "$@"
}

run_hook pre-chroot "" "$chr_dir"

log "Entering chroot"
cpt-chroot "$chr_dir"
rm -rf "$chr_dir"