Debian Codegraph @

templates/lxc-gentoo.in

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