| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# |
| 4 |
# template script for generating Fedora container for LXC |
| 5 |
# |
| 6 |
|
| 7 |
# |
| 8 |
# lxc: linux Container library |
| 9 |
|
| 10 |
# Authors: |
| 11 |
# Daniel Lezcano <[email protected]> |
| 12 |
# Ramez Hanna <[email protected]> |
| 13 |
# Michael H. Warfield <[email protected]> |
| 14 |
# Reto Gantenbein <[email protected]> |
| 15 |
|
| 16 |
# This library is free software; you can redistribute it and/or |
| 17 |
# modify it under the terms of the GNU Lesser General Public |
| 18 |
# License as published by the Free Software Foundation; either |
| 19 |
# version 2.1 of the License, or (at your option) any later version. |
| 20 |
|
| 21 |
# This library is distributed in the hope that it will be useful, |
| 22 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 |
# Lesser General Public License for more details. |
| 25 |
|
| 26 |
# You should have received a copy of the GNU Lesser General Public |
| 27 |
# License along with this library; if not, write to the Free Software |
| 28 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 29 |
|
| 30 |
# configurations |
| 31 |
FEDORA_RELEASE_MIN=24 |
| 32 |
FEDORA_RELEASE_DEFAULT=${FEDORA_RELEASE_DEFAULT:-25} |
| 33 |
FEDORA_RSYNC_URL="${FEDORA_RSYNC_URL:-archives.fedoraproject.org::fedora-enchilada}" |
| 34 |
MIRRORLIST_URL=${MIRRORLIST_URL:-https://mirrors.fedoraproject.org/mirrorlist} |
| 35 |
|
| 36 |
local_state_dir="@LOCALSTATEDIR@" |
| 37 |
lxc_path="@LXCPATH@" |
| 38 |
lxc_template_config="@LXCTEMPLATECONFIG@" |
| 39 |
lxc_default_conf="@LXC_DEFAULT_CONFIG@" |
| 40 |
|
| 41 |
# allows the cache directory to be set by environment variable |
| 42 |
LXC_CACHE_PATH="${LXC_CACHE_PATH:-"${local_state_dir}/cache/lxc"}" |
| 43 |
|
| 44 |
# these are only going into comments in the resulting config... |
| 45 |
lxc_network_type=veth |
| 46 |
lxc_network_link=lxcbr0 |
| 47 |
|
| 48 |
# Some combinations of the tuning knobs below do not exactly make sense. |
| 49 |
# but that's ok. |
| 50 |
# |
| 51 |
# If the "root_password" is non-blank, use it, else set a default. |
| 52 |
# This can be passed to the script as an environment variable and is |
| 53 |
# set by a shell conditional assignment. Looks weird but it is what it is. |
| 54 |
# |
| 55 |
# If the root password contains a ding ($) then try to expand it. |
| 56 |
# That will pick up things like ${name} and ${RANDOM}. |
| 57 |
# If the root password contains more than 3 consecutive X's, pass it as |
| 58 |
# a template to mktemp and take the result. |
| 59 |
# |
| 60 |
# If root_display_password = yes, display the temporary root password at exit. |
| 61 |
# If root_store_password = yes, store it in the configuration directory |
| 62 |
# If root_prompt_password = yes, invoke "passwd" to force the user to change |
| 63 |
# the root password after the container is created. |
| 64 |
# If root_expire_password = yes, you will be prompted to change the root |
| 65 |
# password at the first login. |
| 66 |
# |
| 67 |
# These are conditional assignments... The can be overridden from the |
| 68 |
# preexisting environment variables... |
| 69 |
# |
| 70 |
# Make sure this is in single quotes to defer expansion to later! |
| 71 |
# :{root_password='Root-${name}-${RANDOM}'} |
| 72 |
: "${root_password='Root-${name}-XXXXXX'}" |
| 73 |
|
| 74 |
# Now, it doesn't make much sense to display, store, and force change |
| 75 |
# together. But, we gotta test, right??? |
| 76 |
: "${root_display_password='no'}" |
| 77 |
: "${root_store_password='yes'}" |
| 78 |
# Prompting for something interactive has potential for mayhem |
| 79 |
# with users running under the API... Don't default to "yes" |
| 80 |
: "${root_prompt_password='no'}" |
| 81 |
|
| 82 |
# Expire root password? Default to yes, but can be overridden from |
| 83 |
# the environment variable |
| 84 |
: "${root_expire_password='yes'}" |
| 85 |
|
| 86 |
# detect use under userns (unsupported) |
| 87 |
for arg in "$@"; do |
| 88 |
[ "${arg}" = "--" ] && break |
| 89 |
if [ "${arg}" = "--mapped-uid" ] || [ "${arg}" = "--mapped-gid" ] |
| 90 |
then |
| 91 |
echo "This template can't be used for unprivileged containers." 1>&2 |
| 92 |
echo "You may want to try the \"download\" template instead." 1>&2 |
| 93 |
exit 1 |
| 94 |
fi |
| 95 |
done |
| 96 |
|
| 97 |
# make sure the usual locations are in PATH |
| 98 |
export PATH=${PATH}:/usr/sbin:/usr/bin:/sbin:/bin |
| 99 |
|
| 100 |
# dnf package manager arguments |
| 101 |
dnf_args=( --assumeyes --best --allowerasing --disablerepo=* --enablerepo=fedora --enablerepo=updates ) |
| 102 |
|
| 103 |
# This function is going to setup a minimal host-arch native Fedora bootstrap |
| 104 |
# environment which will be used to create new containers on non-Fedora hosts. |
| 105 |
# |
| 106 |
bootstrap_fedora() |
| 107 |
{ |
| 108 |
local cache="${1}" |
| 109 |
|
| 110 |
local bootstrap="${cache}/bootstrap" |
| 111 |
if [ -d "${bootstrap}" ] |
| 112 |
then |
| 113 |
echo "Existing Fedora bootstrap environment found. Testing ..." |
| 114 |
if chroot_update_fedora "${bootstrap}" |
| 115 |
then |
| 116 |
CHROOT_DIR="${bootstrap}" |
| 117 |
CHROOT_CMD="chroot ${CHROOT_DIR} " |
| 118 |
|
| 119 |
echo "Bootstrap environment appears to be functional." |
| 120 |
return 0 |
| 121 |
else |
| 122 |
echo "Error: Bootstrap environment detected in ${bootstrap}" |
| 123 |
echo "but appears to be non-functional. Please remove and restart." |
| 124 |
return 1 |
| 125 |
fi |
| 126 |
fi |
| 127 |
|
| 128 |
echo "Setting up new Fedora ${FEDORA_RELEASE_DEFAULT} (${basearch}) bootstrap environment." |
| 129 |
|
| 130 |
[[ -d "${cache}" ]] || mkdir -p "${cache}" |
| 131 |
|
| 132 |
tmp_bootstrap_dir=$( mktemp -d --tmpdir="${cache}" bootstrap_XXXXXX ) |
| 133 |
pushd "${tmp_bootstrap_dir}" >/dev/null |
| 134 |
|
| 135 |
mkdir squashfs liveos bootstrap |
| 136 |
|
| 137 |
# download the LiveOS squashfs image |
| 138 |
if [ ! -f "${cache}/install.img" ] |
| 139 |
then |
| 140 |
local os_path="linux/releases/${FEDORA_RELEASE_DEFAULT}/Everything/${basearch}/os" |
| 141 |
local image_path="images/install.img" |
| 142 |
local ret=1 |
| 143 |
|
| 144 |
if [ -n "${rsync}" ] |
| 145 |
then |
| 146 |
echo "Syncing LiveOS squashfs image from ${FEDORA_RSYNC_URL} ... " |
| 147 |
rsync --archive --info=progress "${FEDORA_RSYNC_URL}/${os_path}/${image_path}" . |
| 148 |
ret=$? |
| 149 |
else |
| 150 |
if [ -z "${mirror}" ] |
| 151 |
then |
| 152 |
get_mirrors "${FEDORA_RELEASE_DEFAULT}" "${basearch}" || return $? |
| 153 |
else |
| 154 |
local mirror_url="${mirror}/${os_path}" |
| 155 |
fi |
| 156 |
for url in ${mirror:-${mirror_urls}} |
| 157 |
do |
| 158 |
echo "Downloading LiveOS squashfs image from ${url} ... " |
| 159 |
curl --silent --show-error --fail --remote-name "${url}/${image_path}" |
| 160 |
ret=$? |
| 161 |
if [ ${ret} -ne 0 ] |
| 162 |
then |
| 163 |
continue |
| 164 |
else |
| 165 |
break |
| 166 |
fi |
| 167 |
done |
| 168 |
fi |
| 169 |
|
| 170 |
if [ "${ret}" != 0 ] || [ ! -s install.img ] |
| 171 |
then |
| 172 |
echo "Error: Download of squashfs image failed." |
| 173 |
return 1 |
| 174 |
fi |
| 175 |
mv install.img "${cache}/" |
| 176 |
else |
| 177 |
echo "Using cached LiveOS squashfs image." |
| 178 |
fi |
| 179 |
|
| 180 |
echo "Mounting LiveOS squashfs file system." |
| 181 |
if ! mount -o loop -t squashfs "${cache}/install.img" squashfs/ |
| 182 |
then |
| 183 |
echo " |
| 184 |
Error: Mount of LiveOS squashfs image failed |
| 185 |
-------------------------------------------- |
| 186 |
You must have squashfs support available to mount image. LiveOS image is now |
| 187 |
cached. Process may be rerun without penalty of downloading LiveOS again. If |
| 188 |
LiveOS is corrupt, remove ${cache}/install.img |
| 189 |
" |
| 190 |
return 1 |
| 191 |
fi |
| 192 |
|
| 193 |
# mount contained LiveOS |
| 194 |
if ! mount -o loop squashfs/LiveOS/rootfs.img liveos |
| 195 |
then |
| 196 |
echo " |
| 197 |
Error: Mount of LiveOS stage0 rootfs image failed |
| 198 |
------------------------------------------------- |
| 199 |
LiveOS download may be corrupt. Remove ${cache}/LiveOS |
| 200 |
to force a new download. |
| 201 |
" |
| 202 |
return 1 |
| 203 |
fi |
| 204 |
|
| 205 |
echo "Copying LiveOS content to bootstrap environment ... " |
| 206 |
if ! rsync --archive --acls --hard-links --sparse liveos/. bootstrap/ |
| 207 |
then |
| 208 |
echo "Error: Build of bootstrap environment failed." |
| 209 |
echo "Keeping directory ${tmp_bootstrap_dir} for your investigation." |
| 210 |
exit 1 |
| 211 |
fi |
| 212 |
|
| 213 |
# unmount liveos mounts - we're done with liveos at this point |
| 214 |
umount liveos |
| 215 |
umount squashfs |
| 216 |
|
| 217 |
# customize bootstrap rootfs |
| 218 |
pushd bootstrap >/dev/null |
| 219 |
|
| 220 |
# setup repositories in bootstrap chroot |
| 221 |
CHROOT_DIR="$(pwd)" |
| 222 |
CHROOT_CMD="chroot ${CHROOT_DIR} " |
| 223 |
INSTALL_ROOT="/" |
| 224 |
if ! setup_repositories "${cache}" "${basearch}" "${FEDORA_RELEASE_DEFAULT}" "${mirror}" |
| 225 |
then |
| 226 |
echo "Error: Failed to configure repositories in ${CHROOT_DIR}${INSTALL_ROOT}" |
| 227 |
exit 1 |
| 228 |
fi |
| 229 |
if [ -n "${mirror}" ] |
| 230 |
then |
| 231 |
set_dnf_mirror_url ./etc/yum.repos.d/fedora*.repo |
| 232 |
fi |
| 233 |
|
| 234 |
# make sure /etc/resolv.conf is up to date in the chroot |
| 235 |
cp /etc/resolv.conf ./etc/ |
| 236 |
|
| 237 |
# verify bootstrap chroot by running a dnf update |
| 238 |
chroot_update_fedora "$(pwd)" |
| 239 |
ret=$? |
| 240 |
|
| 241 |
popd >/dev/null |
| 242 |
|
| 243 |
if [ "${ret}" != 0 ] |
| 244 |
then |
| 245 |
echo "Error: Build of bootstrap environment failed." |
| 246 |
echo "Keeping directory ${tmp_bootstrap_dir} for your investigation." |
| 247 |
return 1 |
| 248 |
fi |
| 249 |
|
| 250 |
mv bootstrap "${cache}" |
| 251 |
|
| 252 |
popd >/dev/null |
| 253 |
rm -rf "${tmp_bootstrap_dir}" |
| 254 |
|
| 255 |
CHROOT_DIR="${bootstrap}" |
| 256 |
CHROOT_CMD="chroot ${CHROOT_DIR} " |
| 257 |
|
| 258 |
echo "Fedora bootstrap environment successfully created." |
| 259 |
return 0 |
| 260 |
} |
| 261 |
|
| 262 |
chroot_mounts() |
| 263 |
{ |
| 264 |
test -n "${1}" && local chroot="${1}" || return 1 |
| 265 |
|
| 266 |
mount -t proc proc "${chroot}/proc" && |
| 267 |
mount -o bind /dev "${chroot}/dev" |
| 268 |
} |
| 269 |
|
| 270 |
chroot_umounts() |
| 271 |
{ |
| 272 |
test -n "${1}" && local chroot="${1}" || return 1 |
| 273 |
|
| 274 |
umount "${chroot}/proc" && |
| 275 |
umount "${chroot}/dev" |
| 276 |
} |
| 277 |
|
| 278 |
clean_cache() |
| 279 |
{ |
| 280 |
local cache="${1}" |
| 281 |
|
| 282 |
test ! -e "${cache}" && return 0 |
| 283 |
|
| 284 |
# lock, so we won't purge while someone is creating a repository |
| 285 |
( |
| 286 |
if ! flock -x 9 |
| 287 |
then |
| 288 |
echo "Error: Cache repository is busy." |
| 289 |
exit 1 |
| 290 |
fi |
| 291 |
|
| 292 |
echo -n "Purging the Fedora bootstrap and download cache ... " |
| 293 |
rm --preserve-root --one-file-system -rf "${cache}" && echo "Done." || exit 1 |
| 294 |
|
| 295 |
exit 0 |
| 296 |
|
| 297 |
) 9>"${local_state_dir}/lock/subsys/lxc-fedora" |
| 298 |
|
| 299 |
return $? |
| 300 |
} |
| 301 |
|
| 302 |
# Customize container rootfs |
| 303 |
# |
| 304 |
configure_fedora() |
| 305 |
{ |
| 306 |
local rootfs="${1}" |
| 307 |
local release="${2}" |
| 308 |
local utsname="${3}" |
| 309 |
|
| 310 |
# disable selinux |
| 311 |
mkdir -p "${rootfs}/selinux" |
| 312 |
echo 0 > "${rootfs}/selinux/enforce" |
| 313 |
|
| 314 |
# also kill it in the /etc/selinux/config file if it's there... |
| 315 |
if [ -f "${rootfs}/etc/selinux/config" ] |
| 316 |
then |
| 317 |
sed -i '/^SELINUX=/s/.*/SELINUX=disabled/' "${rootfs}/etc/selinux/config" |
| 318 |
fi |
| 319 |
|
| 320 |
# nice catch from Dwight Engen in the Oracle template. |
| 321 |
# wantonly plagerized here with much appreciation. |
| 322 |
if [ -f "${rootfs}/usr/sbin/selinuxenabled" ] |
| 323 |
then |
| 324 |
rm -f "${rootfs}/usr/sbin/selinuxenabled" |
| 325 |
ln -s /bin/false "${rootfs}/usr/sbin/selinuxenabled" |
| 326 |
fi |
| 327 |
|
| 328 |
# set hostname |
| 329 |
echo "${utsname}" > "${rootfs}/etc/hostname" |
| 330 |
|
| 331 |
# set default localtime to the host localtime if not set... |
| 332 |
if [ -e /etc/localtime ] && [ ! -e "${rootfs}/etc/localtime" ] |
| 333 |
then |
| 334 |
# if /etc/localtime is a symlink, this should preserve it. |
| 335 |
cp -a /etc/localtime "${rootfs}/etc/localtime" |
| 336 |
fi |
| 337 |
|
| 338 |
# set minimal hosts |
| 339 |
cat <<EOF > "${rootfs}/etc/hosts" |
| 340 |
127.0.0.1 localhost.localdomain localhost ${utsname} |
| 341 |
::1 localhost6.localdomain6 localhost6 |
| 342 |
EOF |
| 343 |
|
| 344 |
# setup console and tty[1-4] for login. note that /dev/console and |
| 345 |
# /dev/tty[1-4] will be symlinks to the ptys /dev/lxc/console and |
| 346 |
# /dev/lxc/tty[1-4] so that package updates can overwrite the symlinks. |
| 347 |
# lxc will maintain these links and bind mount ptys over /dev/lxc/* |
| 348 |
# since lxc.tty.dir is specified in the config. |
| 349 |
|
| 350 |
# allow root login on console, tty[1-4], and pts/0 for libvirt |
| 351 |
cat <<EOF >> "${rootfs}/etc/securetty" |
| 352 |
# LXC (Linux Containers) |
| 353 |
lxc/console |
| 354 |
lxc/tty1 |
| 355 |
lxc/tty2 |
| 356 |
lxc/tty3 |
| 357 |
lxc/tty4 |
| 358 |
# For libvirt/Virtual Machine Monitor |
| 359 |
pts/0 |
| 360 |
EOF |
| 361 |
|
| 362 |
if [ "${root_display_password}" = yes ] |
| 363 |
then |
| 364 |
echo "Setting root password to '$root_password'" |
| 365 |
fi |
| 366 |
if [ "${root_store_password}" = yes ] |
| 367 |
then |
| 368 |
touch "${path}/tmp_root_pass" |
| 369 |
chmod 600 "${path}/tmp_root_pass" |
| 370 |
echo "${root_password}" > "${path}/tmp_root_pass" |
| 371 |
echo "Storing root password in '${path}/tmp_root_pass'" |
| 372 |
fi |
| 373 |
|
| 374 |
echo "root:$root_password" | chroot "${rootfs}" chpasswd |
| 375 |
|
| 376 |
if [ "${root_expire_password}" = yes ] |
| 377 |
then |
| 378 |
# also set this password as expired to force the user to change it! |
| 379 |
chroot "${rootfs}" passwd -e root |
| 380 |
fi |
| 381 |
|
| 382 |
chroot_mounts "${rootfs}" |
| 383 |
|
| 384 |
# always make sure /etc/resolv.conf is up to date in the target! |
| 385 |
cp /etc/resolv.conf "${rootfs}/etc/" |
| 386 |
|
| 387 |
# rebuild the rpm database based on the target rpm version... |
| 388 |
rm -f "${rootfs}"/var/lib/rpm/__db* |
| 389 |
chroot "${rootfs}" rpm --rebuilddb |
| 390 |
|
| 391 |
chroot_umounts "${rootfs}" |
| 392 |
|
| 393 |
# default systemd target |
| 394 |
chroot "${rootfs}" ln -s /lib/systemd/system/multi-user.target \ |
| 395 |
/etc/systemd/system/default.target |
| 396 |
|
| 397 |
# enable networking via systemd-networkd |
| 398 |
test -d "${rootfs}/etc/systemd/network" || mkdir "${rootfs}/etc/systemd/network" |
| 399 |
cat <<EOF > "${rootfs}/etc/systemd/network/eth0.network" |
| 400 |
[Match] |
| 401 |
Name=eth0 |
| 402 |
|
| 403 |
[Network] |
| 404 |
DHCP=both |
| 405 |
EOF |
| 406 |
mkdir -p "${rootfs}/etc/systemd/system/socket.target.wants" |
| 407 |
chroot "${rootfs}" ln -s /usr/lib/systemd/system/systemd-networkd.socket \ |
| 408 |
/etc/systemd/system/socket.target.wants/systemd-networkd.socket |
| 409 |
chroot "${rootfs}" ln -s /usr/lib/systemd/system/systemd-networkd.service \ |
| 410 |
/etc/systemd/system/multi-user.target.wants/systemd-networkd.service |
| 411 |
mkdir -p "${rootfs}/etc/systemd/system/network-online.target.wants" |
| 412 |
chroot "${rootfs}" ln -s /usr/lib/systemd/system/systemd-networkd-wait-online.service \ |
| 413 |
/etc/systemd/system/network-online.target.wants/systemd-networkd-wait-online.service |
| 414 |
|
| 415 |
# disable traditional network init |
| 416 |
chroot "${rootfs}" chkconfig network off |
| 417 |
|
| 418 |
# enable systemd-resolved |
| 419 |
rm -f "${rootfs}/etc/resolv.conf" |
| 420 |
chroot "${rootfs}" ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf |
| 421 |
chroot "${rootfs}" ln -s /usr/lib/systemd/system/systemd-resolved.service \ |
| 422 |
/etc/systemd/system/multi-user.target.wants/systemd-resolved.service |
| 423 |
|
| 424 |
# if desired, prevent systemd from over-mounting /tmp with tmpfs |
| 425 |
if [ "${masktmp}" -eq 1 ] |
| 426 |
then |
| 427 |
chroot "${rootfs}" ln -s /dev/null /etc/systemd/system/tmp.mount |
| 428 |
fi |
| 429 |
|
| 430 |
return 0 |
| 431 |
} |
| 432 |
|
| 433 |
copy_configuration() |
| 434 |
{ |
| 435 |
local rootfs="${1}" |
| 436 |
local config="${2}" |
| 437 |
local utsname="${3}" |
| 438 |
|
| 439 |
# include configuration from default.conf if available |
| 440 |
grep -q "^lxc." "${lxc_default_conf}" > "${config}" 2>/dev/null |
| 441 |
|
| 442 |
grep -q "^lxc.rootfs.path" "${config}" 2>/dev/null || echo " |
| 443 |
lxc.rootfs.path = ${rootfs} |
| 444 |
" >> "${config}" |
| 445 |
|
| 446 |
# The following code is to create static MAC addresses for each |
| 447 |
# interface in the container. This code will work for multiple |
| 448 |
# interfaces in the default config. It will also strip any |
| 449 |
# hwaddr stanzas out of the default config since we can not share |
| 450 |
# MAC addresses between containers. |
| 451 |
mv "${config}" "${config}.orig" |
| 452 |
|
| 453 |
local line key |
| 454 |
while read -r line |
| 455 |
do |
| 456 |
# This should catch variable expansions from the default config... |
| 457 |
if expr "${line}" : '.*\$' > /dev/null 2>&1 |
| 458 |
then |
| 459 |
line=$(eval "echo \"${line}\"") |
| 460 |
fi |
| 461 |
|
| 462 |
# There is a tab and a space in the regex bracket below! |
| 463 |
# Seems that \s doesn't work in brackets. |
| 464 |
key=$(expr "${line}" : '\s*\([^ ]*\)\s*=') |
| 465 |
|
| 466 |
if [ "${key}" != "lxc.net.0.hwaddr" ] |
| 467 |
then |
| 468 |
echo "${line}" >> "${config}" |
| 469 |
|
| 470 |
if [ "${key}" == "lxc.net.0.link" ] |
| 471 |
then |
| 472 |
echo "lxc.net.0.hwaddr = $(create_hwaddr)" >> "${config}" |
| 473 |
fi |
| 474 |
fi |
| 475 |
done < "${config}.orig" |
| 476 |
rm -f "${config}.orig" |
| 477 |
|
| 478 |
if [ -e "${lxc_template_config}/fedora.common.conf" ] |
| 479 |
then |
| 480 |
echo " |
| 481 |
# Include common configuration |
| 482 |
lxc.include = ${lxc_template_config}/fedora.common.conf |
| 483 |
" >> "${config}" |
| 484 |
fi |
| 485 |
|
| 486 |
cat <<EOF >> "${path}/config" |
| 487 |
# Container specific configuration |
| 488 |
lxc.arch = ${basearch} |
| 489 |
lxc.uts.name = ${utsname} |
| 490 |
|
| 491 |
# When using LXC with apparmor, uncomment the next line to run unconfined: |
| 492 |
#lxc.apparmor.profile = unconfined |
| 493 |
|
| 494 |
# example simple networking setup, uncomment to enable |
| 495 |
#lxc.net.0.type = ${lxc_network_type} |
| 496 |
#lxc.net.0.flags = up |
| 497 |
#lxc.net.0.link = ${lxc_network_link} |
| 498 |
#lxc.net.0.name = eth0 |
| 499 |
# Additional example for veth network type |
| 500 |
# static MAC address, |
| 501 |
#lxc.net.0.hwaddr = $(create_hwaddr) |
| 502 |
# persistent veth device name on host side |
| 503 |
# Note: This may potentially collide with other containers of same name! |
| 504 |
#lxc.net.0.veth.pair = v-${name}-e0 |
| 505 |
EOF |
| 506 |
|
| 507 |
if [ $? -ne 0 ] |
| 508 |
then |
| 509 |
echo "Failed to add configuration" |
| 510 |
return 1 |
| 511 |
fi |
| 512 |
|
| 513 |
return 0 |
| 514 |
} |
| 515 |
|
| 516 |
copy_fedora() |
| 517 |
{ |
| 518 |
local cache="${1}" |
| 519 |
local rootfs="${2}" |
| 520 |
echo -n "Copying ${cache} to ${rootfs} ... " |
| 521 |
|
| 522 |
mkdir -p "${rootfs}" && |
| 523 |
rsync --archive --hard-links --sparse --acls --xattrs "${cache}/" "${rootfs}/" && |
| 524 |
echo || return 1 |
| 525 |
|
| 526 |
return 0 |
| 527 |
} |
| 528 |
|
| 529 |
# Generate a random hardware (MAC) address composed of FE followed by |
| 530 |
# 5 random bytes... |
| 531 |
# |
| 532 |
create_hwaddr() |
| 533 |
{ |
| 534 |
openssl rand -hex 5 | sed -e 's/\(..\)/:\1/g; s/^/fe/' |
| 535 |
} |
| 536 |
|
| 537 |
# Make sure a fully functional rootfs of the requested release and architecture |
| 538 |
# will be setup in the given cache directory. If this is a Fedora host the |
| 539 |
# commands will run natively otherwise in a minimal Fedora bootstrap chroot. |
| 540 |
# |
| 541 |
download_fedora() |
| 542 |
{ |
| 543 |
local cache_rootfs="${1}" |
| 544 |
local setup_rootfs="${cache_rootfs%%/rootfs}/partial" |
| 545 |
|
| 546 |
# suppress errors due to unknown locales |
| 547 |
LC_ALL=C |
| 548 |
LANG=en_US |
| 549 |
|
| 550 |
echo "Downloading ${basearch} rootfs for Fedora ${release} ..." |
| 551 |
|
| 552 |
# The following variables are going to be overwritten if the rootfs setup |
| 553 |
# is run in a separate boostrap environment (can not build natively). |
| 554 |
# These are the defaults for the non-boostrap (native) mode. |
| 555 |
CHROOT_DIR= |
| 556 |
CHROOT_CMD= |
| 557 |
INSTALL_ROOT=${setup_rootfs} |
| 558 |
|
| 559 |
if [ ! "${is_fedora}" ] || [ "${fedora_host_ver}" -lt "${FEDORA_VERSION_MINIMAL}" ] |
| 560 |
then |
| 561 |
# if this is not a supported Fedora host, use minimal bootstrap chroot |
| 562 |
echo "Non-Fedora host detected. Checking for bootstrap environment ... " |
| 563 |
if ! bootstrap_fedora "${cache}" |
| 564 |
then |
| 565 |
echo "Error: Fedora Bootstrap setup failed" |
| 566 |
return 1 |
| 567 |
fi |
| 568 |
echo "Using bootstrap environment at ${CHROOT_DIR}" |
| 569 |
echo "Ensure bootstrap repository is fully setup for Fedora ${release}" |
| 570 |
# check if the gpg-keys of the demanded release are installed in bootstrap |
| 571 |
# this makes sure, that the bootstrap repo was already prepared for $release |
| 572 |
# if we don't do this check, "setup_repositories" could try to install older |
| 573 |
# versions of the release-rpms which would fail as rpm would fail |
| 574 |
if [ -f ${CHROOT_DIR}/etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-${release}-primary ] |
| 575 |
then |
| 576 |
echo "The bootstrap repository seems to be prepared" |
| 577 |
else |
| 578 |
INSTALL_ROOT="/" |
| 579 |
if ! setup_repositories "${cache}" "${basearch}" "${release}" "${mirror}" |
| 580 |
then |
| 581 |
echo "Error: Failed to configure repositories in ${CHROOT_DIR}${INSTALL_ROOT}" |
| 582 |
return 1 |
| 583 |
fi |
| 584 |
INSTALL_ROOT=${setup_rootfs} |
| 585 |
fi |
| 586 |
fi |
| 587 |
|
| 588 |
if ! mkdir -p "${setup_rootfs}" |
| 589 |
then |
| 590 |
echo "Error: Failed to create '${setup_rootfs}' directory." |
| 591 |
return 1 |
| 592 |
fi |
| 593 |
|
| 594 |
trap revert_rootfs SIGHUP SIGINT SIGTERM |
| 595 |
|
| 596 |
mkdir -p "${setup_rootfs}/var/lib/rpm" |
| 597 |
|
| 598 |
# if the setup is going to be run in a chroot, mount the related file systems |
| 599 |
if [ -n "${CHROOT_DIR}" ] |
| 600 |
then |
| 601 |
chroot_mounts "${CHROOT_DIR}" |
| 602 |
|
| 603 |
# make sure rootfs is available in bootstrap chroot |
| 604 |
INSTALL_ROOT="/run/install" |
| 605 |
test -d "${CHROOT_DIR}${INSTALL_ROOT}" || mkdir -p "${CHROOT_DIR}${INSTALL_ROOT}" |
| 606 |
mount -o bind "${setup_rootfs}" "${CHROOT_DIR}${INSTALL_ROOT}" |
| 607 |
fi |
| 608 |
|
| 609 |
if ! setup_repositories "${cache}" "${basearch}" "${release}" "${mirror}" |
| 610 |
then |
| 611 |
echo "Error: Failed to configure repositories in ${CHROOT_DIR}${INSTALL_ROOT}" |
| 612 |
revert_rootfs >/dev/null |
| 613 |
return 1 |
| 614 |
fi |
| 615 |
|
| 616 |
# Unforunately <dnf-2.0 doesn't respect the repository configuration of the |
| 617 |
# installroot but use the one from the host. This obviously doesn't work |
| 618 |
# with a custom mirror or target architecture. Therefore a temporary dnf.conf |
| 619 |
# is created and passed to the chroot command. |
| 620 |
cat "${CHROOT_DIR}${INSTALL_ROOT}"/etc/yum.repos.d/{fedora,fedora-updates}.repo > "${CHROOT_DIR}${INSTALL_ROOT}/dnf.conf" |
| 621 |
if [ -n "${mirror}" ] |
| 622 |
then |
| 623 |
set_dnf_mirror_url "${CHROOT_DIR}${INSTALL_ROOT}/dnf.conf" |
| 624 |
fi |
| 625 |
|
| 626 |
# install minimal container file system |
| 627 |
local pkg_list="dnf initscripts passwd vim-minimal openssh-server openssh-clients dhclient rootfiles policycoreutils fedora-release fedora-repos" |
| 628 |
if ! ${CHROOT_CMD}dnf --installroot "${INSTALL_ROOT}" \ |
| 629 |
--config="${INSTALL_ROOT}/dnf.conf" \ |
| 630 |
--releasever "${release}" \ |
| 631 |
${dnf_args[@]} \ |
| 632 |
install ${pkg_list} |
| 633 |
then |
| 634 |
echo "Error: Failed to setup the rootfs in ${CHROOT_DIR}${INSTALL_ROOT}." |
| 635 |
revert_rootfs >/dev/null |
| 636 |
return 1 |
| 637 |
fi |
| 638 |
|
| 639 |
unmount_installroot |
| 640 |
|
| 641 |
# from now on we'll work in the new rootfs |
| 642 |
chroot_mounts "${setup_rootfs}" |
| 643 |
|
| 644 |
# It might happen, that the dnf used above will write an incompatible |
| 645 |
# RPM database for the version running in the rootfs. Rebuild it. |
| 646 |
echo "Fixing up RPM databases" |
| 647 |
rm -f "${setup_rootfs}"/var/lib/rpm/__db* |
| 648 |
chroot "${setup_rootfs}" rpm --rebuilddb |
| 649 |
|
| 650 |
# Restrict locale for installed packages to en_US to shrink image size |
| 651 |
# following: https://pagure.io/fedora-kickstarts/blob/master/f/fedora-cloud-base.ks |
| 652 |
echo "Cleanup locales and language files ..." |
| 653 |
find "${setup_rootfs}/usr/share/locale" -mindepth 1 -maxdepth 1 -type d \ |
| 654 |
-not -name "${LANG}" -exec rm -rf {} + |
| 655 |
|
| 656 |
chroot "${setup_rootfs}" localedef --list-archive | grep -v ^"${LANG}" | xargs \ |
| 657 |
chroot "${setup_rootfs}" localedef --delete-from-archive |
| 658 |
|
| 659 |
mv -f "${setup_rootfs}/usr/lib/locale/locale-archive" \ |
| 660 |
"${setup_rootfs}/usr/lib/locale/locale-archive.tmpl" |
| 661 |
chroot "${setup_rootfs}" build-locale-archive |
| 662 |
|
| 663 |
echo "%_install_langs C:en:${LANG}:${LANG}.UTF-8" > "${setup_rootfs}/etc/rpm/macros.image-language-conf" |
| 664 |
|
| 665 |
chroot_umounts "${setup_rootfs}" |
| 666 |
|
| 667 |
# reset traps |
| 668 |
trap SIGHUP |
| 669 |
trap SIGINT |
| 670 |
trap SIGTERM |
| 671 |
|
| 672 |
# use generated rootfs as future cache |
| 673 |
mv "${setup_rootfs}" "${cache_rootfs}" |
| 674 |
|
| 675 |
echo "Download of Fedora rootfs complete." |
| 676 |
return 0 |
| 677 |
} |
| 678 |
|
| 679 |
# Query the Fedora mirrorlist for several HTTPS mirrors |
| 680 |
# |
| 681 |
get_mirrors() |
| 682 |
{ |
| 683 |
local release="${1}" |
| 684 |
local mirror_arch="${2}" |
| 685 |
|
| 686 |
for trynumber in 1 2 3 4 |
| 687 |
do |
| 688 |
[ "${trynumber}" != 1 ] && echo -n "Trying again ... " |
| 689 |
|
| 690 |
# choose some mirrors by parsing directory index |
| 691 |
mirror_urls=$(curl --silent --show-error --fail "${MIRRORLIST_URL}?repo=fedora-${release}&arch=${mirror_arch}" | sed '/^https:/!d' | sed '2,6!d') |
| 692 |
|
| 693 |
# shellcheck disable=SC2181 |
| 694 |
if [ $? -eq 0 ] && [ -n "${mirror_urls}" ] |
| 695 |
then |
| 696 |
break |
| 697 |
fi |
| 698 |
|
| 699 |
echo "Warning: Failed to get a mirror on try ${trynumber}." |
| 700 |
sleep 3 |
| 701 |
done |
| 702 |
|
| 703 |
if [ -z "${mirror_urls}" ] |
| 704 |
then |
| 705 |
echo "Error: Failed to retrieve Fedora mirror URL. Please use '-m MIRROR' option." |
| 706 |
return 1 |
| 707 |
fi |
| 708 |
|
| 709 |
return 0 |
| 710 |
} |
| 711 |
|
| 712 |
# Install a functional Fedora rootfs into the container root |
| 713 |
# |
| 714 |
install_fedora() |
| 715 |
{ |
| 716 |
local rootfs="${1}" |
| 717 |
local cache="${2}" |
| 718 |
local cache_rootfs="${cache}/${release}-${basearch}/rootfs" |
| 719 |
|
| 720 |
mkdir -p "${local_state_dir}/lock/subsys/" |
| 721 |
( |
| 722 |
if ! flock -x 9 |
| 723 |
then |
| 724 |
echo "Error: Cache repository is busy." |
| 725 |
return 1 |
| 726 |
fi |
| 727 |
|
| 728 |
echo "Checking cache download in ${cache_rootfs} ... " |
| 729 |
if [ ! -e "${cache_rootfs}" ] |
| 730 |
then |
| 731 |
if ! download_fedora "${cache_rootfs}" |
| 732 |
then |
| 733 |
echo "Error: Failed to download Fedora ${release} (${basearch})" |
| 734 |
return 1 |
| 735 |
fi |
| 736 |
else |
| 737 |
echo "Cache found at ${cache_rootfs}. Updating ..." |
| 738 |
if ! chroot_update_fedora "${cache_rootfs}" |
| 739 |
then |
| 740 |
echo "Failed to update cached rootfs, continuing with previously cached version." |
| 741 |
else |
| 742 |
echo "Fedora update finished." |
| 743 |
fi |
| 744 |
fi |
| 745 |
|
| 746 |
trap revert_container SIGHUP SIGINT SIGTERM |
| 747 |
|
| 748 |
if ! copy_fedora "${cache_rootfs}" "${rootfs}" |
| 749 |
then |
| 750 |
echo "Error: Failed to copy rootfs" |
| 751 |
return 1 |
| 752 |
fi |
| 753 |
|
| 754 |
chroot_mounts "${rootfs}" |
| 755 |
|
| 756 |
# install additional user provided packages |
| 757 |
if [ -n "${packages}" ] |
| 758 |
then |
| 759 |
# always make sure /etc/resolv.conf is up to date in the target! |
| 760 |
cp /etc/resolv.conf "${rootfs}/etc/" |
| 761 |
|
| 762 |
echo "Installing user requested RPMs: ${packages}" |
| 763 |
if ! chroot "${rootfs}" dnf install ${dnf_args[@]} $(tr ',' ' ' <<< "${packages}") |
| 764 |
then |
| 765 |
echo "Error: Installation of user provided packages failed." |
| 766 |
echo "Cleaning up ... " |
| 767 |
sleep 3 # wait for all file handles to properly close |
| 768 |
chroot_umounts "${setup_rootfs}" |
| 769 |
return 1 |
| 770 |
fi |
| 771 |
fi |
| 772 |
|
| 773 |
# cleanup dnf cache in new container |
| 774 |
chroot "${rootfs}" dnf clean all |
| 775 |
|
| 776 |
sleep 3 # wait for all file handles to properly close |
| 777 |
chroot_umounts "${rootfs}" |
| 778 |
|
| 779 |
return 0 |
| 780 |
) 9>"${local_state_dir}/lock/subsys/lxc-fedora" |
| 781 |
|
| 782 |
return $? |
| 783 |
} |
| 784 |
|
| 785 |
# Cleanup partial container |
| 786 |
# |
| 787 |
revert_container() |
| 788 |
{ |
| 789 |
echo "Interrupted, so cleaning up ..." |
| 790 |
lxc-destroy -n "${name}" 2>/dev/null |
| 791 |
# maybe was interrupted before copy config, try to prevent some mistakes |
| 792 |
if [ -d "${path}" ] && |
| 793 |
[ "${path}" != "/" ] && [ "${path}" != "/tmp" ] && [ "${path}" != "/bin" ] |
| 794 |
then |
| 795 |
rm -rf "${path}" |
| 796 |
fi |
| 797 |
echo "Exiting ..." |
| 798 |
exit 1 |
| 799 |
} |
| 800 |
|
| 801 |
# Cleanup partial rootfs cache |
| 802 |
# |
| 803 |
revert_rootfs() |
| 804 |
{ |
| 805 |
echo "Interrupted, so cleaning up ..." |
| 806 |
unmount_installroot |
| 807 |
rm -rf "${setup_rootfs}" |
| 808 |
echo "Exiting ..." |
| 809 |
exit 1 |
| 810 |
} |
| 811 |
|
| 812 |
# Set dnf repository mirror in given repo files |
| 813 |
# |
| 814 |
set_dnf_mirror_url() |
| 815 |
{ |
| 816 |
sed -i -e 's/^\(metalink=.*\)$/#\1/g' "${@}" |
| 817 |
sed -i -e '/baseurl/ s|^#||g' "${@}" |
| 818 |
sed -i -e "/baseurl/ s|http://download.fedoraproject.org/pub/fedora|${mirror}|g" "${@}" |
| 819 |
} |
| 820 |
|
| 821 |
# Setup dnf repository configuration. It can be run in a chroot by specifying |
| 822 |
# $CHROOT_DIR (chroot directory) and $CHROOT_CMD (chroot command) and/or |
| 823 |
# with an alternative RPM install root defined in $INSTALL_ROOT. |
| 824 |
# |
| 825 |
setup_repositories() |
| 826 |
{ |
| 827 |
local cache="${1}" |
| 828 |
local target_arch="${2}" |
| 829 |
local release="${3}" |
| 830 |
local mirror="${4}" |
| 831 |
|
| 832 |
# select rpm packages to install depending on the version to install |
| 833 |
rpmpkgs="fedora-release-${release} fedora-repos-${release}" |
| 834 |
if [ "${release}" -gt 26 ] |
| 835 |
then |
| 836 |
rpmpkgs="${rpmpkgs} fedora-gpg-keys-${release}" |
| 837 |
fi |
| 838 |
|
| 839 |
# download repository packages if not found in cache |
| 840 |
pushd "${cache}" >/dev/null |
| 841 |
if [ -z "$(ls -1 ./fedora-release-${release}*.noarch.rpm 2>/dev/null)" ] || |
| 842 |
[ -z "$(ls -1 ./fedora-repos-${release}*.noarch.rpm 2>/dev/null)" ] || |
| 843 |
( [ "${release}" -gt 26 ] && [ -z "$(ls -1 ./fedora-gpg-keys-${release}*.noarch.rpm 2>/dev/null)" ] ) |
| 844 |
then |
| 845 |
# if no mirror given, get an appropriate mirror from the mirror list |
| 846 |
if [ -z "${mirror}" ] |
| 847 |
then |
| 848 |
get_mirrors "${release}" "${target_arch}" || return $? |
| 849 |
else |
| 850 |
# construct release-specific mirror url |
| 851 |
mirror="${mirror}/linux/releases/${release}/Everything/${target_arch}/os" |
| 852 |
fi |
| 853 |
|
| 854 |
for mirror_url in ${mirror:-${mirror_urls}} |
| 855 |
do |
| 856 |
local release_url="${mirror_url}/Packages/f" |
| 857 |
|
| 858 |
for pkg in ${rpmpkgs} |
| 859 |
do |
| 860 |
test -n "$(ls -1 ./${pkg}*.noarch.rpm 2>/dev/null)" && continue |
| 861 |
|
| 862 |
# query package name by parsing directory index |
| 863 |
echo "Requesting '${pkg}' package version from ${release_url}." |
| 864 |
pkg_name=$(curl --silent --show-error --fail "${release_url}/" | sed -n -e "/${pkg}/ s/.*href=\"\(${pkg}-.*\.noarch\.rpm\)\">.*/\1/p" | tail -1) |
| 865 |
|
| 866 |
# shellcheck disable=SC2181 |
| 867 |
if [ $? -ne 0 ] || [ -z "${pkg_name}" ] |
| 868 |
then |
| 869 |
echo "Error: Failed to get '${pkg}' version from ${release_url}/." |
| 870 |
continue |
| 871 |
fi |
| 872 |
|
| 873 |
echo "Downloading '${release_url}/${pkg_name} ... " |
| 874 |
if ! curl --silent --show-error --fail --remote-name "${release_url}/${pkg_name}" |
| 875 |
then |
| 876 |
echo "Error: Package download failed." |
| 877 |
continue |
| 878 |
fi |
| 879 |
done |
| 880 |
|
| 881 |
# if we have all packages continue |
| 882 |
if [ -z "$(ls -1 ./fedora-release-${release}*.noarch.rpm 2>/dev/null)" ] || |
| 883 |
[ -z "$(ls -1 ./fedora-repos-${release}*.noarch.rpm 2>/dev/null)" ] |
| 884 |
( [ "${release}" -gt 26 ] && [ -z "$(ls -1 ./fedora-gpg-keys-${release}*.noarch.rpm 2>/dev/null)" ] ) |
| 885 |
then |
| 886 |
break |
| 887 |
fi |
| 888 |
done |
| 889 |
fi |
| 890 |
|
| 891 |
# copy packages to chroot file system |
| 892 |
if [ -n "${CHROOT_DIR}" ] |
| 893 |
then |
| 894 |
cp ./fedora-release-${release}*.noarch.rpm "${CHROOT_DIR}" && |
| 895 |
cp ./fedora-repos-${release}*.noarch.rpm "${CHROOT_DIR}" |
| 896 |
if [ "${release}" -gt 26 ] |
| 897 |
then |
| 898 |
cp ./fedora-gpg-keys-${release}*.noarch.rpm "${CHROOT_DIR}" |
| 899 |
fi |
| 900 |
else |
| 901 |
local pkgdir="${cache}" |
| 902 |
fi |
| 903 |
|
| 904 |
# use '--nodeps' to work around 'fedora-release-24-*' bash dependency |
| 905 |
${CHROOT_CMD}rpm --root "${INSTALL_ROOT}" -ivh --nodeps "${pkgdir}"/{fedora-release-${release}*.noarch.rpm,fedora-repos-${release}*.noarch.rpm} |
| 906 |
if [ "${release}" -gt 26 ]; |
| 907 |
then |
| 908 |
${CHROOT_CMD}rpm --root "${INSTALL_ROOT}" -ivh --nodeps "${pkgdir}"/fedora-gpg-keys-${release}*.noarch.rpm |
| 909 |
fi |
| 910 |
local ret=$? |
| 911 |
|
| 912 |
# dnf will take $basearch from host, so force the arch we want |
| 913 |
sed -i "s|\$basearch|${target_arch}|" ${CHROOT_DIR}${INSTALL_ROOT}/etc/yum.repos.d/* |
| 914 |
|
| 915 |
popd >/dev/null |
| 916 |
|
| 917 |
if [ "${ret}" -ne 0 ] |
| 918 |
then |
| 919 |
echo "Failed to setup repositories in ${CHROOT_DIR}${INSTALL_ROOT}" |
| 920 |
exit 1 |
| 921 |
fi |
| 922 |
|
| 923 |
# cleanup installed packages |
| 924 |
if [ -n "${CHROOT_DIR}" ] |
| 925 |
then |
| 926 |
# shellcheck disable=SC2086 |
| 927 |
rm -f "${CHROOT_DIR}"/{fedora-release-${release}*.noarch.rpm,fedora-repos-${release}*.noarch.rpm} |
| 928 |
if [ "${release}" -gt 26 ] |
| 929 |
then |
| 930 |
rm -f "${CHROOT_DIR}"/fedora-gpg-keys-${release}*.noarch.rpm |
| 931 |
fi |
| 932 |
fi |
| 933 |
|
| 934 |
return 0 |
| 935 |
} |
| 936 |
|
| 937 |
# Run dnf update in the given chroot directory. |
| 938 |
# |
| 939 |
chroot_update_fedora() |
| 940 |
{ |
| 941 |
local chroot="${1}" |
| 942 |
chroot_mounts "${chroot}" |
| 943 |
|
| 944 |
# always make sure /etc/resolv.conf is up to date in the target! |
| 945 |
cp /etc/resolv.conf "${chroot}/etc/" |
| 946 |
|
| 947 |
chroot "${chroot}" dnf -y update |
| 948 |
local ret=$? |
| 949 |
|
| 950 |
sleep 3 # wait for all file handles to properly close |
| 951 |
|
| 952 |
chroot_umounts "${chroot}" |
| 953 |
|
| 954 |
return ${ret} |
| 955 |
} |
| 956 |
|
| 957 |
# Unmount installroot after bootstrapping or on error. |
| 958 |
# |
| 959 |
unmount_installroot() { |
| 960 |
if [ -n "${CHROOT_DIR}" ] |
| 961 |
then |
| 962 |
sleep 3 # wait for all file handles to properly close |
| 963 |
chroot_umounts "${CHROOT_DIR}" 2>/dev/null |
| 964 |
umount "${CHROOT_DIR}${INSTALL_ROOT}" 2>/dev/null |
| 965 |
fi |
| 966 |
} |
| 967 |
|
| 968 |
usage() |
| 969 |
{ |
| 970 |
cat <<EOF |
| 971 |
LXC Container configuration for Fedora images. |
| 972 |
|
| 973 |
Template specific options can be passed to lxc-create after a '--' like this: |
| 974 |
|
| 975 |
lxc-create --name=NAME -t fedora [OPTION..] -- [TEMPLATE_OPTION..] |
| 976 |
|
| 977 |
Template options: |
| 978 |
|
| 979 |
-a, --arch Define what arch the container will be [i386,x86_64] |
| 980 |
-c, --clean Clean bootstrap and download cache |
| 981 |
-d, --debug Run with 'set -x' to debug errors |
| 982 |
--fqdn Fully qualified domain name (FQDN) |
| 983 |
-h, --help Print this help text |
| 984 |
--mask-tmp Prevent systemd from over-mounting /tmp with tmpfs. |
| 985 |
-M, --mirror=MIRROR Fedora mirror to use during installation. |
| •986 |
-p, --path=PATH Path to the directory where the container's directory |
| 987 |
will be created, defaults to ${lxc_path}. |
| 988 |
-P, --packages=PKGS Comma-separated list of additional RPM packages to |
| 989 |
install into the container. |
| 990 |
-R, --release=RELEASE Fedora release number of the container, defaults |
| 991 |
to host's release if the host is Fedora. |
| 992 |
--rootfs=ROOTFS Path for the actual container root file system |
| 993 |
--rsync Use rsync instead of HTTPS to download bootstrap |
| 994 |
image (insecure). |
| 995 |
|
| 996 |
Environment variables: |
| 997 |
|
| 998 |
LXC_CACHE_PATH Cache directory for image bootstrap. Defaults to |
| 999 |
'${LXC_CACHE_PATH}' |
| 1000 |
|
| 1001 |
MIRRORLIST_URL List of Fedora mirrors queried if no custom mirror is |
| 1002 |
given. Defaults to '${MIRRORLIST_URL}' |
| 1003 |
|
| 1004 |
FEDORA_RSYNC_URL Fedora rsync URL to use for bootstrap with '--rsync'. |
| 1005 |
Defaults to '${FEDORA_RSYNC_URL}' |
| 1006 |
|
| 1007 |
FEDORA_RELEASE_DEFAULT Set default Fedora release if not detected from the |
| 1008 |
host. Is set to '${FEDORA_RELEASE_DEFAULT}' |
| 1009 |
|
| 1010 |
EOF |
| 1011 |
return 0 |
| 1012 |
} |
| 1013 |
|
| 1014 |
options=$(getopt -o a:hp:n:cR:dP:M: -l help,path:,rootfs:,name:,clean,release:,arch:,debug,fqdn:,mask-tmp,mirror:,packages:,rsync -- "$@") |
| 1015 |
# shellcheck disable=SC2181 |
| 1016 |
if [ $? -ne 0 ]; then |
| 1017 |
usage |
| 1018 |
exit 1 |
| 1019 |
fi |
| 1020 |
|
| 1021 |
arch=$(uname -m) |
| 1022 |
debug=0 |
| 1023 |
masktmp=0 |
| 1024 |
|
| 1025 |
eval set -- "$options" |
| 1026 |
while true |
| 1027 |
do |
| 1028 |
case "${1}" in |
| 1029 |
-h|--help) usage; exit 0 ;; |
| 1030 |
-n|--name) name="${2}"; shift 2 ;; |
| 1031 |
-p|--path) path="${2}"; shift 2 ;; |
| 1032 |
--rootfs) rootfs="${2}"; shift 2 ;; |
| 1033 |
-a|--arch) newarch="${2}"; shift 2 ;; |
| 1034 |
-c|--clean) clean=1; shift 1 ;; |
| 1035 |
-d|--debug) debug=1; shift 1 ;; |
| 1036 |
--fqdn) utsname="${2}"; shift 2 ;; |
| 1037 |
--mask-tmp) masktmp=1; shift 1 ;; |
| 1038 |
-M|--mirror) mirror="${2}"; shift 2 ;; |
| 1039 |
-P|--packages) packages="${2}"; shift 2 ;; |
| 1040 |
-R|--release) release="${2}"; shift 2 ;; |
| 1041 |
--rsync) rsync=1; shift 1 ;; |
| 1042 |
--) shift 1; break ;; |
| 1043 |
*) break ;; |
| 1044 |
esac |
| 1045 |
done |
| 1046 |
|
| 1047 |
if [ "${debug}" -eq 1 ] |
| 1048 |
then |
| 1049 |
set -x |
| 1050 |
fi |
| 1051 |
|
| 1052 |
# change to a safe directory |
| 1053 |
cd || exit $? |
| 1054 |
|
| 1055 |
# Is this Fedora? |
| 1056 |
# Allow for weird remixes like the Raspberry Pi |
| 1057 |
# |
| 1058 |
# Use the Mitre standard CPE identifier for the release ID if possible... |
| 1059 |
# This may be in /etc/os-release or /etc/system-release-cpe. We |
| 1060 |
# should be able to use EITHER. Give preference to /etc/os-release for now. |
| 1061 |
|
| 1062 |
if [ -e /etc/os-release ] |
| 1063 |
then |
| 1064 |
# This is a shell friendly configuration file. We can just source it. |
| 1065 |
# What we're looking for in here is the ID, VERSION_ID and the CPE_NAME |
| 1066 |
. /etc/os-release |
| 1067 |
fi |
| 1068 |
|
| 1069 |
if [ "${CPE_NAME}" = "" ] && [ -e /etc/system-release-cpe ] |
| 1070 |
then |
| 1071 |
CPE_NAME=$(head -n1 /etc/system-release-cpe) |
| 1072 |
CPE_URI=$(expr "${CPE_NAME}" : '\([^:]*:[^:]*\)') |
| 1073 |
if [ "${CPE_URI}" != "cpe:/o" ] |
| 1074 |
then |
| 1075 |
CPE_NAME= |
| 1076 |
else |
| 1077 |
echo "Host CPE ID from /etc/system-release-cpe: ${CPE_NAME}" |
| 1078 |
# Probably a better way to do this but sill remain posix |
| 1079 |
# compatible but this works, shrug... |
| 1080 |
# Must be nice and not introduce convenient bashisms here. |
| 1081 |
ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:[^:]*:\([^:]*\)') |
| 1082 |
VERSION_ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\)') |
| 1083 |
fi |
| 1084 |
fi |
| 1085 |
|
| 1086 |
if [ "${ID}" = "fedora" ] && [ -n "${CPE_NAME}" ] && [ -n "${VERSION_ID}" ] |
| 1087 |
then |
| 1088 |
fedora_host_ver=${VERSION_ID} |
| 1089 |
is_fedora=true |
| 1090 |
fi |
| 1091 |
|
| 1092 |
# Map a few architectures to their generic Fedora repository archs. |
| 1093 |
# The two ARM archs are a bit of a guesstimate for the v5 and v6 |
| 1094 |
# archs. V6 should have hardware floating point (Rasberry Pi). |
| 1095 |
# The "arm" arch is safer (no hardware floating point). So |
| 1096 |
# there may be cases where we "get it wrong" for some v6 other |
| 1097 |
# than RPi. |
| 1098 |
basearch=${arch} |
| 1099 |
case "${arch}" in |
| 1100 |
i686) basearch=i386 ;; |
| 1101 |
armv3l|armv4l|armv5l) basearch=arm ;; |
| 1102 |
armv6l|armv7l|armv8l) basearch=armhfp ;; |
| 1103 |
*) ;; |
| 1104 |
esac |
| 1105 |
|
| 1106 |
case "${basearch}" in |
| 1107 |
ppc64|s390x) FEDORA_RSYNC_URL="archives.fedoraproject.org::fedora-secondary" ;; |
| 1108 |
*) ;; |
| 1109 |
esac |
| 1110 |
|
| 1111 |
# Somebody wants to specify an arch. This is very limited case. |
| 1112 |
# i386/i586/i686 on i386/x86_64 |
| 1113 |
# - or - |
| 1114 |
# x86_64 on x86_64 |
| 1115 |
if [ "${newarch}" != "" ] && [ "${newarch}" != "${arch}" ] |
| 1116 |
then |
| 1117 |
case "${newarch}" in |
| 1118 |
i386|i586|i686) |
| 1119 |
if [ "${basearch}" = "i386" ] || [ "${basearch}" = "x86_64" ] |
| 1120 |
then |
| 1121 |
# Make the arch a generic x86 32 bit... |
| 1122 |
basearch=i386 |
| 1123 |
else |
| 1124 |
basearch=bad |
| 1125 |
fi |
| 1126 |
;; |
| 1127 |
*) |
| 1128 |
basearch=bad |
| 1129 |
;; |
| 1130 |
esac |
| 1131 |
|
| 1132 |
if [ "${basearch}" = "bad" ] |
| 1133 |
then |
| 1134 |
echo "Error: You cannot build a ${newarch} Fedora container on a ${arch} host. Sorry!" |
| 1135 |
exit 1 |
| 1136 |
fi |
| 1137 |
fi |
| 1138 |
|
| 1139 |
# Let's do something better for the initial root password. |
| 1140 |
# It's not perfect but it will defeat common scanning brute force |
| 1141 |
# attacks in the case where ssh is exposed. It will also be set to |
| 1142 |
# expired, forcing the user to change it at first login. |
| 1143 |
if [ "${root_password}" = "" ] |
| 1144 |
then |
| 1145 |
root_password=Root-${name}-${RANDOM} |
| 1146 |
else |
| 1147 |
# If it's got a ding in it, try and expand it! |
| 1148 |
if [ "$(expr "${root_password}" : '.*$.')" != 0 ] |
| 1149 |
then |
| 1150 |
root_password=$(eval echo "${root_password}") |
| 1151 |
fi |
| 1152 |
|
| 1153 |
# If it has more than 3 consecutive X's in it, feed it |
| 1154 |
# through mktemp as a template. |
| 1155 |
if [ "$(expr "${root_password}" : '.*XXXX')" != 0 ] |
| 1156 |
then |
| 1157 |
root_password=$(mktemp -u "${root_password}") |
| 1158 |
fi |
| 1159 |
fi |
| 1160 |
|
| 1161 |
if [ -z "${utsname}" ]; then |
| 1162 |
utsname=${name} |
| 1163 |
fi |
| 1164 |
|
| 1165 |
# This follows a standard "resolver" convention that an FQDN must have |
| 1166 |
# at least two dots or it is considered a local relative host name. |
| 1167 |
# If it doesn't, append the dns domain name of the host system. |
| 1168 |
# |
| 1169 |
# This changes one significant behavior when running |
| 1170 |
# "lxc_create -n Container_Name" without using the |
| 1171 |
# --fqdn option. |
| 1172 |
# |
| 1173 |
# Old behavior: |
| 1174 |
# utsname and hostname = Container_Name |
| 1175 |
# New behavior: |
| 1176 |
# utsname and hostname = Container_Name.Domain_Name |
| 1177 |
|
| 1178 |
if [ "$(expr "${utsname}" : '.*\..*\.')" = 0 ] |
| 1179 |
then |
| 1180 |
if [ -n "$(dnsdomainname)" ] && [ "$(dnsdomainname)" != "localdomain" ] |
| 1181 |
then |
| 1182 |
utsname="${utsname}.$(dnsdomainname)" |
| 1183 |
fi |
| 1184 |
fi |
| 1185 |
|
| 1186 |
# check if the pre-requisite binaries are available |
| 1187 |
prerequisite_pkgs=( curl openssl rsync ) |
| 1188 |
needed_pkgs="" |
| 1189 |
for pkg in "${prerequisite_pkgs[@]}" |
| 1190 |
do |
| 1191 |
if ! type "${pkg}" >/dev/null 2>&1 |
| 1192 |
then |
| 1193 |
needed_pkgs="${pkg} ${needed_pkgs}" |
| 1194 |
fi |
| 1195 |
done |
| 1196 |
if [ -n "${needed_pkgs}" ] |
| 1197 |
then |
| 1198 |
echo "Error: Missing command(s): ${needed_pkgs}" |
| 1199 |
exit 1 |
| 1200 |
fi |
| 1201 |
|
| 1202 |
if [ "$(id -u)" != "0" ] |
| 1203 |
then |
| 1204 |
echo "This script should be run as 'root'" |
| 1205 |
exit 1 |
| 1206 |
fi |
| 1207 |
|
| 1208 |
# cleanup cache if requested |
| 1209 |
cache="${LXC_CACHE_PATH}/fedora" |
| 1210 |
if [ -n "${clean}" ] |
| 1211 |
then |
| 1212 |
clean_cache "${cache}" || exit 1 |
| 1213 |
exit 0 |
| 1214 |
fi |
| 1215 |
|
| 1216 |
# set container directory |
| 1217 |
if [ -z "${path}" ] |
| 1218 |
then |
| •1219 |
path="${lxc_path}" |
| 1220 |
fi |
| 1221 |
|
| •1222 |
path="${path}/${name}" |
| 1223 |
|
| 1224 |
# set container rootfs and configuration path |
| 1225 |
config="${path}/config" |
| 1226 |
if [ -z "${rootfs}" ] |
| 1227 |
then |
| 1228 |
# check for 'lxc.rootfs.path' passed in through default config by lxc-create |
| 1229 |
if grep -q '^lxc.rootfs.path' "${config}" 2>/dev/null |
| 1230 |
then |
| 1231 |
rootfs=$(awk -F= '/^lxc.rootfs.path =/{ print $2 }' "${config}") |
| 1232 |
else |
| 1233 |
rootfs="${path}/rootfs" |
| 1234 |
fi |
| 1235 |
fi |
| 1236 |
|
| 1237 |
# set release if not given |
| 1238 |
if [ -z "${release}" ] |
| 1239 |
then |
| 1240 |
if [ "${is_fedora}" ] && [ -n "${fedora_host_ver}" ] |
| 1241 |
then |
| 1242 |
echo "Detected Fedora ${fedora_host_ver} host. Set release to ${fedora_host_ver}." |
| 1243 |
release="${fedora_host_ver}" |
| 1244 |
else |
| 1245 |
echo "This is not a Fedora host or release is missing, defaulting release to ${FEDORA_RELEASE_DEFAULT}." |
| 1246 |
release="${FEDORA_RELEASE_DEFAULT}" |
| 1247 |
fi |
| 1248 |
fi |
| 1249 |
if [ "${release}" -lt "${FEDORA_RELEASE_MIN}" ] |
| 1250 |
then |
| 1251 |
echo "Error: Fedora release ${release} is not supported. Set -R at least to ${FEDORA_RELEASE_MIN}." |
| 1252 |
exit 1 |
| 1253 |
fi |
| 1254 |
|
| 1255 |
# bootstrap rootfs and copy to container file system |
| 1256 |
if ! install_fedora "${rootfs}" "${cache}" |
| 1257 |
then |
| 1258 |
echo "Error: Failed to create Fedora container" |
| 1259 |
exit 1 |
| 1260 |
fi |
| 1261 |
|
| 1262 |
# customize container file system |
| 1263 |
if ! configure_fedora "${rootfs}" "${release}" "${utsname}" |
| 1264 |
then |
| 1265 |
echo "Error: Failed to configure Fedora container" |
| 1266 |
exit 1 |
| 1267 |
fi |
| 1268 |
|
| 1269 |
# create container configuration (will be overwritten by newer lxc-create) |
| 1270 |
if ! copy_configuration "${rootfs}" "${config}" "${utsname}" |
| 1271 |
then |
| 1272 |
echo "Error: Failed write container configuration file" |
| 1273 |
exit 1 |
| 1274 |
fi |
| 1275 |
|
| 1276 |
if [ -n "${clean}" ]; then |
| 1277 |
clean || exit 1 |
| 1278 |
exit 0 |
| 1279 |
fi |
| 1280 |
|
| 1281 |
echo "Successfully created container '${name}'" |
| 1282 |
|
| 1283 |
if [ "${root_display_password}" = "yes" ] |
| 1284 |
then |
| 1285 |
echo "The temporary password for root is: '$root_password' |
| 1286 |
|
| 1287 |
You may want to note that password down before starting the container. |
| 1288 |
" |
| 1289 |
fi |
| 1290 |
|
| 1291 |
if [ "${root_store_password}" = "yes" ] |
| 1292 |
then |
| 1293 |
echo "The temporary root password is stored in: |
| 1294 |
|
| 1295 |
'${config}/tmp_root_pass' |
| 1296 |
" |
| 1297 |
fi |
| 1298 |
|
| 1299 |
if [ "${root_prompt_password}" = "yes" ] |
| 1300 |
then |
| 1301 |
echo "Invoking the passwd command in the container to set the root password. |
| 1302 |
|
| 1303 |
chroot ${rootfs} passwd |
| 1304 |
" |
| 1305 |
chroot "${rootfs}" passwd |
| 1306 |
else |
| 1307 |
if [ "${root_expire_password}" = "yes" ] |
| 1308 |
then |
| 1309 |
if ( mountpoint -q -- "${rootfs}" ) |
| 1310 |
then |
| 1311 |
echo "To reset the root password, you can do: |
| 1312 |
|
| 1313 |
lxc-start -n ${name} |
| 1314 |
lxc-attach -n ${name} -- passwd |
| 1315 |
lxc-stop -n ${name} |
| 1316 |
" |
| 1317 |
else |
| 1318 |
echo " |
| 1319 |
The root password is set up as expired and will require it to be changed |
| 1320 |
at first login, which you should do as soon as possible. If you lose the |
| 1321 |
root password or wish to change it without starting the container, you |
| 1322 |
can change it from the host by running the following command (which will |
| 1323 |
also reset the expired flag): |
| 1324 |
|
| 1325 |
chroot ${rootfs} passwd |
| 1326 |
" |
| 1327 |
fi |
| 1328 |
fi |
| 1329 |
fi |
| 1330 |
|
| 1331 |
# vim: set ts=4 sw=4 expandtab: |