| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# |
| 4 |
# template script for generating PLD Linux container for LXC |
| 5 |
# |
| 6 |
|
| 7 |
# |
| 8 |
# lxc: Linux Container library |
| 9 |
|
| 10 |
# Authors: |
| 11 |
# Elan Ruusamäe <[email protected]> |
| 12 |
|
| 13 |
# This library is free software; you can redistribute it and/or |
| 14 |
# modify it under the terms of the GNU Lesser General Public |
| 15 |
# License as published by the Free Software Foundation; either |
| 16 |
# version 2.1 of the License, or (at your option) any later version. |
| 17 |
|
| 18 |
# This library is distributed in the hope that it will be useful, |
| 19 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 |
# Lesser General Public License for more details. |
| 22 |
|
| 23 |
# You should have received a copy of the GNU Lesser General Public |
| 24 |
# License along with this library; if not, write to the Free Software |
| 25 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 |
|
| 27 |
# Detect use under userns (unsupported) |
| 28 |
for arg in "$@"; do |
| 29 |
[ "$arg" = "--" ] && break |
| 30 |
if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then |
| 31 |
echo "This template can't be used for unprivileged containers." 1>&2 |
| 32 |
echo "You may want to try the \"download\" template instead." 1>&2 |
| 33 |
exit 1 |
| 34 |
fi |
| 35 |
done |
| 36 |
|
| 37 |
# Configuration |
| 38 |
arch=$(uname -m) |
| 39 |
cache_base=@LOCALSTATEDIR@/cache/lxc/pld/$arch |
| 40 |
default_path=@LXCPATH@ |
| 41 |
|
| 42 |
if [ -e /etc/os-release ]; then |
| 43 |
# This is a shell friendly configuration file. We can just source it. |
| 44 |
# What we're looking for in here is the ID, VERSION_ID and the CPE_NAME |
| 45 |
. /etc/os-release |
| 46 |
echo "Host CPE ID from /etc/os-release: ${CPE_NAME}" |
| 47 |
fi |
| 48 |
|
| 49 |
if [ "${CPE_NAME}" != "" -a "${ID}" = "pld" -a "${VERSION_ID}" != "" ]; then |
| 50 |
pld_host_ver=${VERSION_ID} |
| 51 |
is_pld=true |
| 52 |
elif [ -e /etc/pld-release ]; then |
| 53 |
# Only if all other methods fail, try to parse the pld-release file. |
| 54 |
pld_host_ver=$(sed -e '/PLD /!d' -e 's/^\([0-9.]*\)\sPLD.*/\1/' < /etc/pld-release) |
| 55 |
if [ "$pld_host_ver" != "" ]; then |
| 56 |
is_pld=true |
| 57 |
fi |
| 58 |
fi |
| 59 |
|
| 60 |
# Map a few architectures to their generic PLD Linux repository archs. |
| 61 |
case "$pld_host_ver:$arch" in |
| 62 |
3.0:i586) arch=i486 ;; |
| 63 |
esac |
| 64 |
|
| 65 |
configure_pld() |
| 66 |
{ |
| 67 |
|
| 68 |
# disable selinux |
| 69 |
mkdir -p $rootfs_path/selinux |
| 70 |
echo 0 > $rootfs_path/selinux/enforce |
| 71 |
|
| 72 |
# configure the network using the dhcp |
| 73 |
sed -i -e "s/^HOSTNAME=.*/HOSTNAME=${utsname}/" ${rootfs_path}/etc/sysconfig/network |
| 74 |
|
| 75 |
# set hostname on systemd |
| 76 |
if [ $release = "3.0" ]; then |
| 77 |
echo "${utsname}" > ${rootfs_path}/etc/hostname |
| 78 |
fi |
| 79 |
|
| 80 |
# set minimal hosts |
| 81 |
test -e $rootfs_path/etc/hosts || \ |
| 82 |
cat <<EOF > $rootfs_path/etc/hosts |
| 83 |
127.0.0.1 localhost.localdomain localhost $utsname |
| 84 |
::1 localhost6.localdomain6 localhost6 |
| 85 |
EOF |
| 86 |
|
| 87 |
dev_path="${rootfs_path}/dev" |
| 88 |
rm -rf $dev_path |
| 89 |
mkdir -p $dev_path |
| 90 |
mknod -m 666 ${dev_path}/null c 1 3 |
| 91 |
mknod -m 666 ${dev_path}/zero c 1 5 |
| 92 |
mknod -m 666 ${dev_path}/random c 1 8 |
| 93 |
mknod -m 666 ${dev_path}/urandom c 1 9 |
| 94 |
mkdir -m 755 ${dev_path}/pts |
| 95 |
mkdir -m 1777 ${dev_path}/shm |
| 96 |
mknod -m 666 ${dev_path}/tty c 5 0 |
| 97 |
mknod -m 666 ${dev_path}/tty0 c 4 0 |
| 98 |
mknod -m 666 ${dev_path}/tty1 c 4 1 |
| 99 |
mknod -m 666 ${dev_path}/tty2 c 4 2 |
| 100 |
mknod -m 666 ${dev_path}/tty3 c 4 3 |
| 101 |
mknod -m 666 ${dev_path}/tty4 c 4 4 |
| 102 |
mknod -m 600 ${dev_path}/console c 5 1 |
| 103 |
mknod -m 666 ${dev_path}/full c 1 7 |
| 104 |
mknod -m 600 ${dev_path}/initctl p |
| 105 |
mknod -m 666 ${dev_path}/ptmx c 5 2 |
| 106 |
|
| 107 |
if [ -n "${root_password}" ]; then |
| 108 |
echo "setting root passwd to $root_password" |
| 109 |
echo "root:$root_password" | chroot $rootfs_path chpasswd |
| 110 |
fi |
| 111 |
|
| 112 |
return 0 |
| 113 |
} |
| 114 |
|
| 115 |
configure_pld_init() |
| 116 |
{ |
| 117 |
# default powerfail action waits 2 minutes. for lxc we want it immediately |
| 118 |
sed -i -e '/^pf::powerfail:/ s,/sbin/shutdown.*,/sbin/halt,' ${rootfs_path}/etc/inittab |
| 119 |
} |
| 120 |
|
| 121 |
configure_pld_systemd() |
| 122 |
{ |
| 123 |
unlink ${rootfs_path}/etc/systemd/system/default.target |
| 124 |
chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/udev.service |
| 125 |
chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target |
| 126 |
|
| 127 |
# Actually, the After=dev-%i.device line does not appear in the |
| 128 |
# Fedora 17 or Fedora 18 systemd [email protected] file. It may be left |
| 129 |
# over from an earlier version and it's not doing any harm. We do need |
| 130 |
# to disable the "ConditionalPathExists=/dev/tty0" line or no gettys are |
| 131 |
# started on the ttys in the container. Lets do it in an override copy of |
| 132 |
# the service so it can still pass rpm verifies and not be automatically |
| 133 |
# updated by a new systemd version. -- mhw /\/\|=mhw=|\/\/ |
| 134 |
|
| 135 |
sed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \ |
| 136 |
-e 's/After=dev-%i.device/After=/' \ |
| 137 |
< ${rootfs_path}/lib/systemd/system/[email protected] \ |
| 138 |
> ${rootfs_path}/etc/systemd/system/[email protected] |
| 139 |
|
| 140 |
# Setup getty service on the 4 ttys we are going to allow in the |
| 141 |
# default config. Number should match lxc.tty.max |
| 142 |
for i in 1 2 3 4; do |
| 143 |
ln -sf ../[email protected] ${rootfs_path}/etc/systemd/system/getty.target.wants/getty@tty${i}.service |
| 144 |
done |
| 145 |
} |
| 146 |
|
| 147 |
download_pld() |
| 148 |
{ |
| 149 |
|
| 150 |
# check the mini pld was not already downloaded |
| 151 |
INSTALL_ROOT=$cache/partial |
| 152 |
mkdir -p $INSTALL_ROOT |
| 153 |
if [ $? -ne 0 ]; then |
| 154 |
echo "Failed to create '$INSTALL_ROOT' directory" |
| 155 |
return 1 |
| 156 |
fi |
| 157 |
|
| 158 |
# download a mini pld into a cache |
| 159 |
echo "Downloading PLD Linux minimal ..." |
| 160 |
POLDEK="poldek --root $INSTALL_ROOT --noask --nohold --noignore" |
| 161 |
PKG_LIST="basesystem filesystem pld-release rpm poldek vserver-packages rc-scripts pwdutils mingetty" |
| 162 |
|
| 163 |
mkdir -p $INSTALL_ROOT@LOCALSTATEDIR@/lib/rpm |
| 164 |
rpm --root $INSTALL_ROOT --initdb |
| 165 |
$POLDEK -u $PKG_LIST |
| 166 |
|
| 167 |
if [ $? -ne 0 ]; then |
| 168 |
echo "Failed to download the rootfs, aborting." |
| 169 |
return 1 |
| 170 |
fi |
| 171 |
|
| 172 |
mv "$INSTALL_ROOT" "$cache/rootfs" |
| 173 |
echo "Download complete." |
| 174 |
|
| 175 |
return 0 |
| 176 |
} |
| 177 |
|
| 178 |
copy_pld() |
| 179 |
{ |
| 180 |
|
| 181 |
# make a local copy of the minipld |
| 182 |
echo -n "Copying rootfs to $rootfs_path ..." |
| 183 |
cp -a $cache/rootfs/* $rootfs_path || return 1 |
| 184 |
return 0 |
| 185 |
} |
| 186 |
|
| 187 |
update_pld() |
| 188 |
{ |
| 189 |
POLDEK="poldek --root $cache/rootfs --noask" |
| 190 |
$POLDEK --upgrade-dist |
| 191 |
} |
| 192 |
|
| 193 |
install_pld() |
| 194 |
{ |
| 195 |
mkdir -p @LOCALSTATEDIR@/lock/subsys/ |
| 196 |
( |
| 197 |
flock -x 9 |
| 198 |
if [ $? -ne 0 ]; then |
| 199 |
echo "Cache repository is busy." |
| 200 |
return 1 |
| 201 |
fi |
| 202 |
|
| 203 |
echo "Checking cache download in $cache/rootfs ... " |
| 204 |
if [ ! -e "$cache/rootfs" ]; then |
| 205 |
download_pld |
| 206 |
if [ $? -ne 0 ]; then |
| 207 |
echo "Failed to download 'pld base'" |
| 208 |
return 1 |
| 209 |
fi |
| 210 |
else |
| 211 |
echo "Cache found. Updating..." |
| 212 |
update_pld |
| 213 |
if [ $? -ne 0 ]; then |
| 214 |
echo "Failed to update 'pld base', continuing with last known good cache" |
| 215 |
else |
| 216 |
echo "Update finished" |
| 217 |
fi |
| 218 |
fi |
| 219 |
|
| 220 |
echo "Copy $cache/rootfs to $rootfs_path ... " |
| 221 |
copy_pld |
| 222 |
if [ $? -ne 0 ]; then |
| 223 |
echo "Failed to copy rootfs" |
| 224 |
return 1 |
| 225 |
fi |
| 226 |
|
| 227 |
return 0 |
| 228 |
) 9>@LOCALSTATEDIR@/lock/subsys/lxc-pld |
| 229 |
|
| 230 |
return $? |
| 231 |
} |
| 232 |
|
| 233 |
copy_configuration() |
| 234 |
{ |
| 235 |
|
| 236 |
mkdir -p $config_path |
| 237 |
grep -q "^lxc.rootfs.path" $config_path/config 2>/dev/null || echo "lxc.rootfs.path = $rootfs_path" >> $config_path/config |
| 238 |
cat <<EOF >> $config_path/config |
| 239 |
# Most of below settings should be taken as defaults from |
| 240 |
# lxc.include = /usr/share/lxc/config/common.conf |
| 241 |
lxc.uts.name = $utsname |
| 242 |
lxc.tty.max = 4 |
| 243 |
lxc.pty.max = 1024 |
| 244 |
# Consider if below line is right for systemd container |
| 245 |
lxc.mount.fstab = $config_path/fstab |
| 246 |
lxc.cap.drop = sys_module mac_admin mac_override sys_time |
| 247 |
|
| 248 |
lxc.autodev = $auto_dev |
| 249 |
|
| 250 |
# When using LXC with apparmor, uncomment the next line to run unconfined: |
| 251 |
#lxc.apparmor.profile = unconfined |
| 252 |
|
| 253 |
## Devices |
| 254 |
# Allow all devices |
| •255 |
#lxc.cgroup2.devices.allow = a |
| 256 |
# Deny all devices |
| •257 |
lxc.cgroup2.devices.deny = a |
| 258 |
# Allow to mknod all devices (but not using them) |
| •259 |
lxc.cgroup2.devices.allow = c *:* m |
| 260 |
lxc.cgroup2.devices.allow = b *:* m |
| 261 |
|
| 262 |
# /dev/null and zero |
| •263 |
lxc.cgroup2.devices.allow = c 1:3 rwm |
| 264 |
lxc.cgroup2.devices.allow = c 1:5 rwm |
| 265 |
# consoles |
| •266 |
lxc.cgroup2.devices.allow = c 5:1 rwm |
| 267 |
lxc.cgroup2.devices.allow = c 5:0 rwm |
| 268 |
lxc.cgroup2.devices.allow = c 4:0 rwm |
| 269 |
lxc.cgroup2.devices.allow = c 4:1 rwm |
| 270 |
# /dev/{,u}random |
| •271 |
lxc.cgroup2.devices.allow = c 1:9 rwm |
| 272 |
lxc.cgroup2.devices.allow = c 1:8 rwm |
| 273 |
lxc.cgroup2.devices.allow = c 136:* rwm |
| 274 |
lxc.cgroup2.devices.allow = c 5:2 rwm |
| 275 |
# rtc |
| •276 |
lxc.cgroup2.devices.allow = c 254:0 rm |
| 277 |
EOF |
| 278 |
|
| 279 |
cat <<EOF > $config_path/fstab |
| 280 |
proc proc proc nodev,noexec,nosuid 0 0 |
| 281 |
sysfs sys sysfs defaults 0 0 |
| 282 |
EOF |
| 283 |
if [ $? -ne 0 ]; then |
| 284 |
echo "Failed to add configuration" |
| 285 |
return 1 |
| 286 |
fi |
| 287 |
|
| 288 |
return 0 |
| 289 |
} |
| 290 |
|
| 291 |
clean() |
| 292 |
{ |
| 293 |
|
| 294 |
if [ ! -e $cache ]; then |
| 295 |
exit 0 |
| 296 |
fi |
| 297 |
|
| 298 |
# lock, so we won't purge while someone is creating a repository |
| 299 |
( |
| 300 |
flock -x 9 |
| 301 |
if [ $? != 0 ]; then |
| 302 |
echo "Cache repository is busy." |
| 303 |
exit 1 |
| 304 |
fi |
| 305 |
|
| 306 |
echo -n "Purging the download cache for PLD Linux $release..." |
| 307 |
rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1 |
| 308 |
exit 0 |
| 309 |
) 9>@LOCALSTATEDIR@/lock/subsys/lxc-pld |
| 310 |
} |
| 311 |
|
| 312 |
usage() |
| 313 |
{ |
| 314 |
cat <<EOF |
| 315 |
usage: |
| 316 |
$1 -n|--name=<container_name> |
| 317 |
[-p|--path=<path>] [-c|--clean] [-R|--release=<PLD Release>] [--fqdn=<network name of container>] [-A|--arch=<arch of the container>] |
| 318 |
[-h|--help] |
| 319 |
Mandatory args: |
| 320 |
-n,--name container name, used to as an identifier for that container from now on |
| 321 |
Optional args: |
| •322 |
-p,--path path to the directory where the container's directory will be created, defaults to @LXCPATH@. |
| 323 |
The container config will go under @LXCPATH@ in that case |
| 324 |
--rootfs path for actual rootfs. |
| 325 |
-c,--clean clean the cache |
| 326 |
-R,--release PLD Linux release for the new container. if the host is PLD Linux, then it will default to the host's release. |
| 327 |
--fqdn fully qualified domain name (FQDN) for DNS and system naming |
| 328 |
-A,--arch NOT USED YET. Define what arch the container will be [i686,x86_64] |
| 329 |
-h,--help print this help |
| 330 |
EOF |
| 331 |
return 0 |
| 332 |
} |
| 333 |
|
| 334 |
options=$(getopt -o hp:n:cR: -l help,path:,rootfs:,name:,clean,release:,fqdn: -- "$@") |
| 335 |
if [ $? -ne 0 ]; then |
| 336 |
usage $(basename $0) |
| 337 |
exit 1 |
| 338 |
fi |
| 339 |
eval set -- "$options" |
| 340 |
|
| 341 |
while :; do |
| 342 |
case "$1" in |
| 343 |
-h|--help) usage $0 && exit 0;; |
| 344 |
-p|--path) path=$2; shift 2;; |
| 345 |
--rootfs) rootfs=$2; shift 2;; |
| 346 |
-n|--name) name=$2; shift 2;; |
| 347 |
-c|--clean) clean=$2; shift 2;; |
| 348 |
-R|--release) release=$2; shift 2;; |
| 349 |
--fqdn) utsname=$2; shift 2;; |
| 350 |
--) shift 1; break ;; |
| 351 |
*) break ;; |
| 352 |
esac |
| 353 |
done |
| 354 |
|
| 355 |
if [ ! -z "$clean" -a -z "$path" ]; then |
| 356 |
clean || exit 1 |
| 357 |
exit 0 |
| 358 |
fi |
| 359 |
|
| 360 |
if [ -z "${utsname}" ]; then |
| 361 |
utsname=${name} |
| 362 |
fi |
| 363 |
|
| 364 |
# This follows a standard "resolver" convention that an FQDN must have |
| 365 |
# at least two dots or it is considered a local relative host name. |
| 366 |
# If it doesn't, append the dns domain name of the host system. |
| 367 |
# |
| 368 |
# This changes one significant behavior when running |
| 369 |
# "lxc_create -n Container_Name" without using the |
| 370 |
# --fqdn option. |
| 371 |
# |
| 372 |
# Old behavior: |
| 373 |
# utsname and hostname = Container_Name |
| 374 |
# New behavior: |
| 375 |
# utsname and hostname = Container_Name.Domain_Name |
| 376 |
|
| 377 |
if [ $(expr "$utsname" : '.*\..*\.') = 0 ]; then |
| 378 |
if [ -n "$(dnsdomainname)" ]; then |
| 379 |
utsname=${utsname}.$(dnsdomainname) |
| 380 |
fi |
| 381 |
fi |
| 382 |
|
| 383 |
needed_pkgs="" |
| 384 |
type poldek >/dev/null 2>&1 |
| 385 |
if [ $? -ne 0 ]; then |
| 386 |
needed_pkgs="poldek $needed_pkgs" |
| 387 |
fi |
| 388 |
|
| 389 |
#type curl >/dev/null 2>&1 |
| 390 |
#if [ $? -ne 0 ]; then |
| 391 |
# needed_pkgs="curl $needed_pkgs" |
| 392 |
#fi |
| 393 |
|
| 394 |
if [ -n "$needed_pkgs" ]; then |
| 395 |
echo "Missing commands: $needed_pkgs" |
| 396 |
echo "Please install these using \"sudo poldek -u $needed_pkgs\"" |
| 397 |
exit 1 |
| 398 |
fi |
| 399 |
|
| 400 |
if [ -z "$path" ]; then |
| •401 |
path=$default_path |
| 402 |
fi |
| 403 |
|
| •404 |
path="${path}/${name}" |
| 405 |
|
| 406 |
if [ -z "$release" ]; then |
| 407 |
if [ "$is_pld" -a "$pld_host_ver" ]; then |
| 408 |
release=$pld_host_ver |
| 409 |
else |
| 410 |
echo "This is not a PLD Linux host and release missing, defaulting to 3.0. use -R|--release to specify release" |
| 411 |
release=3.0 |
| 412 |
fi |
| 413 |
fi |
| 414 |
|
| 415 |
# pld th have systemd. We need autodev enabled to keep systemd from causing problems. |
| 416 |
if [ $release = 3.0 ]; then |
| 417 |
auto_dev="0" |
| 418 |
else |
| 419 |
auto_dev="0" |
| 420 |
fi |
| 421 |
|
| 422 |
if [ "$(id -u)" != "0" ]; then |
| 423 |
echo "This script should be run as 'root'" |
| 424 |
exit 1 |
| 425 |
fi |
| 426 |
|
| 427 |
if [ -z "$rootfs" ]; then |
| 428 |
rootfs_path=$path/rootfs |
| 429 |
# check for 'lxc.rootfs.path' passed in through default config by lxc-create |
| 430 |
# TODO: should be lxc.rootfs.mount used instead? |
| 431 |
if grep -q '^lxc.rootfs.path' $path/config 2>/dev/null ; then |
| 432 |
rootfs_path=$(awk -F= '/^lxc.rootfs.path =/{ print $2 }' $path/config) |
| 433 |
fi |
| 434 |
else |
| 435 |
rootfs_path=$rootfs |
| 436 |
fi |
| 437 |
config_path=$default_path/$name |
| 438 |
cache=$cache_base/$release |
| 439 |
|
| 440 |
revert() |
| 441 |
{ |
| 442 |
echo "Interrupted, so cleaning up" |
| 443 |
lxc-destroy -n $name |
| 444 |
# maybe was interrupted before copy config |
| 445 |
rm -rf $path |
| 446 |
rm -rf $default_path/$name |
| 447 |
echo "exiting..." |
| 448 |
exit 1 |
| 449 |
} |
| 450 |
|
| 451 |
trap revert SIGHUP SIGINT SIGTERM |
| 452 |
|
| 453 |
copy_configuration |
| 454 |
if [ $? -ne 0 ]; then |
| 455 |
echo "Failed write configuration file" |
| 456 |
exit 1 |
| 457 |
fi |
| 458 |
|
| 459 |
install_pld |
| 460 |
if [ $? -ne 0 ]; then |
| 461 |
echo "Failed to install PLD Linux" |
| 462 |
exit 1 |
| 463 |
fi |
| 464 |
|
| 465 |
configure_pld |
| 466 |
if [ $? -ne 0 ]; then |
| 467 |
echo "Failed to configure PLD Linux for a container" |
| 468 |
exit 1 |
| 469 |
fi |
| 470 |
|
| 471 |
# If the systemd configuration directory exists - set it up for what we need. |
| 472 |
if [ -d ${rootfs_path}/etc/systemd/system ]; then |
| 473 |
configure_pld_systemd |
| 474 |
fi |
| 475 |
|
| 476 |
# This configuration (rc.sysinit) is not inconsistent with the systemd stuff |
| 477 |
# above and may actually coexist on some upgraded systems. Let's just make |
| 478 |
# sure that, if it exists, we update this file, even if it's not used... |
| 479 |
if [ -f ${rootfs_path}/etc/rc.sysinit ]; then |
| 480 |
configure_pld_init |
| 481 |
fi |
| 482 |
|
| 483 |
if [ ! -z $clean ]; then |
| 484 |
clean || exit 1 |
| 485 |
exit 0 |
| 486 |
fi |
| 487 |
echo "container rootfs and config created" |