Debian Codegraph @

share/build-scripts/debootstrap

raw symbols SCIP
1 #!/bin/sh
2
3 # Copyright (C) 2014-2026 Daniel Baumann <[email protected]>
4 #
5 # SPDX-License-Identifier: GPL-3.0+
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19
20 set -e
21
22 PROJECT="open-infrastructure"
23 SOFTWARE="compute-tools"
24 PROGRAM="container"
25
26 SCRIPT="$(basename ${0})"
27
28 HOOKS="/etc/${SOFTWARE}/hooks"
29 MACHINES="/var/lib/machines"
30
31 Parameters ()
32 {
33 GETOPT_LONGOPTIONS="bind:,bind-ro:,script:,name:,architecture:,distribution:,mirror:,password:"
34 GETOPT_OPTIONS="b:,s:,n:,a:,d:,m:,p:"
35
36 PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${SCRIPT} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
37
38 if [ "${?}" != "0" ]
39 then
40 echo "'${SCRIPT}': getopt exit" >&2
41 exit 1
42 fi
43
44 eval set -- "${PARAMETERS}"
45
46 while true
47 do
48 case "${1}" in
49 -b|--bind)
50 # ignore
51 shift 2
52 ;;
53
54 --bind-ro)
55 # ignore
56 shift 2
57 ;;
58
59 --cnt.auto)
60 # ignore
61 shift 2
62 ;;
63
64 --cnt.container-server)
65 # ignore
66 shift 2
67 ;;
68
69 -s|--script)
70 # ignore
71 shift 2
72 ;;
73
74 -n|--name)
75 NAME="${2}"
76 shift 2
77 ;;
78
79 -a|--architecture)
80 ARCHITECTURE="${2}"
81 shift 2
82 ;;
83
84 -d|--distribution)
85 DISTRIBUTION="${2}"
86 shift 2
87 ;;
88
89 -m|--mirror)
90 MIRROR="${2}"
91 shift 2
92 ;;
93
94 -p|--password)
95 PASSWORD="${2}"
96 shift 2
97 ;;
98
99 --)
100 shift 1
101 break
102 ;;
103
104 *)
105 echo "'${SCRIPT}': getopt error" >&2
106 exit 1
107 ;;
108 esac
109 done
110 }
111
112 Usage ()
113 {
114 echo "Usage: container build -n|--name NAME -s|--script ${SCRIPT} -- [-a|--architecture ARCHITECTURE] [-d|--distribution DISTRIBUTION] [-m|--mirror MIRROR] [-p|--password PASSWORD}" >&2
115 exit 1
116 }
117
118 Parameters "${@}"
119
120 if [ -z "${NAME}" ]
121 then
122 Usage
123 fi
124
125 if [ -e "${MACHINES}/${NAME}" ]
126 then
127 echo "'${NAME}': container already exists" >&2
128 exit 1
129 fi
130
131 case "${SCRIPT}" in
132 debootstrap)
133 BOOTSTRAP="/usr/sbin/debootstrap"
134 BOOTSTRAP_OPTIONS=""
135 ;;
136
137 mmdebstrap)
138 BOOTSTRAP="/usr/bin/mmdebstrap"
139 BOOTSTRAP_OPTIONS="--format=directory --mode=root --aptopt='APT::Sandbox::User \"root\"'"
140 ;;
141 esac
142
143 if [ ! -x "${BOOTSTRAP}" ]
144 then
145 echo "'${NAME}': ${BOOTSTRAP} - no such file." >&2
146 exit 1
147 fi
148
149 if [ "$(id -u)" -ne 0 ]
150 then
151 echo "'${NAME}': need root privileges" >&2
152 exit 1
153 fi
154
155 ARCHITECTURE="${ARCHITECTURE:-$(dpkg --print-architecture)}"
156 DISTRIBUTION="${DISTRIBUTION:-trixie}"
157 MIRROR="${MIRROR:-https://deb.debian.org/debian}"
158 PASSWORD="${PASSWORD:-$(dd if=/dev/urandom bs=12 count=1 2> /dev/null | base64)}"
159
160 INCLUDE="dbus"
161
162 case "${MIRROR}" in
163 https*)
164 INCLUDE="${INCLUDE},ca-certificates"
165 ;;
166 esac
167
168 # Pre hooks
169 for FILE in "${HOOKS}/pre-${SCRIPT}".* "${HOOKS}/${NAME}.pre-${SCRIPT}"
170 do
171 if [ -x "${FILE}" ]
172 then
173 "${FILE}"
174 fi
175 done
176
177 # Run
178 mkdir -p "${MACHINES}"
179 ${BOOTSTRAP} ${BOOTSTRAP_OPTIONS} --arch=${ARCHITECTURE} --include=${INCLUDE} ${DISTRIBUTION} ${MACHINES}/${NAME} ${MIRROR}
180
181 # Cleaning apt cache
182 chroot "${MACHINES}/${NAME}" apt clean
183
184 # Setting hostname
185 echo "${NAME}" > "${MACHINES}/${NAME}/etc/hostname"
186
187 # Setting root password
188 echo root:${PASSWORD} | chroot "${MACHINES}/${NAME}" chpasswd
189 echo "${NAME}: root password set to '${PASSWORD}'."
190
191 # Post hooks
192 for FILE in "${HOOKS}/post-${SCRIPT}".* "${HOOKS}/${NAME}.post-${SCRIPT}"
193 do
194 if [ -x "${FILE}" ]
195 then
196 "${FILE}"
197 fi
198 done