File: lxc-plamo.in

package info (click to toggle)
lxc 1%3A1.0.6-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 4,800 kB
  • sloc: ansic: 33,735; sh: 11,868; python: 1,223; makefile: 734
file content (389 lines) | stat: -rw-r--r-- 11,877 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/bin/bash -eu

#
# template script for generating Plamo Linux container for LXC
#

#
# lxc: linux Container library

# Authors:
# KATOH Yasufumi <karma@jazz.email.ne.jp>
# TAMUKI Shoichi <tamuki@linet.gr.jp>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# ref. https://github.com/Ponce/lxc-slackware/blob/master/lxc-slackware
#      lxc-ubuntu script

# Detect use under userns (unsupported)
for arg in "$@"; do
    [ "$arg" = "--" ] && break
    if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
        echo "This template can't be used for unprivileged containers." 1>&2
        echo "You may want to try the \"download\" template instead." 1>&2
        exit 1
    fi
done

# Make sure the usual locations are in PATH
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin

[ -r /etc/default/lxc ] && . /etc/default/lxc

DLSCHEME=${DLSCHEME:-"http"}
MIRRORSRV=${MIRRORSRV:-"www.ring.gr.jp"}
MIRRORPATH=${MIRRORPATH:-"/pub/linux/Plamo"}
CATEGORIES=${CATEGORIES-"00_base 01_minimum"}
EXTRACTGRS=${EXTRACTGRS-""}
IGNOREPKGS=${IGNOREPKGS-"grub kernel lilo linux_firmware microcode_ctl
    cpufreqd cpufrequtils gpm"}
ADDONPKGS=${ADDONPKGS-"`echo contrib/Hamradio/{morse,qrq}`"}

download_plamo() {
  # check the mini plamo was not already downloaded
  if ! mkdir -p $ptcache ; then
    echo "Failed to create '$ptcache' directory."
    return 1
  fi
  # download a mini plamo into a cache
  echo "Downloading Plamo-$release minimal..."
  cd $ptcache
  case $DLSCHEME in http) depth=2 ;; ftp) depth=3 ;; esac
  rej=${IGNOREPKGS%% *} ; [ -n "$rej" ] && rej="$rej-*"
  if [ `echo $IGNOREPKGS | wc -w` -gt 1 ] ; then
    for p in ${IGNOREPKGS#* } ; do rej="$rej,$p-*" ; done
  fi
  for i in $CATEGORIES ; do
    wget -nv -e robots=off -r -l $depth -nd -A .tgz,.txz -R "$rej" \
        -I $MIRRORPATH/Plamo-$release/$arch/plamo/$i \
        -X $MIRRORPATH/Plamo-$release/$arch/plamo/$i/old \
        $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/plamo/$i
    if [ $? -ne 0 ] ; then
      echo "Failed to download the rootfs, aborting."
      return 1
    fi
  done
  for i in $EXTRACTGRS ; do
    wget -nv -e robots=off -r -l $depth -nd -A .tgz,.txz -R "$rej" \
        -I $MIRRORPATH/Plamo-$release/$arch/contrib/$i \
        -X $MIRRORPATH/Plamo-$release/$arch/contrib/$i/old \
        $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/contrib/$i
    if [ $? -ne 0 ] ; then
      echo "Failed to download the rootfs, aborting."
      return 1
    fi
  done
  for p in $ADDONPKGS ; do
    wget -nv -e robots=off -r -l $depth -nd -A "`basename $p`-*" \
        -I $MIRRORPATH/Plamo-$release/$arch/`dirname $p` \
        -X $MIRRORPATH/Plamo-$release/$arch/`dirname $p`/old \
        $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/`dirname $p`
    if [ $? -ne 0 ] ; then
      echo "Failed to download the rootfs, aborting."
      return 1
    fi
  done
  mv $ptcache $dlcache
  echo "Download complete."
  return 0
}

copy_plamo() {
  # make a local copy of the mini plamo
  echo "Copying $rtcache to $rootfs..."
  mkdir -p $rootfs
  find $rtcache -mindepth 1 -maxdepth 1 -exec cp -a {} $rootfs \; || return 1
  return 0
}

install_plamo() {
  mkdir -p @LOCALSTATEDIR@/lock/subsys
  (
    if ! flock -n 9 ; then
      echo "Cache repository is busy."
      return 1
    fi
    echo "Checking cache download in $dlcache..."
    if [ ! -d $dlcache ] ; then
      if ! download_plamo ; then
        echo "Failed to download plamo $release base packages."
        return 1
      fi
    fi
    # install "installpkg" command temporarily with static linked tar
    # command into the lxc cache directory to keep the original uid/
    # gid of files/directories.
    echo "Installing 'installpkg' command into $dlcache/sbin..."
    ( cd $dlcache ; tar xpJf hdsetup-*.txz ; rm -rf tmp usr var )
    sed -i "/ldconfig/!s@/sbin@$dlcache&@g" $dlcache/sbin/installpkg*
    PATH=$dlcache/sbin:$PATH
    echo "Installing packages to $rtcache..."
    if [ ! -d $rtcache ] ; then
      mkdir -p $rtcache
      for p in `ls -cr $dlcache/*.t?z` ; do
        installpkg -root $rtcache -priority ADD $p
      done
    fi
    echo "Copy $rtcache to $rootfs..."
    if ! copy_plamo ; then
      echo "Failed to copy rootfs."
      return 1
    fi
    return 0
  ) 9> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
}

configure_plamo() {
  # create /dev
  chmod    666 $rootfs/dev/null
  mknod -m 666 $rootfs/dev/zero c 1 5
  chmod    666 $rootfs/dev/random
  mknod -m 666 $rootfs/dev/urandom c 1 9
  mkdir -m 755 $rootfs/dev/pts
  chmod    666 $rootfs/dev/tty
  chmod    600 $rootfs/dev/console
  mknod -m 666 $rootfs/dev/tty0 c 4 0
  mknod -m 666 $rootfs/dev/tty1 c 4 1
  mknod -m 666 $rootfs/dev/tty2 c 4 2
  mknod -m 666 $rootfs/dev/tty3 c 4 3
  mknod -m 666 $rootfs/dev/tty4 c 4 4
  mknod -m 666 $rootfs/dev/full c 1 7
  mknod -m 600 $rootfs/dev/initctl p
  mknod -m 666 $rootfs/dev/ptmx c 5 2
  # suppress log level output for udev
  sed -i 's/="err"/=0/' $rootfs/etc/udev/udev.conf
  # /etc/fstab
  cat <<- "EOF" > $rootfs/etc/fstab
	none             /proc    proc        defaults   0   0
	none             /sys     sysfs       defaults   0   0
	none             /dev     tmpfs       defaults   0   0
	none		    /tmp     tmpfs       defaults   0   0
	none            /dev/pts        devpts  gid=5,mode=620    0 0
	none            /proc/bus/usb             usbfs        noauto   0   0
	none             /var/lib/nfs/rpc_pipefs rpc_pipefs  defaults   0   0
	EOF
  # /etc/inittab
  cat <<- "EOF" | patch $rootfs/etc/inittab
	32,33c32,33
	< # What to do when power fails (shutdown to single user).
	< pf::powerfail:/sbin/shutdown -f +5 "THE POWER IS FAILING"
	---
	> # What to do when power fails (shutdown).
	> pf::powerfail:/sbin/shutdown -h +0 "THE POWER IS FAILING"
	47a48
	> 1:1235:respawn:/sbin/agetty 38400 console
	52,53d52
	< c5:1235:respawn:/sbin/agetty 38400 tty5 linux
	< c6:12345:respawn:/sbin/agetty 38400 tty6 linux
	EOF
  # set the hostname
  echo "$name" > $rootfs/etc/HOSTNAME
  # set minimal hosts
  echo "127.0.0.1 localhost $name" > $rootfs/etc/hosts
  # configure the network using the dhcp
  echo "DHCP" > $rootfs/var/run/inet1-scheme
  # localtime (JST)
  ln -s ../usr/share/zoneinfo/Asia/Tokyo $rootfs/etc/localtime
  # disable pam_loginuid.so in /etc/pam.d/login (for libvirt's lxc driver)
  sed -i '/pam_loginuid/s/^/#/' $rootfs/etc/pam.d/login
  # glibc configure
  mv $rootfs/etc/ld.so.conf{.new,}
  chroot $rootfs ldconfig
  # root password
  echo "Setting root password to 'root'..."
  echo "root:root" | chroot $rootfs chpasswd
  echo "Please change root password!"
  # /etc/rc.d/rc.S
  ed - $rootfs/etc/rc.d/rc.S <<- "EOF"
	/^mount -w -n -t proc/;/^# ln -s \/bin\/true/-1d
	/^mknod \/dev\/unikey/;/^# Clean \/etc\/mtab/-2d
	/^# copy the rules/;/^# Set the hostname/-1d
	/^# Check the integrity/;/^# Clean up temporary/-1d
	w
	EOF
  # /etc/rc.d/rc.M
  ed - $rootfs/etc/rc.d/rc.M <<- "EOF"
	/^# Screen blanks/;/^# Initialize ip6tables/-1d
	/^# Initialize sysctl/;/^echo "Starting services/-1d
	/^sync/;/^# All done/-1d
	w
	EOF
  # /etc/rc.d/rc.inet1.tradnet
  head -n-93 $rootfs/sbin/netconfig.tradnet > /tmp/netconfig.rconly
  cat <<- EOF >> /tmp/netconfig.rconly
	PCMCIA=n
	RC=$rootfs/etc/rc.d/rc.inet1.tradnet
	IFCONFIG=sbin/ifconfig
	ROUTE=sbin/route
	INET1SCHEME=var/run/inet1-scheme
	IPADDR=127.0.0.1
	NETWORK=127.0.0.0
	DHCPCD=usr/sbin/dhclient
	LOOPBACK=y
	make_config_file
	EOF
  rm -f $rootfs/etc/rc.d/rc.inet1.tradnet
  sh /tmp/netconfig.rconly
  rm -f /tmp/netconfig.rconly
  sed -i '/cmdline/s/if/& false \&\&/' $rootfs/etc/rc.d/rc.inet1.tradnet
  return 0
}

copy_configuration() {
  ret=0
  cat <<- EOF >> $path/config || let ret++
	lxc.utsname = $name
	lxc.mount = $path/fstab
	lxc.arch = $arch
	EOF
  if [ -f "@LXCTEMPLATECONFIG@/plamo.common.conf" ] ; then
    cat <<- "EOF" >> $path/config || let ret++
	
	lxc.include = @LXCTEMPLATECONFIG@/plamo.common.conf
	EOF
  fi
  # create the fstab (empty by default)
  touch $path/fstab || let ret++
  if [ $ret -ne 0 ] ; then
    echo "Failed to add configuration."
    return 1
  fi
  return 0
}

post_process() {
  # nothing do in Plamo Linux
  true
}

do_bindhome() {
  # bind-mount the user's path into the container's /home
  h=`getent passwd $bindhome | cut -d: -f6`
  mkdir -p $rootfs/$h
  echo "$h $rootfs/$h none bind 0 0" >> $path/fstab
  # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
  if ! pwd=`getent passwd $bindhome` ; then
    echo "Warning: failed to copy password entry for $bindhome."
  else
    echo $pwd >> $rootfs/etc/passwd
  fi
  echo `getent shadow $bindhome` >> $rootfs/etc/shadow
}

cleanup() {
  [ -d $dlcache -a -d $rtcache ] || return 0
  # lock, so we won't purge while someone is creating a repository
  (
    if ! flock -n 9 ; then
      echo "Cache repository is busy."
      return 1
    fi
    echo "Purging the download cache..."
    rm -rf --one-file-system $dlcache $rtcache || return 1
    echo "Done."
    return 0
  ) 9> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
}

usage() {
  cat <<- EOF
	$prog [-h|--help] -p|--path=<path> -n|--name=<name> --rootfs=<rootfs>
	          [-c|--clean] [-r|--release=<release>] [-a|--arch=<arch>]
	          [-b|--bindhome=<user>]
	release: $release
	arch: x86 or x86_64: defaults to host arch
	bindhome: bind <user>'s home into the container
	EOF
}

prog=`basename $0`
path="" ; name="" ; rootfs=""
clean=0
release=${release:-5.x}
arch=`uname -m | sed 's/i.86/x86/'` ; hostarch=$arch
bindhome=""
sopts=hp:n:cr:a:b:
lopts=help,path:,name:,rootfs:,clean,release:,arch:,bindhome:
if ! options=`getopt -o $sopts -l $lopts -- "$@"` ; then
  usage
  exit 1
fi
eval set -- "$options"
while true ; do
  case "$1" in
  -h|--help) usage && exit 0 ;;
  -p|--path) path=$2 ; shift 2 ;;
  -n|--name) name=$2 ; shift 2 ;;
  --rootfs) rootfs=$2 ; shift 2 ;;
  -c|--clean) clean=1 ; shift 1 ;;
  -r|--release) release=$2 ; shift 2 ;;
  -a|--arch) arch=$2 ; shift 2 ;;
  -b|--bindhome) bindhome=$2 ; shift 2 ;;
  --) shift 1 ; break ;;
  *) break ;;
  esac
done
if [ $clean -eq 1 -a -z "$path" ] ; then
  cleanup || exit 1
  exit 0
fi
if [ $hostarch == "x86" -a $arch == "x86_64" ] ; then
  echo "Can't create x86_64 container on x86."
  exit 1
fi
if [ -z "$path" ] ; then
  echo "'path' parameter is required."
  exit 1
fi
if [ -z "$name" ] ; then
  echo "'name' parameter is required."
  exit 1
fi
if [ `id -u` -ne 0 ] ; then
  echo "This script should be run as 'root'."
  exit 1
fi
cache=@LOCALSTATEDIR@/cache/lxc
ptcache=$cache/partial-${prog##*-}-$release-$arch
dlcache=$cache/cache-${prog##*-}-$release-$arch
rtcache=$cache/rootfs-${prog##*-}-$release-$arch
if [ -z "$rootfs" ] ; then
  if grep -q "^lxc.rootfs" $path/config ; then
    rootfs=`awk -F= '/^lxc.rootfs =/{ print $2 }' $path/config`
  else
    rootfs=$path/rootfs
  fi
fi
if ! install_plamo ; then
  echo "Failed to install plamo $release."
  exit 1
fi
if ! configure_plamo ; then
  echo "Failed to configure plamo $release for a container."
  exit 1
fi
if ! copy_configuration ; then
  echo "Failed to write configuration file."
  exit 1
fi
post_process
if [ -n "$bindhome" ] ; then
  do_bindhome
fi
if [ $clean -eq 1 ] ; then
  cleanup || exit 1
  exit 0
fi