This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.sh
executable file
·181 lines (155 loc) · 4.33 KB
/
build.sh
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
#!/usr/bin/env sh
# MIT License
#
# Copyright (c) 2022-2023 Carlo Corradini
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Current directory
# shellcheck disable=SC1007
DIRNAME=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
# Load commons
# shellcheck source=../../scripts/__commons.sh
. "$DIRNAME/../../scripts/__commons.sh"
# ================
# CONFIGURATION
# ================
# Docker image
DOCKER_IMAGE="recluster-arch:latest"
# Dockerfile
DOCKERFILE="$DIRNAME/Dockerfile"
# ISO directory
ISO_DIR=$(readlink -f "$DIRNAME/iso")
# Architectures
ARCHS='["x86_64"]'
# Arch profile file
ARCH_PROFILE_FILE=$(readlink -f "$DIRNAME/profiledef.sh")
# Arch packages file
ARCH_PACKAGES_FILE=$(readlink -f "$DIRNAME/packages.x86_64")
# ================
# GLOBALS
# ================
# Container identifier
CONTAINER_ID=
# ================
# CLEANUP
# ================
cleanup() {
# Exit code
_exit_code=$?
[ $_exit_code = 0 ] || WARN "Cleanup exit code $_exit_code"
# Cleanup iso directory
[ $_exit_code = 0 ] || cleanup_dir "$ISO_DIR"
# Cleanup Docker container
cleanup_docker_container "$CONTAINER_ID"
exit "$_exit_code"
}
# Trap
trap cleanup INT QUIT TERM EXIT
# ================
# FUNCTIONS
# ================
# Show help message
show_help() {
cat << EOF
Usage: $(basename "$0") [--help]
$HELP_COMMONS_USAGE
reCluster Arch Linux distribution script.
Options:
--help Show this help message and exit
$HELP_COMMONS_OPTIONS
EOF
}
################################################################################################################################
# Parse command line arguments
# @param $@ Arguments
parse_args() {
while [ $# -gt 0 ]; do
# Number of shift
_shifts=1
case $1 in
--help)
# Display help message and exit
show_help
exit 0
;;
*)
# Commons
parse_args_commons "$@"
_shifts=$RETVAL
;;
esac
# Shift arguments
while [ "$_shifts" -gt 0 ]; do
shift
_shifts=$((_shifts = _shifts - 1))
done
done
}
# Verify system
verify_system() {
assert_cmd docker
assert_cmd jq
assert_docker_image "$DOCKER_IMAGE" "$DOCKERFILE"
[ -f "$ARCH_PROFILE_FILE" ] || FATAL "Arch profile file '$ARCH_PROFILE_FILE' does not exists"
[ -f "$ARCH_PACKAGES_FILE" ] || FATAL "Arch packages file '$ARCH_PACKAGES_FILE' does not exists"
}
# Prepare container
prepare_container() {
# Start container
CONTAINER_ID=$(
docker run \
--volume "$ISO_DIR:/tmp/recluster/out" \
--volume "$ARCH_PROFILE_FILE:/tmp/recluster/profile/profiledef.sh" \
--volume "$ARCH_PACKAGES_FILE:/tmp/recluster/profile/packages.x86_64" \
--rm \
--detach \
--interactive \
--privileged \
"$DOCKER_IMAGE"
)
# File permission
docker exec "$CONTAINER_ID" chmod u+x \
/tmp/recluster/profile/profiledef.sh
}
# Build ISO image
# @param $1 Architecture
builder() {
_arch=$1
INFO "Building Arch Linux architecture '$_arch'"
docker exec --tty "$CONTAINER_ID" \
mkarchiso \
-v \
-w "/tmp/recluster/work" \
-o "/tmp/recluster/out" \
"/tmp/recluster/profile"
}
# ================
# MAIN
# ================
{
parse_args "$@"
verify_system
recreate_dir "$ISO_DIR"
prepare_container
while read -r _arch; do
builder "$_arch"
done << EOF
$(printf '%s\n' "$ARCHS" | jq --compact-output --raw-output '.[]')
EOF
}