| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# |
| 4 |
# lxc: linux Container library |
| 5 |
|
| 6 |
# Authors: |
| 7 |
# Daniel Lezcano <[email protected]> |
| 8 |
|
| 9 |
# Template for slackware by Matteo Bernardini <[email protected]> |
| 10 |
# some parts are taken from the debian one (used as model) |
| 11 |
|
| 12 |
# This library is free software; you can redistribute it and/or |
| 13 |
# modify it under the terms of the GNU Lesser General Public |
| 14 |
# License as published by the Free Software Foundation; either |
| 15 |
# version 2.1 of the License, or (at your option) any later version. |
| 16 |
|
| 17 |
# This library is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
# Lesser General Public License for more details. |
| 21 |
|
| 22 |
# You should have received a copy of the GNU Lesser General Public |
| 23 |
# License along with this library; if not, write to the Free Software |
| 24 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
|
| 26 |
# Detect use under userns (unsupported) |
| 27 |
for arg in "$@"; do |
| 28 |
[ "$arg" = "--" ] && break |
| 29 |
if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then |
| 30 |
echo "This template can't be used for unprivileged containers." 1>&2 |
| 31 |
echo "You may want to try the \"download\" template instead." 1>&2 |
| 32 |
exit 1 |
| 33 |
fi |
| 34 |
done |
| 35 |
|
| 36 |
# Add some directories to PATH in case we create containers with sudo |
| 37 |
export PATH=/sbin:/usr/sbin:$PATH |
| 38 |
|
| 39 |
cache=${cache:-/var/cache/lxc/slackware} |
| 40 |
|
| 41 |
# Use the primary Slackware site by default, but please consider changing |
| 42 |
# this to a closer mirror site. |
| 43 |
MIRROR=${MIRROR:-http://ftp.slackware.com/pub/slackware} |
| 44 |
|
| 45 |
if [ -z "$arch" ]; then |
| 46 |
case "$( uname -m )" in |
| 47 |
i?86) arch=i486 ;; |
| 48 |
arm*) arch=arm ;; |
| 49 |
*) arch=$( uname -m ) ;; |
| 50 |
esac |
| 51 |
fi |
| 52 |
|
| 53 |
LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@" |
| 54 |
|
| 55 |
configure_slackware() |
| 56 |
{ |
| 57 |
rootfs=$1 |
| 58 |
hostname=$2 |
| 59 |
|
| 60 |
echo "Configuring..." ; echo |
| 61 |
|
| 62 |
# The next part contains excerpts taken from SeTconfig (written by |
| 63 |
# Patrick Volkerding) from the slackware setup disk. |
| 64 |
# But before pasting them just set a variable to use them as they are |
| 65 |
T_PX=$rootfs |
| 66 |
|
| 67 |
( cd $T_PX ; chmod 755 ./ ) |
| 68 |
( cd $T_PX ; chmod 755 ./var ) |
| 69 |
if [ -d $T_PX/usr/src/linux ]; then |
| 70 |
chmod 755 $T_PX/usr/src/linux |
| 71 |
fi |
| 72 |
if [ ! -d $T_PX/proc ]; then |
| 73 |
mkdir $T_PX/proc |
| 74 |
chown root.root $T_PX/proc |
| 75 |
fi |
| 76 |
if [ ! -d $T_PX/sys ]; then |
| 77 |
mkdir $T_PX/sys |
| 78 |
chown root.root $T_PX/sys |
| 79 |
fi |
| 80 |
chmod 1777 $T_PX/tmp |
| 81 |
if [ ! -d $T_PX/var/spool/mail ]; then |
| 82 |
mkdir -p $T_PX/var/spool/mail |
| 83 |
chmod 755 $T_PX/var/spool |
| 84 |
chown root.mail $T_PX/var/spool/mail |
| 85 |
chmod 1777 $T_PX/var/spool/mail |
| 86 |
fi |
| 87 |
|
| 88 |
echo "#!/bin/sh" > $T_PX/etc/rc.d/rc.keymap |
| 89 |
echo "# Load the keyboard map. More maps are in /usr/share/kbd/keymaps." \ |
| 90 |
>> $T_PX/etc/rc.d/rc.keymap |
| 91 |
echo "if [ -x /usr/bin/loadkeys ]; then" >> $T_PX/etc/rc.d/rc.keymap |
| 92 |
echo " /usr/bin/loadkeys us" >> $T_PX/etc/rc.d/rc.keymap |
| 93 |
echo "fi" >> $T_PX/etc/rc.d/rc.keymap |
| 94 |
chmod 755 $T_PX/etc/rc.d/rc.keymap |
| 95 |
|
| 96 |
# Network configuration is left to the user, that have to edit |
| 97 |
# /etc/rc.d/rc.inet1.conf and /etc/resolv.conf of the container |
| 98 |
# just set the hostname |
| 99 |
cat <<EOF > $rootfs/etc/HOSTNAME |
| 100 |
$hostname.example.net |
| 101 |
EOF |
| 102 |
cp $rootfs/etc/HOSTNAME $rootfs/etc/hostname |
| 103 |
|
| 104 |
# make needed devices, from Chris Willing's MAKEDEV.sh |
| 105 |
# http://www.vislab.uq.edu.au/howto/lxc/MAKEDEV.sh |
| 106 |
DEV=$rootfs/dev |
| 107 |
mkdir -p ${DEV} |
| 108 |
mknod -m 666 ${DEV}/null c 1 3 |
| 109 |
mknod -m 666 ${DEV}/zero c 1 5 |
| 110 |
mknod -m 666 ${DEV}/random c 1 8 |
| 111 |
mknod -m 666 ${DEV}/urandom c 1 9 |
| 112 |
mkdir -m 755 ${DEV}/pts |
| 113 |
mkdir -m 1777 ${DEV}/shm |
| 114 |
mknod -m 666 ${DEV}/tty c 5 0 |
| 115 |
mknod -m 600 ${DEV}/console c 5 1 |
| 116 |
mknod -m 666 ${DEV}/tty0 c 4 0 |
| 117 |
mknod -m 666 ${DEV}/tty1 c 4 1 |
| 118 |
mknod -m 666 ${DEV}/tty2 c 4 2 |
| 119 |
mknod -m 666 ${DEV}/tty3 c 4 3 |
| 120 |
mknod -m 666 ${DEV}/tty4 c 4 4 |
| 121 |
mknod -m 666 ${DEV}/tty5 c 4 5 |
| 122 |
mknod -m 666 ${DEV}/full c 1 7 |
| 123 |
mknod -m 600 ${DEV}/initctl p |
| 124 |
mknod -m 660 ${DEV}/loop0 b 7 0 |
| 125 |
mknod -m 660 ${DEV}/loop1 b 7 1 |
| 126 |
ln -s pts/ptmx ${DEV}/ptmx |
| 127 |
ln -s /proc/self/fd ${DEV}/fd |
| 128 |
|
| 129 |
echo "Adding an etc/fstab" |
| 130 |
cat >$rootfs/etc/fstab <<EOF |
| 131 |
none /run tmpfs defaults,mode=0755 0 0 |
| 132 |
EOF |
| 133 |
|
| 134 |
# simplify rc.6 and rc.S, http://www.vislab.uq.edu.au/howto/lxc/create_container.html |
| 135 |
# and some other small fixes for a clean boot |
| 136 |
cat >$rootfs/tmp/rcs.patch <<'EOF' |
| 137 |
--- ./etc/rc.orig/rc.6 2012-08-15 01:03:12.000000000 +0200 |
| 138 |
+++ ./etc/rc.d/rc.6 2013-02-17 10:26:30.888839354 +0100 |
| 139 |
@@ -9,6 +9,12 @@ |
| 140 |
# Author: Miquel van Smoorenburg <[email protected]> |
| 141 |
# Modified by: Patrick J. Volkerding, <[email protected]> |
| 142 |
# |
| 143 |
+# minor tweaks for an lxc container |
| 144 |
+# by Matteo Bernardini <[email protected]>, |
| 145 |
+# based also on Chris Willing's modifications |
| 146 |
+# http://www.vislab.uq.edu.au/howto/lxc/rc.6 |
| 147 |
+# a check for a container variable is made to jump sections |
| 148 |
+container="lxc" |
| 149 |
|
| 150 |
# Set the path. |
| 151 |
PATH=/sbin:/etc:/bin:/usr/bin |
| 152 |
@@ -37,6 +43,9 @@ |
| 153 |
;; |
| 154 |
esac |
| 155 |
|
| 156 |
+# lxc container check |
| 157 |
+if [ ! $container = "lxc" ]; then |
| 158 |
+ |
| 159 |
# Save the system time to the hardware clock using hwclock --systohc. |
| 160 |
if [ -x /sbin/hwclock ]; then |
| 161 |
# Check for a broken motherboard RTC clock (where ioports for rtc are |
| 162 |
@@ -53,6 +62,8 @@ |
| 163 |
fi |
| 164 |
fi |
| 165 |
|
| 166 |
+fi # end container check |
| 167 |
+ |
| 168 |
# Run any local shutdown scripts: |
| 169 |
if [ -x /etc/rc.d/rc.local_shutdown ]; then |
| 170 |
/etc/rc.d/rc.local_shutdown stop |
| 171 |
@@ -148,6 +159,9 @@ |
| 172 |
sleep 2 |
| 173 |
fi |
| 174 |
|
| 175 |
+# lxc container check |
| 176 |
+if [ ! $container = "lxc" ]; then |
| 177 |
+ |
| 178 |
# Shut down PCMCIA devices: |
| 179 |
if [ -x /etc/rc.d/rc.pcmcia ]; then |
| 180 |
. /etc/rc.d/rc.pcmcia stop |
| 181 |
@@ -155,11 +169,16 @@ |
| 182 |
/bin/sleep 5 |
| 183 |
fi |
| 184 |
|
| 185 |
+fi # end container check |
| 186 |
+ |
| 187 |
# Turn off process accounting: |
| 188 |
if [ -x /sbin/accton -a -r /var/log/pacct ]; then |
| 189 |
/sbin/accton off |
| 190 |
fi |
| 191 |
|
| 192 |
+# lxc container check |
| 193 |
+if [ ! $container = "lxc" ]; then |
| 194 |
+ |
| 195 |
# Terminate acpid before syslog: |
| 196 |
if [ -x /etc/rc.d/rc.acpid -a -r /var/run/acpid.pid ]; then # quit |
| 197 |
. /etc/rc.d/rc.acpid stop |
| 198 |
@@ -170,6 +189,8 @@ |
| 199 |
sh /etc/rc.d/rc.udev force-stop |
| 200 |
fi |
| 201 |
|
| 202 |
+fi # end container check |
| 203 |
+ |
| 204 |
# Kill all remaining processes. |
| 205 |
if [ ! "$1" = "fast" ]; then |
| 206 |
echo "Sending all processes the SIGTERM signal." |
| 207 |
@@ -179,6 +200,9 @@ |
| 208 |
/sbin/killall5 -9 |
| 209 |
fi |
| 210 |
|
| 211 |
+# lxc container check |
| 212 |
+if [ ! $container = "lxc" ]; then |
| 213 |
+ |
| 214 |
# Try to turn off quota. |
| 215 |
if /bin/grep -q quota /etc/fstab ; then |
| 216 |
if [ -x /sbin/quotaoff ]; then |
| 217 |
@@ -187,6 +211,8 @@ |
| 218 |
fi |
| 219 |
fi |
| 220 |
|
| 221 |
+fi # end container check |
| 222 |
+ |
| 223 |
# Carry a random seed between reboots. |
| 224 |
echo "Saving random seed from /dev/urandom in /etc/random-seed." |
| 225 |
# Use the pool size from /proc, or 512 bytes: |
| 226 |
@@ -205,6 +231,9 @@ |
| 227 |
rm -f /var/lock/subsys/* |
| 228 |
fi |
| 229 |
|
| 230 |
+# lxc container check |
| 231 |
+if [ ! $container = "lxc" ]; then |
| 232 |
+ |
| 233 |
# Turn off swap: |
| 234 |
echo "Turning off swap." |
| 235 |
/sbin/swapoff -a |
| 236 |
@@ -216,6 +245,8 @@ |
| 237 |
echo "Remounting root filesystem read-only." |
| 238 |
/bin/mount -v -n -o remount,ro / |
| 239 |
|
| 240 |
+fi # end container check |
| 241 |
+ |
| 242 |
# This never hurts: |
| 243 |
/bin/sync |
| 244 |
|
| 245 |
@@ -240,12 +271,17 @@ |
| 246 |
done |
| 247 |
fi |
| 248 |
|
| 249 |
+# lxc container check |
| 250 |
+if [ ! $container = "lxc" ]; then |
| 251 |
+ |
| 252 |
# Deactivate LVM volume groups: |
| 253 |
if [ -r /etc/lvmtab -o -d /etc/lvm/backup ]; then |
| 254 |
echo "Deactivating LVM volume groups:" |
| 255 |
/sbin/vgchange -an --ignorelockingfailure |
| 256 |
fi |
| 257 |
|
| 258 |
+fi # end container check |
| 259 |
+ |
| 260 |
# This never hurts again (especially since root-on-LVM always fails |
| 261 |
# to deactivate the / logical volume... but at least it was |
| 262 |
# remounted as read-only first) |
| 263 |
@@ -258,6 +294,9 @@ |
| 264 |
# This is to ensure all processes have completed on SMP machines: |
| 265 |
wait |
| 266 |
|
| 267 |
+# lxc container check |
| 268 |
+if [ ! $container = "lxc" ]; then |
| 269 |
+ |
| 270 |
if [ -x /sbin/genpowerd ]; then |
| 271 |
# See if this is a powerfail situation: |
| 272 |
if /bin/egrep -q "FAIL|SCRAM" /etc/upsstatus 2> /dev/null ; then |
| 273 |
@@ -274,6 +313,13 @@ |
| 274 |
fi |
| 275 |
fi |
| 276 |
|
| 277 |
+else |
| 278 |
+ |
| 279 |
+# confirm successful shutdown of the container |
| 280 |
+echo ; echo "* container stopped. *" ; echo |
| 281 |
+ |
| 282 |
+fi # end container check |
| 283 |
+ |
| 284 |
# Now halt (poweroff with APM or ACPI enabled kernels) or reboot. |
| 285 |
if [ "$command" = "reboot" ]; then |
| 286 |
echo "Rebooting." |
| 287 |
--- ./etc/rc.orig/rc.S 2012-09-13 21:38:34.000000000 +0200 |
| 288 |
+++ ./etc/rc.d/rc.S 2013-02-17 09:39:41.579799641 +0100 |
| 289 |
@@ -4,9 +4,18 @@ |
| 290 |
# |
| 291 |
# Mostly written by: Patrick J. Volkerding, <[email protected]> |
| 292 |
# |
| 293 |
+# minor tweaks for an lxc container |
| 294 |
+# by Matteo Bernardini <[email protected]>, |
| 295 |
+# based also on Chris Willing's modifications |
| 296 |
+# http://www.vislab.uq.edu.au/howto/lxc/rc.S |
| 297 |
+# a check for a container variable is made to jump sections |
| 298 |
+container="lxc" |
| 299 |
|
| 300 |
PATH=/sbin:/usr/sbin:/bin:/usr/bin |
| 301 |
|
| 302 |
+# lxc container check |
| 303 |
+if [ ! $container = "lxc" ]; then |
| 304 |
+ |
| 305 |
# Try to mount /proc: |
| 306 |
/sbin/mount -v proc /proc -n -t proc 2> /dev/null |
| 307 |
|
| 308 |
@@ -254,10 +263,27 @@ |
| 309 |
read junk; |
| 310 |
fi # Done checking root filesystem |
| 311 |
|
| 312 |
+else |
| 313 |
+ # We really don't want to start udev in the container |
| 314 |
+ if [ -f /etc/rc.d/rc.udev ]; then |
| 315 |
+ chmod -x /etc/rc.d/rc.udev |
| 316 |
+ fi |
| 317 |
+ # Alsa won't work |
| 318 |
+ if [ -f /etc/rc.d/rc.alsa ]; then |
| 319 |
+ chmod -x /etc/rc.d/rc.alsa |
| 320 |
+ fi |
| 321 |
+ # This too |
| 322 |
+ if [ -f /etc/rc.d/rc.loop ]; then |
| 323 |
+ chmod -x /etc/rc.d/rc.loop |
| 324 |
+ fi |
| 325 |
+fi # end container check |
| 326 |
|
| 327 |
# Any /etc/mtab that exists here is old, so we start with a new one: |
| 328 |
/bin/rm -f /etc/mtab{,~,.tmp} && /bin/touch /etc/mtab |
| 329 |
|
| 330 |
+# lxc container check |
| 331 |
+if [ ! $container = "lxc" ]; then |
| 332 |
+ |
| 333 |
# Add entry for / to /etc/mtab: |
| 334 |
/sbin/mount -f -w / |
| 335 |
|
| 336 |
@@ -337,6 +363,8 @@ |
| 337 |
# mounted read-write. |
| 338 |
/sbin/swapon -a 2> /dev/null |
| 339 |
|
| 340 |
+fi # end container check |
| 341 |
+ |
| 342 |
# Clean up some temporary files: |
| 343 |
rm -f /var/run/* /var/run/*/* /var/run/*/*/* /etc/nologin \ |
| 344 |
/etc/dhcpc/*.pid /etc/forcefsck /etc/fastboot \ |
| 345 |
@@ -364,7 +392,7 @@ |
| 346 |
# if the first line of that file begins with the word 'Linux'. |
| 347 |
# You are free to modify the rest of the file as you see fit. |
| 348 |
if [ -x /bin/sed ]; then |
| 349 |
- /bin/sed -i "{1s/^Linux.*/$(/bin/uname -sr)\./}" /etc/motd |
| 350 |
+ /bin/sed -i "{1s/^Linux.*/$(/bin/uname -sr) lxc container\./}" /etc/motd |
| 351 |
fi |
| 352 |
|
| 353 |
# If there are SystemV init scripts for this runlevel, run them. |
| 354 |
@@ -372,6 +400,9 @@ |
| 355 |
. /etc/rc.d/rc.sysvinit |
| 356 |
fi |
| 357 |
|
| 358 |
+# lxc container check |
| 359 |
+if [ ! $container = "lxc" ]; then |
| 360 |
+ |
| 361 |
# Run serial port setup script: |
| 362 |
# CAREFUL! This can make some systems hang if the rc.serial script isn't |
| 363 |
# set up correctly. If this happens, you may have to edit the file from a |
| 364 |
@@ -380,6 +411,8 @@ |
| 365 |
sh /etc/rc.d/rc.serial start |
| 366 |
fi |
| 367 |
|
| 368 |
+fi # end container check |
| 369 |
+ |
| 370 |
# Carry an entropy pool between reboots to improve randomness. |
| 371 |
if [ -f /etc/random-seed ]; then |
| 372 |
echo "Using /etc/random-seed to initialize /dev/urandom." |
| 373 |
--- ./etc/rc.orig/rc.M 2012-09-25 19:47:07.000000000 +0200 |
| 374 |
+++ ./etc/rc.d/rc.M 2013-02-17 09:39:41.579799641 +0100 |
| 375 |
@@ -10,6 +10,10 @@ |
| 376 |
# Author: Fred N. van Kempen, <[email protected]> |
| 377 |
# Heavily modified by Patrick Volkerding <[email protected]> |
| 378 |
# |
| 379 |
+# minor tweaks for an lxc container |
| 380 |
+# by Matteo Bernardini <[email protected]>: |
| 381 |
+# a check for a container variable is made to jump sections |
| 382 |
+container="lxc" |
| 383 |
|
| 384 |
# Tell the viewers what's going to happen. |
| 385 |
echo "Going multiuser..." |
| 386 |
@@ -20,6 +24,9 @@ |
| 387 |
/sbin/ldconfig & |
| 388 |
fi |
| 389 |
|
| 390 |
+# lxc container check |
| 391 |
+if [ ! $container = "lxc" ]; then |
| 392 |
+ |
| 393 |
# Screen blanks after 15 minutes idle time, and powers down in one hour |
| 394 |
# if the kernel supports APM or ACPI power management: |
| 395 |
/bin/setterm -blank 15 -powersave powerdown -powerdown 60 |
| 396 |
@@ -33,6 +40,8 @@ |
| 397 |
/bin/hostname darkstar |
| 398 |
fi |
| 399 |
|
| 400 |
+fi # end container check |
| 401 |
+ |
| 402 |
# Set the permissions on /var/log/dmesg according to whether the kernel |
| 403 |
# permits non-root users to access kernel dmesg information: |
| 404 |
if [ -r /proc/sys/kernel/dmesg_restrict ]; then |
| 405 |
@@ -135,6 +144,9 @@ |
| 406 |
chmod 755 / 2> /dev/null |
| 407 |
chmod 1777 /tmp /var/tmp |
| 408 |
|
| 409 |
+# lxc container check |
| 410 |
+if [ ! $container = "lxc" ]; then |
| 411 |
+ |
| 412 |
# Start APM or ACPI daemon. |
| 413 |
# If APM is enabled in the kernel, start apmd: |
| 414 |
if [ -e /proc/apm ]; then |
| 415 |
@@ -146,6 +158,8 @@ |
| 416 |
. /etc/rc.d/rc.acpid start |
| 417 |
fi |
| 418 |
|
| 419 |
+fi # end container check |
| 420 |
+ |
| 421 |
# Update any existing icon cache files: |
| 422 |
if find /usr/share/icons 2> /dev/null | grep -q icon-theme.cache ; then |
| 423 |
for theme_dir in /usr/share/icons/* ; do |
| 424 |
--- ./etc/rc.orig/rc.inet1 2012-08-05 19:13:27.000000000 +0200 |
| 425 |
+++ ./etc/rc.d/rc.inet1 2013-02-17 09:39:41.579799641 +0100 |
| 426 |
@@ -3,6 +3,11 @@ |
| 427 |
# This script is used to bring up the various network interfaces. |
| 428 |
# |
| 429 |
# @(#)/etc/rc.d/rc.inet1 10.2 Sun Jul 24 12:45:56 PDT 2005 (pjv) |
| 430 |
+# |
| 431 |
+# minor tweaks for an lxc container |
| 432 |
+# by Matteo Bernardini <[email protected]>: |
| 433 |
+# a check for a container variable is made to jump sections |
| 434 |
+container="lxc" |
| 435 |
|
| 436 |
############################ |
| 437 |
# READ NETWORK CONFIG FILE # |
| 438 |
@@ -105,6 +110,10 @@ |
| 439 |
[ "${IFNAME[$i]}" = "${1}" ] && break |
| 440 |
i=$(($i+1)) |
| 441 |
done |
| 442 |
+ |
| 443 |
+ # lxc container check |
| 444 |
+ if [ ! $container = "lxc" ]; then |
| 445 |
+ |
| 446 |
# If the interface is a bridge, then create it first: |
| 447 |
[ -n "${BRNICS[$i]}" ] && br_open $i |
| 448 |
# If the interface isn't in the kernel yet (but there's an alias for it in |
| 449 |
@@ -115,6 +124,9 @@ |
| 450 |
/sbin/modprobe ${1} |
| 451 |
fi |
| 452 |
fi |
| 453 |
+ |
| 454 |
+ fi # end container check |
| 455 |
+ |
| 456 |
if grep `echo ${1}: | cut -f 1 -d :`: /proc/net/dev 1> /dev/null ; then # interface exists |
| 457 |
if ! /sbin/ifconfig | grep -w "${1}" 1>/dev/null || \ |
| 458 |
! /sbin/ifconfig ${1} | grep -w inet 1> /dev/null ; then # interface not up or not configured |
| 459 |
EOF |
| 460 |
( cd $rootfs ; patch -p1 < tmp/rcs.patch ; rm tmp/rcs.patch ) |
| 461 |
|
| 462 |
# restart rc.inet1 to have routing for the loop device |
| 463 |
echo "/etc/rc.d/rc.inet1 restart" >> $rootfs/etc/rc.d/rc.local |
| 464 |
|
| 465 |
# reduce the number of local consoles: two should be enough |
| 466 |
sed -i '/^c3\|^c4\|^c5\|^c6/s/^/# /' $rootfs/etc/inittab |
| 467 |
|
| 468 |
# better not use this in a container |
| 469 |
sed -i 's/.*genpowerfail.*//' $rootfs/etc/inittab |
| 470 |
|
| 471 |
# add a message to rc.local that confirms successful container startup |
| 472 |
echo "echo ; echo \"* container $name started. *\" ; echo" >> $rootfs/etc/rc.d/rc.local |
| 473 |
|
| 474 |
# borrow the time configuration from the local machine |
| 475 |
cp -a /etc/localtime $rootfs/etc/localtime |
| 476 |
|
| 477 |
return 0 |
| 478 |
} |
| 479 |
|
| 480 |
copy_slackware() |
| 481 |
{ |
| 482 |
rootfs=$1 |
| 483 |
|
| 484 |
# make a local copy of the installed filesystem |
| 485 |
echo -n "Copying rootfs to $rootfs..." |
| 486 |
mkdir -p $rootfs |
| 487 |
cp -a $cache/rootfs-$release-$arch/* $rootfs/ || exit 1 |
| 488 |
|
| 489 |
# fix fstab with the actual path |
| 490 |
sed -i "s|$cache/rootfs-$release-$arch|$rootfs|" $rootfs/etc/fstab |
| 491 |
|
| 492 |
return 0 |
| 493 |
} |
| 494 |
|
| 495 |
install_slackware() |
| 496 |
{ |
| 497 |
rootfs=$1 |
| 498 |
mkdir -p /var/lock/subsys/ |
| 499 |
( |
| 500 |
flock -n -x 9 |
| 501 |
if [ $? -ne 0 ]; then |
| 502 |
echo "Cache repository is busy." |
| 503 |
return 1 |
| 504 |
fi |
| 505 |
|
| 506 |
if [ "$arch" == "x86_64" ]; then |
| 507 |
PKGMAIN=slackware64 |
| 508 |
elif [ "$arch" == "arm" ]; then |
| 509 |
PKGMAIN=slackwarearm |
| 510 |
else |
| 511 |
PKGMAIN=slackware |
| 512 |
fi |
| 513 |
|
| 514 |
export CONF=$cache/slackpkg-conf |
| 515 |
export ROOT=$cache/rootfs-$release-$arch |
| 516 |
|
| 517 |
mkdir -p $cache/cache-$release-$arch $cache/rootfs-$release-$arch \ |
| 518 |
$cache/slackpkg-$release-$arch $CONF/templates |
| 519 |
|
| 520 |
echo "$MIRROR/$PKGMAIN-$release/" > $CONF/mirrors |
| 521 |
touch $CONF/blacklist |
| 522 |
|
| 523 |
cat <<EOF > $CONF/slackpkg.conf |
| 524 |
# v2.8 |
| 525 |
ARCH=$arch |
| 526 |
TEMP=$cache/cache-$release-$arch |
| 527 |
WORKDIR=$cache/slackpkg-$release-$arch |
| 528 |
DELALL=off |
| 529 |
CHECKMD5=on |
| 530 |
CHECKGPG=on |
| 531 |
CHECKSIZE=off |
| 532 |
PRIORITY=( patches %PKGMAIN extra pasture testing ) |
| 533 |
POSTINST=on |
| 534 |
ONLY_NEW_DOTNEW=off |
| 535 |
ONOFF=on |
| 536 |
DOWNLOAD_ALL=on |
| 537 |
DIALOG=off |
| 538 |
BATCH=on |
| 539 |
DEFAULT_ANSWER=y |
| 540 |
USE_INCLUDES=on |
| 541 |
SPINNING=off |
| 542 |
EOF |
| 543 |
|
| 544 |
# thanks to Vincent Batts for this list of packages |
| 545 |
# (that I modified a little :P) |
| 546 |
# http://connie.slackware.com/~vbatts/minimal/ |
| 547 |
cat <<EOF > $CONF/templates/minimal-lxc.template |
| 548 |
aaa_base |
| 549 |
aaa_elflibs |
| 550 |
aaa_terminfo |
| 551 |
bash |
| 552 |
bin |
| 553 |
bzip2 |
| 554 |
coreutils |
| 555 |
dhcpcd |
| 556 |
dialog |
| 557 |
diffutils |
| 558 |
e2fsprogs |
| 559 |
elvis |
| 560 |
etc |
| 561 |
findutils |
| 562 |
gawk |
| 563 |
glibc-solibs |
| 564 |
gnupg |
| 565 |
grep |
| 566 |
gzip |
| 567 |
hostname |
| 568 |
iputils |
| 569 |
libpsl |
| 570 |
libunistring |
| 571 |
logrotate |
| 572 |
mpfr |
| 573 |
net-tools |
| 574 |
network-scripts |
| 575 |
ncurses |
| 576 |
openssh |
| 577 |
openssl-solibs |
| 578 |
pcre2 |
| 579 |
pkgtools |
| 580 |
procps-ng |
| 581 |
sed |
| 582 |
shadow |
| 583 |
sharutils |
| 584 |
slackpkg |
| 585 |
sysklogd |
| 586 |
sysvinit |
| 587 |
sysvinit-functions |
| 588 |
sysvinit-scripts |
| 589 |
tar |
| 590 |
udev |
| 591 |
util-linux |
| 592 |
wget |
| 593 |
which |
| 594 |
xz |
| 595 |
EOF |
| 596 |
|
| 597 |
TEMPLATE=${TEMPLATE:-minimal-lxc} |
| 598 |
if [ ! "$TEMPLATE" = "minimal-lxc" ]; then |
| 599 |
if [ -f /etc/slackpkg/templates/$TEMPLATE.template ]; then |
| 600 |
cat /etc/slackpkg/templates/$TEMPLATE.template \ |
| 601 |
> $CONF/templates/$TEMPLATE.template |
| 602 |
else |
| 603 |
TEMPLATE="minimal-lxc" |
| 604 |
fi |
| 605 |
fi |
| 606 |
|
| 607 |
# clean previous installs |
| 608 |
rm -fR $ROOT/* |
| 609 |
|
| 610 |
slackpkg -default_answer=n update |
| 611 |
slackpkg install-template $TEMPLATE |
| 612 |
|
| 613 |
# add a slackpkg default mirror |
| 614 |
echo "$MIRROR/$PKGMAIN-$release/" >> $ROOT/etc/slackpkg/mirrors |
| 615 |
|
| 616 |
# blacklist the devs package (we have to use our premade devices). |
| 617 |
# do the same with the kernel packages (we use the host's one), |
| 618 |
# but leave available headers and sources |
| 619 |
echo "devs" >> $ROOT/etc/slackpkg/blacklist |
| 620 |
sed -i \ |
| 621 |
-e "s|^#kernel-|kernel-|" \ |
| 622 |
-e "s|^kernel-headers|#kernel-headers|" \ |
| 623 |
-e "s|^kernel-source|#kernel-source|" \ |
| 624 |
$ROOT/etc/slackpkg/blacklist |
| 625 |
|
| 626 |
# force klog to use the system call interface to the kernel message |
| 627 |
# buffers - needed for unprivileged containers |
| 628 |
sed -i 's|3\ \-x|3 -x -s|' $ROOT/etc/rc.d/rc.syslog || true |
| 629 |
|
| 630 |
return 0 |
| 631 |
|
| 632 |
) 9>/var/lock/subsys/lxc |
| 633 |
|
| 634 |
return $? |
| 635 |
} |
| 636 |
|
| 637 |
copy_configuration() |
| 638 |
{ |
| 639 |
path=$1 |
| 640 |
rootfs=$2 |
| 641 |
name=$3 |
| 642 |
|
| 643 |
cat <<EOF >> $path/config |
| 644 |
|
| 645 |
lxc.uts.name = $name |
| 646 |
lxc.arch = $arch |
| 647 |
|
| 648 |
lxc.mount.fstab = $rootfs/etc/fstab |
| 649 |
|
| 650 |
lxc.include = ${LXC_TEMPLATE_CONFIG}/slackware.common.conf |
| 651 |
EOF |
| 652 |
|
| 653 |
if [ $? -ne 0 ]; then |
| 654 |
echo "Failed to add configuration." |
| 655 |
return 1 |
| 656 |
fi |
| 657 |
|
| 658 |
return 0 |
| 659 |
} |
| 660 |
|
| 661 |
clean() |
| 662 |
{ |
| 663 |
if [ ! -e $cache ]; then |
| 664 |
exit 0 |
| 665 |
fi |
| 666 |
|
| 667 |
# lock, so we won't purge while someone is creating a repository |
| 668 |
( |
| 669 |
flock -n -x 9 |
| 670 |
if [ $? != 0 ]; then |
| 671 |
echo "Cache repository is busy." |
| 672 |
exit 1 |
| 673 |
fi |
| 674 |
|
| 675 |
echo -n "Purging the download cache..." |
| 676 |
rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1 |
| 677 |
exit 0 |
| 678 |
|
| 679 |
) 9>/var/lock/subsys/lxc |
| 680 |
} |
| 681 |
|
| 682 |
usage() |
| 683 |
{ |
| 684 |
cat <<EOF |
| 685 |
$1 -h|--help -p|--path=<path> --clean |
| 686 |
EOF |
| 687 |
return 0 |
| 688 |
} |
| 689 |
|
| 690 |
options=$(getopt -o hp:n:a:r:c -l help,rootfs:,path:,name:,arch:,release:,clean -- "$@") |
| 691 |
if [ $? -ne 0 ]; then |
| 692 |
usage $(basename $0) |
| 693 |
exit 1 |
| 694 |
fi |
| 695 |
eval set -- "$options" |
| 696 |
|
| 697 |
while true |
| 698 |
do |
| 699 |
case "$1" in |
| 700 |
-h|--help) usage $0 && exit 0;; |
| 701 |
-p|--path) path=$2; shift 2;; |
| 702 |
--rootfs) rootfs=$2; shift 2;; |
| 703 |
-a|--arch) arch=$2; shift 2;; |
| 704 |
-r|--release) release=$2; shift 2;; |
| 705 |
-n|--name) name=$2; shift 2;; |
| 706 |
-c|--clean) clean=$2; shift 2;; |
| 707 |
--) shift 1; break ;; |
| 708 |
*) break ;; |
| 709 |
esac |
| 710 |
done |
| 711 |
|
| 712 |
if [ ! -z "$clean" -a -z "$path" ]; then |
| 713 |
clean || exit 1 |
| 714 |
exit 0 |
| 715 |
fi |
| 716 |
|
| 717 |
type installpkg |
| 718 |
if [ $? -ne 0 ]; then |
| 719 |
echo "'installpkg' command is missing." |
| 720 |
exit 1 |
| 721 |
fi |
| 722 |
|
| 723 |
type slackpkg |
| 724 |
if [ $? -ne 0 ]; then |
| 725 |
echo "'slackpkg' command is missing." |
| 726 |
exit 1 |
| 727 |
fi |
| 728 |
|
| 729 |
if [ -z "$path" ]; then |
| 730 |
echo "'path' parameter is required." |
| 731 |
exit 1 |
| 732 |
fi |
| 733 |
|
| 734 |
if [ "$(id -u)" != "0" ]; then |
| 735 |
echo "This script should be run as 'root'." |
| 736 |
exit 1 |
| 737 |
fi |
| 738 |
|
| 739 |
# If no release version was specified, use current |
| 740 |
release=${release:-current} |
| 741 |
|
| 742 |
if [ -z "$name" ]; then |
| 743 |
# no name given? set a default one |
| 744 |
name=slackwarecontainer |
| 745 |
fi |
| 746 |
|
| 747 |
# detect rootfs |
| 748 |
config="$path/config" |
| 749 |
if [ -z "$rootfs" ]; then |
| 750 |
if grep -q '^lxc.rootfs.path' $config 2>/dev/null ; then |
| 751 |
rootfs=$(awk -F= '/^lxc.rootfs.path =/{ print $2 }' $config) |
| 752 |
else |
| 753 |
rootfs=$path/rootfs |
| 754 |
fi |
| 755 |
fi |
| 756 |
|
| 757 |
echo |
| 758 |
|
| 759 |
set -e |
| 760 |
|
| 761 |
install_slackware $rootfs |
| 762 |
if [ $? -ne 0 ]; then |
| 763 |
echo "Failed to install slackware." |
| 764 |
exit 1 |
| 765 |
fi |
| 766 |
|
| 767 |
echo |
| 768 |
|
| 769 |
configure_slackware $cache/rootfs-$release-$arch $name |
| 770 |
if [ $? -ne 0 ]; then |
| 771 |
echo "Failed to configure slackware for a container." |
| 772 |
exit 1 |
| 773 |
fi |
| 774 |
|
| 775 |
echo |
| 776 |
|
| 777 |
rootfs=$path/rootfs |
| 778 |
copy_slackware $rootfs |
| 779 |
if [ $? -ne 0 ]; then |
| 780 |
echo "Failed to copy rootfs." |
| 781 |
exit 1 |
| 782 |
fi |
| 783 |
|
| 784 |
echo |
| 785 |
|
| 786 |
copy_configuration $path $rootfs $name |
| 787 |
if [ $? -ne 0 ]; then |
| 788 |
echo "Failed to write configuration file." |
| 789 |
exit 1 |
| 790 |
fi |
| 791 |
|
| 792 |
if [ ! -z $clean ]; then |
| 793 |
clean || exit 1 |
| 794 |
exit 0 |
| 795 |
fi |