aboutsummaryrefslogtreecommitdiff
path: root/mkrootfs.sh
blob: d6c54fcb7e822881a490283c1293f03a7a21f9ed (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
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
#!/bin/sh -e
# shellcheck disable=1090,1091

# Bootstrapper for Carbs Linux
# See LICENSE file for copyright and license details

# Source the package manager library.
. cpt-lib

# Functions
msg() { printf '\033[1;35m-> \033[m%s\n' "$@" ;}
die() { printf '\033[1;31m!> ERROR: \033[m%s\n' "$@" >&2; exit 1 ;}


# Let's get current working directory
BASEDIR="$PWD"

# If there is no config file, we copy config.def
# to config. After we make sure config file is
# in place, we source the contents
! [ -e config ] && cp config.def config
. "${0%/*}/config"

# Check whether absolutely required variables are set.
[ "$PKGS" ]   || die "You must set PKGS variable to continue the bootstrapper"
[ "$MNTDIR" ] || die "You must specify fakeroot location 'MNTDIR' in order to continue the bootstrapper"

# Word splitting is intentional here
# shellcheck disable=2086
pkg_order $PKGS

# Print variables from the configuration file
# shellcheck disable=2154
cat <<EOF
Here are the configuration values:

MNTDIR    = $MNTDIR

Build Options
CFLAGS    = $CFLAGS
CXXFLAGS  = $CXXFLAGS
MAKEFLAGS = $MAKEFLAGS

Repository and package options

REPO            = $REPO
REPOSITORY PATH = $HOST_REPO_PATH
PKGS            = $PKGS
ORDER           = $order

EOF

# If there is NOCONFIRM, skip the prompt.
[ "$NOCONFIRM" ] || {
    printf '\033[1;33m?> \033[mDo you want to start the bootstrapper? (y/N)\n'
    read -r ans
    case "$ans" in [Yy]*|'') ;; *) die "User exited" ; esac
}

# Script starts here

msg "Starting Script..."
msg "Setting CPT_ROOT to $MNTDIR"
export CPT_ROOT="$MNTDIR"

# Check whether REPO and REPO_PATH variables exist
[ "$REPO" ] || die "REPO variable is not set"

# Create parent directories for the repositories, and
# remove pre-existing repositories. We then shallow
# clone the repositories to both locations.
case $REPO in
    rsync://*)
        msg "Acquiring repository"
        mkdir -p "$MNTDIR/var/db/cpt" /tmp
        rm -rf /tmp/repo "$MNTDIR/var/db/cpt/repo"
        rsync -avCz --include=core --delete "$REPO/" /tmp/repo
        cp -r /tmp/repo "$MNTDIR/var/db/cpt/repo"
    ;;
    *)
        msg "Cloning repository"
        mkdir -p "$MNTDIR/var/db/cpt" /tmp
        rm -rf /tmp/repo "$MNTDIR/var/db/cpt/repo"
        git clone --depth 1 "$REPO" /tmp/repo
        cp -r /tmp/repo "$MNTDIR/var/db/cpt/repo"
esac

# Install extra repositories defined in a 'repositories'
# file if it exists. The file is formed by these three
# space seperated sections:
#
# 1: URI of git repository
# 2: The location where the repository will be cloned.
# 3: Options for the git clone, space seperation is not important.
#
[ -f repositories ] &&
while read -r repourl repodir gitopts; do
    # We already die if MNTDIR doesn't exist
    # shellcheck disable=2115
    rm -rf "$MNTDIR/$repodir"
    mkdir -p "$MNTDIR/${repodir%/*}"

    # We want word splitting here.
    # shellcheck disable=2086
    git clone $gitopts -- "$repourl" "$MNTDIR/$repodir"
done < repositories


# We export the new CPT_PATH
export CPT_PATH="${HOST_REPO_PATH:-/tmp/repo/core}"

msg "Starting build from the PKGS variable"


# shellcheck disable=2154
for pkg in $order; do
    # Get the package directory so we can get version
    # and release numbers.
    pkgdir=$(cpt s --single "$pkg")
    read -r ver rel < "$pkgdir/version"

    # Check if the package is already installed and skip.
    [ "$(cpt l "$pkg")" = "$pkg $ver $rel" ] && continue

    # Check if a prebuild tarball exists, build the package
    # if it doesn't exist.
    #
    # pkg_order should be dealing with packages in a way that
    # no prompts are asked, but let's not take any chances
    # either.
    pkg_isbuilt "$pkg" || CPT_NOPROMPT=1 cpt b "$pkg"
    CPT_NOPROMPT=1 cpt i "$pkg"
done

# You can check out about post-installation
# from the configuration file
msg "Installation Complete, starting custombuild procedure if there is one"
postinstall

msg "Generating rootfs to $BASEDIR"
(
    cd "$MNTDIR" || die "Could not change directory to $MNTDIR"
    tar -cJf "$BASEDIR/carbs-rootfs-$(date +%Y%m%d)-$(uname -m).tar.xz" .
)
msg "Done!"