-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
lxc-plamo.in
386 lines (360 loc) · 10.8 KB
/
lxc-plamo.in
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
#!/bin/bash
#
# 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
set -eu
[ -r /etc/default/lxc ] && . /etc/default/lxc
LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
DOWNLOAD_SCHEME=${DOWNLOAD_SCHEME:-"http"}
MIRRORSRV=${MIRRORSRV:-"ftp.ne.jp"}
MIRRORPATH=${MIRRORPATH:-"/Linux/distributions/plamolinux"}
CATEGORY[0]=${CATEGORY:-"00_base"}
IGNOREPKG[0]=${IGNOREPKGS:-"grub kernel lilo linux_firmware microcode_ctl"}
CATEGORY[1]="01_minimum"
IGNOREPKG[1]="cpufreqd cpufrequtils gpm"
CATEGORY[2]="01_minimum/alsa.txz"
IGNOREPKG[2]=""
CATEGORY[3]="01_minimum/aspell.txz"
IGNOREPKG[3]=""
CATEGORY[4]="01_minimum/devel.txz"
IGNOREPKG[4]=""
CATEGORY[5]="01_minimum/gnupg_tls.txz"
IGNOREPKG[5]=""
CATEGORY[6]="01_minimum/network.txz"
IGNOREPKG[6]=""
CATEGORY[7]="01_minimum/nfs.txz"
IGNOREPKG[7]=""
CATEGORY[8]="01_minimum/tcl.txz"
IGNOREPKG[8]=""
CATEGORY_PATH=""
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
for i in `seq 0 $((${#CATEGORY[@]} - 1))` ; do
CATEGORYPATH=${MIRRORPATH}/Plamo-${release}/${arch}/plamo/${CATEGORY[$i]}
WGETOPT="-nv -r -l1 -e robots=off -nd --no-parent -c --retr-symlinks -A .txz"
EXCLUDE_OPT=""
if [ $DOWNLOAD_SCHEME = "http" ] ; then
if [ -n "${IGNOREPKG[$i]}" ] ; then
for p in ${IGNOREPKG[$i]} ; do
EXCLUDE_OPT="${EXCLUDE_OPT} -R ${p}* "
done
fi
if ! wget ${WGETOPT} ${EXCLUDE_OPT} -X ${CATEGORY_PATH}/old http://${MIRRORSRV}${CATEGORYPATH} ; then
echo "Failed to download the rootfs, aborting."
return 1
fi
elif [ $DOWNLOAD_SCHEME = "ftp" ] ; then
if [ -n "${IGNOREPKG[$i]}" ] ; then
for p in ${IGNOREPKG[$i]} ; do
EXCLUDE_OPT="${EXCLUDE_OPT} -x ${p} "
done
fi
if ! lftp -c "open ${MIRRORSRV} && cd ${CATEGORYPATH} && mirror -i .txz -x old ${EXCLUDE_OPT} -r ." ; then
echo "Failed to download the rootfs, aborting."
return 1
fi
else
echo "Invalid DOWNLOAD_SCHEME value (can set http or ftp). "
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 200 ; 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
if [ ! -x /sbin/installpkg ] ; then
echo "'installpkg' command is missing."
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
fi
echo "Installing packages to $rtcache..."
if [ ! -d $rtcache ] ; then
mkdir -p $rtcache
for p in `ls $dlcache` ; 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
) 200> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
}
configure_plamo() {
# create /dev
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"
230,261d
156,163d
26,147d
16,22d
w
EOF
# /etc/rc.d/rc.M
ed - $rootfs/etc/rc.d/rc.M <<- "EOF"
269,270d
56,82d
31,38d
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
return 0
}
copy_configuration() {
# Create the fstab (empty by default)
touch $path/fstab
if ! cat <<- EOF >> $path/config ; then
lxc.utsname = $name
lxc.arch = $arch
lxc.mount = $path/fstab
EOF
echo "Failed to add configuration."
return 1
fi
if [ -e "${LXC_TEMPLATE_CONFIG}/plamo.common.conf" ] ; then
echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/plamo.common.conf" >> $path/config
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 200 ; 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
) 200> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
}
usage() {
cat <<- EOF
$prog [-h|--help] -p|--path=<path> -n|--name=<name> --rootfs=<rootfs>
[--clean] [-r|--release=<release>] [-b|--bindhome=<user>]
[-a|--arch=<arch>]
release: $release
bindhome: bind <user>'s home into the container
arch: x86 or x86_64: defaults to host arch
EOF
}
prog=`basename $0`
path="" ; name="" ; rootfs=""
clean=0
release=${release:-5.x}
bindhome=""
arch=`uname -m | sed 's/i.86/x86/'` ; hostarch=$arch
sopts=hp:n:cr:b:a:
lopts=help,path:,name:,rootfs:,clean,release:,bindhome:,arch:
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 ;;
-b|--bindhome) bindhome=$2 ; shift 2 ;;
-a|--arch) arch=$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