aboutsummaryrefslogtreecommitdiffstats
path: root/hyper-bootstrap_v0.3.sh
blob: a8f35952fb578baa9bde2477ce946b7e594a1335 (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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
#
# hyperbola-bootstrap: Bootstrap a base Hyperbola GNU+Linux-libre system using any GNU distribution.
#
# Dependencies: bash >= 4, coreutils, wget, sed, gawk, tar, gzip, chroot, xz, zstd.
# Project: https://git.sr.ht/~heckyel/hyperbola-bootstrap
#
# Usage:
#
#   # ./hyper-bootstrap.sh destination
#   # ./hyper-bootstrap.sh -a x86_64 -r https://repo.hyperbola.info:50011/gnu-plus-linux-libre/testing destination-64
#
# Example:
#
#   # ./hyper-bootstrap.sh -a x86_64 -r "https://mirror.fsf.org/hyperbola/gnu-plus-linux-libre/testing" myhyper
#   # ./hyper-bootstrap.sh myhyper
#
# And then you can chroot to the destination directory (user: root, password: root):
#
#   # chroot destination
#
# Note that some packages require some system directories to be mounted. Some of the commands you can try:
#
#   # mount --bind /proc myhyper/proc
#   # mount --bind /sys myhyper/sys
#   # mount --bind /dev myhyper/dev
#   # mount --bind /dev/pts myhyper/dev/pts
#

set -e -u -o pipefail

# Packages needed by pacman (see get-pacman-dependencies.sh)
PACMAN_PACKAGES=(
    bash acl hyperbola-keyring attr bzip2 curl expat glibc gpgme libarchive grep sed coreutils
    libassuan libgpg-error libnghttp2 libssh2 lzo libressl pacman pacman-mirrorlist xz zlib libffi
    krb5 e2fsprogs keyutils libidn gcc-libs lz4 libpsl icu readline libunistring findutils
    ncurses pinentry lsb-release ca-certificates ca-certificates-utils p11-kit libtasn1
    libcap shadow pcre
)
CORE_PACKAGES=(${PACMAN_PACKAGES[*]} filesystem)
COMMUNITY_PACKAGES=(lzip zstd)
EXTRA_PACKAGES=(gawk file tar openrc)
DEFAULT_REPO_URL="https://mirror.fsf.org/hyperbola/gnu-plus-linux-libre/stable"

stderr() {
    echo "$@" >&2
}

debug() {
    echo -e "\e[1;32m==>\e[0m\033[1m $* \e[m"
}

extract_href() {
    sed -n '/<a / s/^.*<a [^>]*href="\([^\"]*\)".*$/\1/p'
}

fetch() {
    curl -L -s "$@"
}

fetch_file() {
    local FILEPATH=$1
    shift
    if [[ -e "$FILEPATH" ]]; then
        curl -L -z "$FILEPATH" -o "$FILEPATH" "$@"
    else
        curl -L -o "$FILEPATH" "$@"
    fi
}

uncompress() {
    local FILEPATH=$1 DEST=$2

    case "$FILEPATH" in
        *.gz)
            tar xzf "$FILEPATH" -C "$DEST";;
        *.xz)
            # tar xzf "$FILEPATH" -C "$DEST" ;;
            tar -xf "$FILEPATH" -C "$DEST" > /dev/null 2> /dev/null;;
        *.lz)
            tar xf "$FILEPATH" -C "$DEST";;
        *.zst)
            zstd -dc "$FILEPATH" | tar x -C "$DEST";;
        *)
            debug "Error: unknown package format: $FILEPATH"
            return 1;;
    esac
}

###

get_default_repo() {
    local ARCH=$1
    if [[ "$ARCH" == x86* || "$ARCH" == i686 ]]; then
        echo $DEFAULT_REPO_URL
    fi
}

get_core_repo_url() {
    local REPO_URL=$1 ARCH=$2
    if [[ "$ARCH" == x86* || "$ARCH" == i686 ]]; then
        echo "${REPO_URL%/}/core/os/$ARCH"
    fi
}

get_extra_repo_url() {
    local REPO_URL=$1 ARCH=$2
    if [[ "$ARCH" == x86* || "$ARCH" == i686 ]]; then
        echo "${REPO_URL%/}/extra/os/$ARCH"
    fi
}

get_community_repo_url() {
    local REPO_URL=$1 ARCH=$2
    if [[ "$ARCH" == x86* || "$ARCH" == i686 ]]; then
        echo "${REPO_URL%/}/community/os/$ARCH"
    fi
}

get_template_repo_url() {
    local REPO_URL=$1 ARCH=$2
    if [[ "$ARCH" == x86* || "$ARCH" == i686 ]]; then
        echo "${REPO_URL%/}/\$repo/os/$ARCH"
    fi
}

configure_pacman() {
    local DEST=$1 ARCH=$2
    debug "Configuring SERVER"
    SERVER=$(get_template_repo_url "$REPO_URL" "$ARCH")
    echo "Server = $SERVER" > "$DEST/etc/pacman.d/mirrorlist"
    debug "Configuring CERT"
    cp -fv certs/1.pem "$DEST/etc/ca-certificates/extracted/tls-ca-bundle.pem"
}

clean_chroot() {
    local DEST=$1
    debug "Clean Chroot"
    rm -rf "$DEST/.BUILDINFO" "$DEST/.INSTALL" "$DEST/.MTREE" "$DEST/.PKGINFO" || true
}

configure_minimal_system() {
    local DEST=$1

    mkdir -p "$DEST/dev"
    sed -ie 's|^root:.*$|root:$1$GT9AUpJe$oXANVIjIzcnmOpY07iaGi/:14657::::::|' "$DEST/etc/shadow"
    touch "$DEST/etc/group"
    echo "bootstrap" > "$DEST/etc/hostname"

    rm -f "$DEST/etc/mtab"
    echo "rootfs / rootfs rw 0 0" > "$DEST/etc/mtab"
    test -e "$DEST/dev/null" || mknod "$DEST/dev/null" c 1 3
    test -e "$DEST/dev/random" || mknod -m 0644 "$DEST/dev/random" c 1 8
    test -e "$DEST/dev/urandom" || mknod -m 0644 "$DEST/dev/urandom" c 1 9

    sed -i "s|^[[:space:]]*\(CheckSpace\)|# \1|" "$DEST/etc/pacman.conf"
    sed -i "s|^[[:space:]]*SigLevel[[:space:]]*=.*$|SigLevel = Never|" "$DEST/etc/pacman.conf"
}

fetch_packages_list() {
    local REPO=$1

    debug "Fetch packages list: $REPO/"
    fetch "$REPO/" | extract_href | awk -F"/" '{print $NF}' | sort -rn ||
        { debug "Error: cannot fetch packages list: $REPO"; return 1; }
}

install_pacman_packages() {
    local BASIC_PACKAGES=$1 DEST=$2 LIST=$3 DOWNLOAD_DIR=$4
    debug "pacman package and dependencies: $BASIC_PACKAGES"

    for PACKAGE in $BASIC_PACKAGES; do
        local FILE=$(echo "$LIST" | grep -m1 "^$PACKAGE-[[:digit:]].*\(\.gz\|\.xz\|\.lz\|\.zst\)$")
        test "$FILE" || { debug "Error: cannot find package: $PACKAGE"; return 1; }
        local FILEPATH="$DOWNLOAD_DIR/$FILE"

        debug "Download package: $REPO/$FILE"
        fetch_file "$FILEPATH" "$REPO/$FILE"
        debug "Uncompress package: $FILEPATH"
        uncompress "$FILEPATH" "$DEST"
    done
}

install_pacman_packages_community() {
    local BASIC_PACKAGES=$1 DEST=$2 LIST=$3 DOWNLOAD_DIR=$4
    debug "pacman package and dependencies: $COMMUNITY_PACKAGES"

    for PACKAGE in $BASIC_PACKAGES; do
        local FILE=$(echo "$LIST" | grep -m1 "^$PACKAGE-[[:digit:]].*\(\.gz\|\.xz\|\.lz\|\.zst\)$")
        test "$FILE" || { debug "Error: cannot find package: $PACKAGE"; return 1; }
        local FILEPATH="$DOWNLOAD_DIR/$FILE"

        debug "Download package: $REPO_COMMUNITY/$FILE"
        fetch_file "$FILEPATH" "$REPO_COMMUNITY/$FILE"
        debug "Uncompress package: $FILEPATH"
        uncompress "$FILEPATH" "$DEST"
    done
}

configure_static_qemu() {
    local ARCH=$1 DEST=$2
    [[ "$ARCH" == arm* ]] && ARCH=arm
    QEMU_STATIC_BIN=$(command -v qemu-$ARCH-static || echo )
    [[ -e "$QEMU_STATIC_BIN" ]] ||\
        { debug "No static qemu for $ARCH, ignoring"; return 0; }
    cp "$QEMU_STATIC_BIN" "$DEST/usr/bin"
}

install_packages() {
    local ARCH=$1 DEST=$2 PACKAGES=$3
    debug "Install packages: $PACKAGES"
    LC_ALL=C chroot "$DEST" \
          /usr/bin/pacman --noconfirm --arch $ARCH -Syy --force $PACKAGES
}

configure_keyring() {
    local DEST=$1
    sed -i 's|SigLevel = Never|SigLevel = Required DatabaseOptional|' "$DEST/etc/pacman.conf"
    LC_ALL=C chroot "$DEST" \
          /usr/bin/pacman-key --init && /usr/bin/pacman-key --populate archlinux hyperbola \
        && /usr/bin/pacman-key --refresh-keys \
        && /usr/bin/pacman -Sy hyperbola-keyring --noconfirm
}

show_usage() {
    stderr "Usage: $(basename "$0") [-q] [-a i686|x86_64|arm] [-r REPO_URL] [-d DOWNLOAD_DIR] DESTDIR"
}

main() {
    # Process arguments and options
    test $# -eq 0 && set -- "-h"
    local ARCH=
    local REPO_URL=
    local USE_QEMU=
    local DOWNLOAD_DIR=
    local PRESERVE_DOWNLOAD_DIR=

    while getopts "qa:r:d:h" ARG; do
        case "$ARG" in
            a) ARCH=$OPTARG;;
            r) REPO_URL=$OPTARG;;
            q) USE_QEMU=true;;
            d) DOWNLOAD_DIR=$OPTARG
               PRESERVE_DOWNLOAD_DIR=true;;
            *) show_usage; return 1;;
        esac
    done
    shift $(($OPTIND-1))
    test $# -eq 1 || { show_usage; return 1; }

    [[ -z "$ARCH" ]] && ARCH=$(uname -m)
    [[ -z "$REPO_URL" ]] && REPO_URL=$(get_default_repo "$ARCH")

    local DEST=$1
    local REPO=$(get_core_repo_url "$REPO_URL" "$ARCH")
    local REPO_COMMUNITY=$(get_community_repo_url "$REPO_URL" "$ARCH")
    [[ -z "$DOWNLOAD_DIR" ]] && DOWNLOAD_DIR=$(mktemp -d)
    mkdir -p "$DOWNLOAD_DIR"
    [[ -z "$PRESERVE_DOWNLOAD_DIR" ]] && trap "rm -rf '$DOWNLOAD_DIR'" TERM EXIT
    debug "Destination directory: $DEST"
    debug "Core repository: $REPO"
    debug "Temporary directory: $DOWNLOAD_DIR"

    # Fetch packages, install system and do a minimal configuration
    mkdir -p "$DEST"
    local LIST_1=$(fetch_packages_list $REPO)
    local LIST_3=$(fetch_packages_list $REPO_COMMUNITY)
    install_pacman_packages "${CORE_PACKAGES[*]}" "$DEST" "$LIST_1" "$DOWNLOAD_DIR"
    install_pacman_packages_community "${COMMUNITY_PACKAGES[*]}" "$DEST" "$LIST_3" "$DOWNLOAD_DIR"
    configure_pacman "$DEST" "$ARCH"
    configure_minimal_system "$DEST"
    [[ -n "$USE_QEMU" ]] && configure_static_qemu "$ARCH" "$DEST"
    install_packages "$ARCH" "$DEST" "${CORE_PACKAGES[*]} ${EXTRA_PACKAGES[*]}"
    configure_keyring "$DEST"
    clean_chroot "$DEST" # clean
    [[ -z "$PRESERVE_DOWNLOAD_DIR" ]] && rm -rf "$DOWNLOAD_DIR"

    debug "Done!"
    debug
    debug "You may now chroot or arch-chroot from package arch-install-scripts:"
    debug "$ doas arch-chroot $DEST"
}

main "$@"