]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-gentoo.in
lxc-gentoo, fix lack of any generated locale
[mirror_lxc.git] / templates / lxc-gentoo.in
1 #!/bin/bash
2
3 #
4 # LXC template for gentoo
5 #
6 # Author: Guillaume Zitta <lxc@zitta.fr>
7 #
8 # Widely inspired from lxc-gentoo script at https://github.com/globalcitizen/lxc-gentoo
9 #
10 # this version is reworked with :
11 # - out of the lxc-create compat
12 # - vanilla gentoo config
13 # - ready to use cache
14 #
15
16 # Detect use under userns (unsupported)
17 for arg in "$@"; do
18 [ "$arg" = "--" ] && break
19 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
20 echo "This template can't be used for unprivileged containers." 1>&2
21 echo "You may want to try the \"download\" template instead." 1>&2
22 exit 1
23 fi
24 done
25
26 # Make sure the usual locations are in PATH
27 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
28
29 # Ensure strict root's umask doesen't render the VM unusable
30 umask 022
31
32 LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
33
34 ################################################################################
35 # Various helper functions
36 ################################################################################
37
38 # param: $1: the name of the lock
39 # param: $2: the timeout for the lock
40 # The rest contain the command to execute and its parameters
41 execute_exclusively()
42 {
43 mkdir -p @LOCALSTATEDIR@/lock/subsys/
44
45 local lock_name="$1"
46 local timeout="$2"
47 shift 2
48
49 {
50 printf "Attempting to obtain an exclusive lock (timeout: %s sec) named \"%s\"...\n" "${timeout}" "$lock_name"
51
52 flock -x -w "${timeout}" 50
53
54 if [[ $? -ne 0 ]]; then
55 printf " => unable to obtain lock, aborting.\n"
56 return 2
57 else
58 printf " => done.\n"
59 fi
60
61 printf " => Executing \"%s\"\n" "$*"
62 "$@"
63 retval=$?
64 } 50> "@LOCALSTATEDIR@/lock/subsys/lxc-gentoo-${lock_name}"
65 return $retval
66 }
67
68 # a die function is always a good idea
69 die()
70 {
71 printf "\n[the last exit code leading to this death was: %s ]\n" "$?"
72 local retval="$1"
73 shift 1
74 printf "$@"
75 exit "$retval"
76 }
77
78 # gentoo arch/variant detection
79 set_default_arch()
80 {
81 printf "### set_default_arch: default arch/variant autodetect...\n"
82 arch=$(arch)
83 if [[ $arch =~ i.86 ]]; then
84 arch="x86"
85 variant="x86"
86 elif [[ $arch == "x86_64" ]]; then
87 arch="amd64"
88 variant="amd64"
89 elif [[ $arch =~ arm.* ]]; then
90 arch="arm"
91 variant="armv7a"
92 else
93 #who knows, it may work...
94 printf " => warn: unexpected arch:${arch} let me knows if it works :)\n"
95 variant="${arch}"
96 fi
97 printf " => Got: arch=%s variant=%s\n" "${arch}" "${variant}"
98 }
99
100 store_user_message()
101 {
102 user_message="${user_message}=> $@\n"
103 }
104
105 ################################################################################
106 # CACHE Preparation
107 ################################################################################
108 # during setup cachedir is $cacheroot/partial-$arch-$variant
109 # at the end, it will be $cacheroot/rootfs-$arch-$variant
110
111 cache_setup(){
112 partialfs="${cacheroot}/partial-${arch}-${variant}"
113
114 #if cache exists and flush not needed, return
115 [[ -d "${cachefs}" && -z "${flush_cache}" ]] && return 0
116
117 printf "###### cache_setup(): doing cache preparation\n"
118 local retval=1
119
120 #clean from failed previous run
121 rm -rf "${partialfs}"
122 mkdir -p "${partialfs}"
123
124 #let's go
125 cache_precheck && \
126 cache_stage3 && \
127 cache_portage && \
128 cache_inittab && \
129 cache_net && \
130 cache_dev && \
131 cache_openrc && \
132 cache_locale && \
133 rm -rf "${cachefs}" && \
134 mv "${partialfs}" "${cachefs}" && \
135 printf "###### cache_setup: Cache should be ready\n"
136
137 return $?
138 }
139
140 cache_precheck()
141 {
142 printf "### cache_precheck(): doing some pre-start checks ...\n"
143 # never hurts to have a fail-safe.
144 [[ -n "${cacheroot//\/}" ]] \
145 || die 8 "\$cacheroot (%s) IS EMPTY OR MADE OF ONLY DIRECTORY SEPERATORS, THIS IS *VERY* BAD!\n" "${cacheroot}"
146 }
147
148 #get latest stage3 tarball
149 cache_stage3()
150 {
151 printf "### cache_stage3(): stage3 cache deployment...\n"
152
153 if [ -z "${tarball}" ]; then
154 #variables init
155 local stage3_baseurl="${mirror}/releases/${arch}/autobuilds"
156
157 # get latest-stage3....txt file for subpath
158 local stage3_pointer="${stage3_baseurl}/latest-stage3-${variant}.txt"
159
160 printf "Determining path to latest Gentoo %s (%s) stage3 archive...\n" "${arch}" "${variant}"
161 printf " => downloading and processing %s\n" "${stage3_pointer}"
162
163 local stage3_latest_tarball=$(wget -q -O - "${stage3_pointer}" | tail -n1 ) \
164 || die 6 "Error: unable to fetch\n"
165
166 printf " => Got: %s\n" "${stage3_latest_tarball}"
167
168 printf "Downloading/untarring the actual stage3 tarball...\n"
169 wget -O - "${stage3_baseurl}/${stage3_latest_tarball}" | tar -xjpf - -C "${partialfs}" \
170 || die 6 "Error: unable to fetch or untar\n"
171 printf " => extracted to: %s\n" "${partialfs}"
172 else
173 printf "Extracting the stage3 tarball...\n"
174 tar -xpf "${tarball}" -C "${partialfs}" || die 6 "unable to untar ${tarball} to ${partialfs}"
175 fi
176
177 #check if it chroots
178 printf "chroot test..."
179 chroot ${partialfs} /bin/true || die 1 "Error: chroot %s /bin/true, failed" "${partialfs}"
180 printf " OK\n"
181 printf " => stage3 cache extracted in : %s\n" "${partialfs}"
182 return 0
183 }
184
185 cache_portage()
186 {
187 printf "### cache_portage: caching portage tree tarball...\n"
188 [[ -z "${flush_cache}" && -f "${portage_cache}" ]] && return 0
189
190 rm -f ${portage_cache}
191
192 printf "Downloading Gentoo portage (software build database) snapshot...\n"
193 execute_exclusively portage 60 wget -O "${portage_cache}" "${mirror}/snapshots/portage-latest.tar.bz2" \
194 || die 6 "Error: unable to fetch\n"
195 printf " => done.\n"
196 }
197
198 # custom inittab
199 cache_inittab()
200 {
201 printf "### cache_inittab: tuning inittab...\n"
202
203 INITTAB="${partialfs}/etc/inittab"
204
205 [[ -w "$INITTAB" ]] || die 1 "Error: $INITTAB is not writeable"
206
207 # create console
208 echo "# Lxc main console" >> "$INITTAB"
209 echo "1:12345:respawn:/sbin/agetty -a root --noclear 115200 console linux" >> "$INITTAB"
210
211 # finally we add a pf line to enable clean shutdown on SIGPWR (issue 60)
212 echo "# clean container shutdown on SIGPWR" >> "$INITTAB"
213 echo "pf:12345:powerwait:/sbin/halt" >> "$INITTAB"
214
215 # we also blank out /etc/issue here in order to prevent delays spawning login
216 # caused by attempts to determine domainname on disconnected containers
217 sed -i 's/[\][Oo]//g' "${partialfs}/etc/issue"
218 }
219
220 cache_net()
221 {
222 printf "### cache_net: doing some useful net tuning...\n"
223 # useful for chroot
224 # /etc/resolv.conf
225 grep -i 'search ' /etc/resolv.conf > "${partialfs}/etc/resolv.conf"
226 grep -i 'nameserver ' /etc/resolv.conf >> "${partialfs}/etc/resolv.conf"
227
228 # fix boot-time interface config wipe under aggressive cap drop
229 # (openrc 0.9.8.4 ~sep 2012 - https://bugs.gentoo.org/show_bug.cgi?id=436266)
230 # initial warkaround was: sed -i -e 's/^#rc_nostop=""/rc_nostop="net.eth0 net.lo"/' "${partialfs}/etc/rc.conf"
231 # but this one does not depends on interfaces names
232 echo 'rc_keyword="-stop"' >> "${partialfs}/etc/conf.d/net"
233 }
234
235 cache_dev()
236 {
237 printf "### cache_dev(): /dev tuning...\n"
238
239 #Wait for https://bugs.gentoo.org/show_bug.cgi?id=496054
240 mkdir "${partialfs}/dev/pts"
241 mkdir "${partialfs}/dev/shm"
242 mkdir "${partialfs}/dev/mqueue"
243
244 mkdir -m 755 "${partialfs}/dev/net"
245 mknod -m 666 "${partialfs}/dev/net/tun" c 10 200
246
247 return 0
248 }
249
250 # fix openrc system
251 cache_openrc()
252 {
253 printf "### cache_openrc(): doing openrc tuning\n"
254
255 #Wait for https://bugs.gentoo.org/show_bug.cgi?id=496054
256 chroot "${partialfs}" sed s/-lxc//g -i "/etc/init.d/devfs"
257
258 return 0
259 }
260
261 cache_locale()
262 {
263 printf "### cache_locale(): initiating minimale locale en_US.UTF-8 \n"
264
265 echo "en_US.UTF-8 UTF-8" >> "${partialfs}/etc/locale.gen"
266 chroot "${partialfs}" locale-gen
267
268 return 0
269 }
270
271 ################################################################################
272 # CONTAINER Preparation
273 ################################################################################
274
275 container_setup() {
276 printf "##### container_setup(): starting container setup\n"
277
278 #in most cases lxc-create should have provided a copy of default lxc.conf
279 #let's tag where template starts, or just create the files
280 echo '### lxc-gentoo template stuff starts here' >> "$path/config"
281
282 #Determine rootfs
283 #If backingstore was specified, lxc.rootfs should be present or --rootfs did the rootfs var creation
284 if [ -z "${rootfs}" ]; then
285 rootfs=`awk -F= '$1 ~ /^lxc.rootfs/ { print $2 }' "$path/config" 2>/dev/null`
286 if [ -z "${rootfs}" ]; then
287 #OK it's default
288 rootfs="${path}/rootfs"
289 fi
290 fi
291 store_user_message "rootfs of container is : ${rootfs}"
292 store_user_message "config of container is : ${path}/config"
293
294 container_precheck && \
295 container_rootfs && \
296 container_consoles && \
297 container_tz && \
298 container_portage && \
299 container_net && \
300 container_hostname && \
301 container_auth && \
302 container_conf
303 if [ $? -ne 0 ]; then
304 die 1 "container_setup(): one step didn't complete, sorry\n"
305 fi
306
307 printf "###### container_setup(): container should be ready to start!\n"
308 printf "\n\n"
309 printf "You could now use you container with: lxc-start -n %s\n" "${name}"
310 printf "little things you should know about your container:\n"
311 printf "${user_message}"
312 return 0
313 }
314
315 container_precheck()
316 {
317 printf "### container_precheck(): doing some pre-start checks ...\n"
318 # never hurts to have a fail-safe.
319 [[ -n "${name//\/}" ]] \
320 || die 8 "\$name (%s) IS EMPTY OR MADE OF ONLY DIRECTORY SEPERATORS, THIS IS *VERY* BAD!\n" "${name}"
321
322 [[ -n "${rootfs//\/}" ]] \
323 || die 8 "\$rootfs (%s) IS EMPTY OR MADE OF ONLY DIRECTORY SEPERATORS, THIS IS *VERY* BAD!\n" "${rootfs}"
324
325 [[ -n "${cachefs//\/}" ]] \
326 || die 8 "\$cachefs (%s) IS EMPTY OR MADE OF ONLY DIRECTORY SEPERATORS, THIS IS *VERY* BAD!\n" "${cachefs}"
327
328 # check if the rootfs already exists
329 [[ -d "${rootfs}/etc" ]] && die 18 "Error: \$rootfs (%s) already exists!" "${rootfs}"
330
331 # check cache
332 [[ ! -d "${cachefs}/etc" ]] && die 1 "Error: \$cachefs (%s) not found!" "${cachefs}"
333
334 return 0
335 }
336
337 container_rootfs()
338 {
339 printf "#### container_rootfs(): copying rootfs %s from cache %s ...\n" "${rootfs}" "${cachefs}"
340 tar -c -f - -C "${cachefs}" . | tar -x -p -f - -C "${rootfs}" || die 1 "Error: cache copy to rootfs failed"
341
342 printf "chroot test..."
343 chroot "${rootfs}" /bin/true || die 1 "Error: 'chroot %s /bin/true' failed"
344 printf " OK\n"
345
346 printf " => done\n"
347 return 0
348 }
349
350 container_consoles() {
351 printf "#### container_consoles(): setting container consoles ...\n"
352
353 # disable unwanted ttys
354 if [[ ${tty} < 6 ]]; then
355 local mindis=$(( ${tty} + 1 ))
356 sed -i "s/^c[${mindis}-6]/#&/" "${rootfs}/etc/inittab"
357 fi
358 printf " => main console + ${tty} ttys\n"
359
360 if [[ -z "${autologin}" ]]; then
361 sed 's/agetty -a root/agetty/' -i "${rootfs}/etc/inittab"
362 elif [[ "${user}" != "root" ]]; then
363 sed "s/agetty -a root/agetty -a ${user}/" -i "${rootfs}/etc/inittab"
364 printf " => Autologin on main console for %s enabled\n" "${user}"
365 [[ -z "${forced_password}" ]] && unset password
366 store_user_message "${user} has autologin on main console"
367 else
368 printf " => Autologin on main console for root enabled\n"
369 [[ -z "${forced_password}" ]] && unset password
370 store_user_message "${user} has autologin on main console"
371 fi
372 printf " => done\n"
373 }
374
375 container_tz()
376 {
377 printf "#### container_tz(): setting container timezone ...\n"
378
379 #let's try to copy it from host
380 if [ -L "/etc/localtime" ]; then
381 #host has a symlink
382 #let see if we can reproduct symlink
383 target=$(readlink /etc/localtime)
384 if [[ "$target" != "" ]]; then
385 if [ -f "${rootfs}/${target}" ]; then
386 #same target exists in container
387 chroot "${rootfs}" ln -sf "${target}" "/etc/localtime"
388 printf " => host symlink reproducted in container : %s\n" "${target}"
389 store_user_message "timezone copyed from host"
390 return 0
391 fi
392 fi
393 fi
394
395 if [ -e /etc/localtime ]; then
396 # duplicate host timezone
397 cat /etc/localtime > "${rootfs}/etc/localtime"
398 printf " => host localtime copyed to container\n"
399 store_user_message "timezone was staticly copyed from host"
400 else
401 # otherwise set up UTC
402 chroot "${rootfs}" ln -sf /usr/share/zoneinfo/UTC /etc/localtime
403 printf " => fallback: fixed to UTC\n"
404 store_user_message "timezone was fixed to UTC"
405 fi
406 }
407
408
409 container_portage()
410 {
411 printf "#### container_portage(): setting container portage... \n"
412
413 #default entry for conf
414 portage_mount="#container set with private portage tree, no mount here"
415
416 printf "Warnings are normal here, don't worry\n"
417 #container repos detection
418 if chroot ${rootfs} portageq get_repo_path / gentoo > /dev/null ; then
419 portage_container="$(chroot ${rootfs} portageq get_repo_path / gentoo)"
420 else
421 die 1 "Failed to figure out container portage tree location with portageq get_repo_path / gentoo\n"
422 fi
423
424 if [[ -n "${private_portage}" ]]; then
425 container_private_portage
426 return 0
427 fi
428
429 if [ -z "${portage_dir}" ]; then
430 #gentoo host detection
431 printf "trying to guess portage_dir from host...\n"
432 portage_dir="$(portageq get_repo_path / gentoo 2>/dev/null)"
433 if [ ! -d "${portage_dir}/profiles" ]; then
434 printf " => host portage detection failed (not gentoo host), fallback to private portage tree\n"
435 container_private_portage
436 return 0
437 fi
438 else
439 if [ ! -d "${portage_dir}/profiles" ]; then
440 die 1 "specified portage_dir (%s) does not contains profiles, is it a portage tree ?\n" "${portage_dir}"
441 fi
442 fi
443
444 # if we are here, we have shared portage_dir
445 #ensure dir exists
446 chroot "${rootfs}" mkdir ${portage_container}
447 portage_mount="#container set with shared portage
448 lxc.mount.entry=${portage_dir} ${portage_container/\//} none ro,bind 0 0
449 lxc.mount.entry=${portage_dir}/distfiles ${portage_container/\//}/distfiles none rw,bind 0 0
450 #If you use eix, you should uncomment this
451 #lxc.mount.entry=/var/cache/eix var/cache/eix none ro,bind 0 0"
452 store_user_message "container has a shared portage from host's ${portage_dir} to ${portage_container/\//}"
453 #Let's propose binary packages
454 cat <<- EOF >> "${rootfs}/etc/portage/make.conf"
455 # enable this to store built binary packages
456 #FEATURES="\$FEATURES buildpkg"
457
458 # enable this to use built binary packages
459 #EMERGE_DEFAULT_OPTS="\${EMERGE_DEFAULT_OPTS} --usepkg"
460
461 # enable and *tune* this kind of entry to slot binaries, specialy if you use multiples archs and variants
462 #PKGDIR="\${PKGDIR}/amd64
463 #or PKGDIR="\${PKGDIR}/hardened"
464 EOF
465 printf " => portage stuff done, see /etc/portage/make.conf for additionnal tricks\n"
466
467 }
468
469 container_private_portage()
470 {
471 #called from container_portage() do not call directly from container_setup
472 printf "# untaring private portage to %s from %s ... \n" "${rootfs}/${portage_container}" "${portage_cache}"
473 mkdir -p "${rootfs}/${portage_container}"
474 execute_exclusively portage 60 tar -xp --strip-components 1 -C "${rootfs}/${portage_container}" -f "${portage_cache}" \
475 || die 2 "Error: unable to extract the portage tree.\n"
476 store_user_message "container has its own portage tree at ${portage_container}"
477 printf "=> done\n"
478 }
479
480 #helper func for container_genconf_net()
481 nic_write()
482 {
483 #display with gentoo's confd.net format
484 echo "config_${nic_name}=\"${nic_conf}\""
485 #add to managed list
486 [[ "${nic_conf}" == "dhcp" ]] && nic_managed="${nic_managed} ${nic_name}"
487 [[ "${nic_conf}" == "null" ]] && nic_unmanaged="${nic_unmanaged} ${nic_name}"
488 [[ -z "${nic_hwaddr}" && ${nic_type} == "veth" ]] && nic_wo_hwaddr="${nic_wo_hwaddr} ${nic_name}"
489 nic_writed=1
490 }
491
492 #Analyse lxc.conf and print conf.d/net content
493 container_conf_net()
494 {
495 local file=${1}
496 [[ -z "${nic_last}" ]] && nic_last=-1
497 [[ -z "${nic_named}" ]] && nic_named=0
498 OLDIFS=$IFS
499 IFS="
500 "
501 #let's do some drity bash things to parse lxc network conf
502 for line in $( sed -r "s/[ ]*=[ ]*/_real_ugly_sep_42_/" "${file}" ); do
503 key=$(echo "${line}" | sed 's/_real_ugly_sep_42_.*$//')
504 value=$(echo "${line}" | sed 's/^.*_real_ugly_sep_42_//')
505
506 #new nic !
507 if [[ "${key}" == "lxc.network.type" ]]; then
508 #we don't know what to do with it.
509 [[ "${value}" == "empty" ]] && continue
510
511 #write conf from previous loops
512 [[ "${nic_writed}" == "0" ]] && nic_write
513
514 #init defaults
515 let nic_last=nic_last+1
516
517 nic_writed=0
518 #if 1 named between 2 not named: last is eth1
519 #=> Number is ID munis number of named NIC before
520 nic_name="eth$(( ${nic_last} - ${nic_named} ))"
521 nic_conf="dhcp"
522 nic_type="${value}"
523 fi
524
525 if [[ "${key}" == "lxc.network.hwaddr" ]]; then
526 nic_hwaddr=1
527 fi
528
529 if [[ "${key}" =~ ^lxc.network.ipv(4|6) ]]; then
530 #tell openrc to not manage this NIC as LXC set there address
531 nic_conf="null"
532 fi
533 if [[ "${key}" =~ ^lxc.network.name ]]; then
534 nic_name="${value}"
535 let nic_named=nic_named+1
536 fi
537 if [[ "${key}" == "lxc.include" ]]; then
538 #recursive into include
539 container_conf_net "${value}"
540 fi
541 done
542 #write conf from previous loops
543 [[ "${nic_writed}" == "0" ]] && nic_write
544 IFS=$OLDIFS
545 }
546
547 container_net()
548 {
549 printf "container_net(): setting container network conf... \n"
550
551 #Analyse network configuration in config
552 container_conf_net "$path/config" >> "${rootfs}/etc/conf.d/net"
553
554 # found how much nic finaly have
555 nic_count=$(( ${nic_last} + 1 ))
556
557 # unless openrc manage a nic, we now have to force openrc to automatic
558 # provision of the 'net' dep. If we do not, network dependent services
559 # will fail to load
560 if [[ -z "${nic_managed}" ]]; then
561 #tell openrc that lxc already did the work
562 echo 'rc_provide="net"' >> "${rootfs}/etc/rc.conf"
563 fi
564
565 #No NIC ?
566 if [[ ${nic_count} == 0 ]]; then
567 #If no Nic, no need to continue
568 bridge=$(brctl show | awk 'NR==2 {print $1}')
569 if [[ "${bridge}" != "" ]]; then
570 store_user_message "No network interface for this container
571 It's a pitty, you have bridge, ${bridge}.
572 If it is for Lxc, use it next time by adding this to your default.conf :
573 lxc.network.type = veth
574 lxc.network.link = ${bridge}
575 lxc.network.flags = up
576 lxc.network.hwaddr = fe:xx:xx:xx:xx:xx"
577 return 0
578 else
579 store_user_message "No network interface for this container"
580 return 0
581 fi
582 fi
583
584 #For each openrc managed nic, activate
585 sys_nic_index=1
586 for nic in ${nic_managed}
587 do
588 chroot "${rootfs}" ln -s net.lo "/etc/init.d/net.${nic}"
589 chroot "${rootfs}" rc-update add net.${nic} default
590 #fake sysfs for openrc, in case settings does not provide it
591 mkdir -p "${rootfs}/sys/class/net/${nic}"
592 echo ${sys_nic_index} > "${rootfs}/sys/class/net/${nic}/ifindex"
593 echo up > "${rootfs}/sys/class/net/${nic}/operstate"
594 let sys_nic_index=sys_nic_index+1
595 done
596
597 #Warn about dynamic hwaddr
598 if [[ -n "${nic_wo_hwaddr}" ]]; then
599 store_user_message "Warning, these veth NIC don't have fixed hwaddr :
600 ${nic_wo_hwaddr}
601
602 see http://lists.linuxcontainers.org/pipermail/lxc-devel/2013-December/006736.html
603 and man lxc.conf"
604 fi
605
606 printf " => network conf done.\n"
607 }
608
609 # custom hostname
610 container_hostname()
611 {
612 printf "#### container_hostname(): setting hostname... \n"
613 printf "hostnale=%s\n" "${name}" > "${rootfs}/etc/conf.d/hostname"
614 printf " => done.\n"
615 }
616
617 container_auth()
618 {
619 printf "#### container_auth(): setting authentification... \n"
620 if [[ "${user}" != "root" ]]; then
621 printf " non root user requested, creating... \n"
622 chroot "${rootfs}" useradd --create-home -s /bin/bash "${user}" || die 1 "failed to create user ${user}"
623 printf " => user %s created\n" "${user}"
624 fi
625 store_user_message "Connection user is ${user}"
626 #Home of user
627 auth_home=$(chroot "${rootfs}" getent passwd "${user}" | cut -d : -f 6)
628 if [[ -r "${auth_key}" ]]; then
629 printf " deploying auth_key %s for user %s ...\n" "${auth_key}" "${user}"
630 mkdir -p "${rootfs}/${auth_home}/.ssh"
631 cat >> "${rootfs}/${auth_home}/.ssh/authorized_keys"
632 chroot "${rootfs}" chown "${user}:" "${auth_home}/.ssh/authorized_keys"
633 printf " => inserted public key in %s/.ssh/authorized_keys\n" "${auth_home}"
634 [[ -z "${forced_password}" ]] && unset password
635 store_user_message "${user} has the ssh key you gived us"
636 fi
637
638 if [[ -n "${password}" ]]; then
639 printf " setting password for %s ...\n" "${user}"
640 echo "${user}:${password}" | chroot "${rootfs}" chpasswd || die 1 "failed to change password"
641 printf " => done. if you didn't specify , default is 'toor'\n"
642 if [[ -n "${forced_password}" ]]; then
643 store_user_message "${user} has the password you give for him"
644 else
645 store_user_message "${user} has the default password 'toor', please change it ASAP"
646 fi
647 fi
648
649 printf " => done.\n"
650 }
651
652 ################################################################################
653 # lxc configuration files
654 ################################################################################
655
656 container_conf()
657 {
658 printf "container_configuration(): making lxc configuration file... \n"
659
660 #at this point if there
661 conf_file="${path}/config"
662
663 if grep -q "^lxc.rootfs" "${conf_file}" ; then
664 #lxc-create already provided one
665 conf_rootfs_line=""
666 else
667 conf_rootfs_line="lxc.rootfs = $(readlink -f "${rootfs}")"
668 fi
669 if [[ "${arch}" == "x86" || "${arch}" == "amd64" ]]; then
670 local conf_arch_line="lxc.arch = ${arch}"
671 else
672 local conf_arch_line="# lxc.arch = ${arch}"
673 fi
674
675 cat <<- EOF >> "${conf_file}"
676 # sets container architecture
677 # If desired architecture != amd64 or x86, then we leave it unset as
678 # LXC does not oficially support anything other than x86 or amd64.
679 ${conf_arch_line}
680
681 # set the hostname
682 lxc.utsname = ${name}
683 lxc.tty = ${tty}
684
685 ${conf_rootfs_line}
686 ${portage_mount}
687 ${conf_sysfs}
688 ${conf_mounts}
689
690 lxc.include = ${LXC_TEMPLATE_CONFIG}/gentoo.${settings}.conf
691 EOF
692 printf " => done.\n"
693 }
694
695 usage()
696 {
697 cat <<EOF
698 $1 -h|--help [-a|--arch <arch>] [-v|--variant <variant>] [-P|--private-portage] [--portage-dir <protagedir>] [-t|--tarball <stage3file>]
699 [-F|--flush-cache] [-c|--cache-only] [-u|--user <username>] [-w|--password <password>] [--autologin] [-S|--auth-key <keyfile>]
700 [-s|--settings <name>] [-m|--mirror <gentoomirror>] [--tty <number>]
701
702 arch: the container architecture (e.g. amd64): defaults to host arch (currently: '${arch}')
703 If you choose one that needs emulation
704 tested: amd64, x86
705 You could try any other gentoo arch, why not...
706
707 variant: gentoo's Architecture variant as of dec 2013 : (currently: '${variant}')
708 for amd64 arch: amd64 (default), amd64-hardened+nomultilib, amd64-hardened, amd64-nomultilib, x32
709 for x86 arch: i686 (default), i486, i686-hardened
710 for arm arch: armv7a (default), armv7a_hardfp, armv6j, armv6j_hardfp, armv5tel, armv4tl
711
712 private-portage: by default, /usr/portage is mount-binded with host one if exists (currently: '${private_portage}')
713 this force container to have his own copy
714
715 portage-dir: portage dir used for shared portage
716 by default the host on if any (currently: '${portage_dir}')
717
718 tarball: force usage of local stage3 archive (currently: '${arch}')
719 If empty, latest will be downloaded
720
721 flush-cache: do like there is no previous cache
722
723 cache-only: just ensure cache is present
724 if cache exists and "flush-cache" not specified, does nothing
725
726 user: user used in auth oriented options (currently: '${user}')
727
728 password: password for user (currently: '${password}')
729 if default, usage of auth-key will disable password setting
730
731 autologin: enable autologin for user (currently: '${autologin}')
732 This unset default password setting
733
734 auth-key: SSH Public key file to inject into container for user (currently: '${auth_key}')
735 This unset default password setting
736
737 settings: choose common configuration (currently: '${settings}')
738 see ${LXC_TEMPLATE_CONFIG}/gentoo.*.conf
739 Available settings:
740 $(ls -1 ${LXC_TEMPLATE_CONFIG}/gentoo.*.conf | xargs basename -a -s .conf | sed 's/^gentoo.//')
741
742 mirror: gentoo mirror for download (currently: '${mirror}')
743
744 tty: number of tty (6 max) (currently: '${tty}')
745 EOF
746 exit 0
747 }
748
749 #some overridable defaults
750 set_default_arch
751
752 mirror="http://distfiles.gentoo.org"
753 user="root"
754 password="toor"
755 tty=1
756 settings="common"
757 options=$(getopt -o hp:n:a:FcPv:t:S:u:w:s:m: -l help,rootfs:,path:,name:,arch:,flush-cache,cache-only,private-portage,variant:,portage-dir:,tarball:,auth_key:,user:,autologin,password:,settings:,mirror:,tty: -- "$@")
758
759 eval set -- "$options"
760
761 while true
762 do
763 case "$1" in
764 -h|--help) usage $0 && exit 0;;
765 --rootfs) rootfs=$2; shift 2;;
766 -p|--path) path=$2; shift 2;;
767 -n|--name) name=$2; shift 2;;
768 -a|--arch) arch=$2; shift 2;;
769 -F|--flush-cache) flush_cache=1; shift 1;;
770 -c|--cache-only) cache_only=1; shitf 1;;
771 -P|--private-portage) private_portage=1; shift 1;;
772 -v|--variant) variant=$2; shift 2;;
773 --portage-dir) portage_dir=$2; shift 2;;
774 -t|--tarball) tarball=$2; shift 2;;
775 -S|--auth-key) auth_key=$2; shift 2;;
776 -u|--user) user=$2; shift 2;;
777 -w|--password) forced_password=1; password=$2; shift 2;;
778 -s|--settings) settings=$2; shift 2;;
779 -m|--mirror) mirror=$2; shift 2;;
780 --tty) [[ $2 -lt 6 ]] && tty=$2; shift 2;;
781 --autologin) autologin=1; shift 1;;
782 --) shift 1; break ;;
783 *) break ;;
784 esac
785 done
786
787 cacheroot="@LOCALSTATEDIR@/cache/lxc/gentoo"
788 portage_cache="${cacheroot}/portage.tbz"
789 cachefs="${cacheroot}/rootfs-${arch}-${variant}"
790
791 alias wget="wget --timeout=8 --read-timeout=15 -c -t10 -nd"
792
793 do_all() {
794 cache_setup
795 if [ -z "${cache_only}" ]; then
796 container_setup
797 fi
798 }
799
800 execute_exclusively "cache-${arch}-${variant}" 60 do_all