1 diff --git a/templates/lxc-fedora.in b/templates/lxc-fedora.in
2 index 684bb9c..922563d 100644
3 --- a/templates/lxc-fedora.in
4 +++ b/templates/lxc-fedora.in
6 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
11 cache_base=@LOCALSTATEDIR@/cache/lxc/fedora/$arch
12 default_path=@LXCPATH@
16 -[ -f /etc/fedora-release ] && is_fedora=true
17 +# Alow for weird remixes like the Raspberry Pi
19 +# Use the Mitre standard CPE identifier for the release ID if possible...
20 +# This may be in /etc/os-release or /etc/system-release-cpe. We
21 +# should be able to use EITHER. Give preference to /etc/os-release for now.
23 +if [ -e /etc/os-release ]
25 +# This is a shell friendly configuration file. We can just source it.
26 +# What we're looking for in here is the ID, VERSION_ID and the CPE_NAME
28 + echo "Host CPE ID from /etc/os-release: ${CPE_NAME}"
31 +if [ "${CPE_NAME}" = "" -a -e /etc/system-release-cpe ]
33 + CPE_NAME=$(head -n1 /etc/system-release-cpe)
34 + CPE_URI=$(expr ${CPE_NAME} : '\([^:]*:[^:*]\)')
35 + if [ "${CPE_URI}" != "cpe:/o" ]
39 + echo "Host CPE ID from /etc/system-release-cpe: ${CPE_NAME}"
40 + # Probably a better way to do this but sill remain posix
41 + # compatible but this works, shrug...
42 + # Must be nice and not introduce convenient bashisms here.
43 + ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:[^:]*:\([^:]*\)')
44 + VERSION_ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\)')
48 -if [ "$arch" = "i686" ]; then
50 +if [ "${CPE_NAME}" != "" -a "${ID}" = "fedora" -a "${VERSION_ID}" != "" ]
52 + fedora_host_ver=${VERSION_ID}
54 +elif [ -e /etc/redhat-release ]
56 + # Only if all other methods fail, try to parse the redhat-release file.
57 + fedora_host_ver=$( sed -e '/^Fedora /!d' -e 's/Fedora.*\srelease\s*\([0-9][0-9]*\)\s.*/\1/' < /etc/redhat-release )
58 + if [ "$fedora_host_ver" != "" ]
64 +# Map a few architectures to their generic Fedora repository archs.
65 +# The two ARM archs are a bit of a guesstimate for the v5 and v6
66 +# archs. V6 should have hardware floating point (Rasberry Pi).
67 +# The "arm" arch is safer (no hardware floating point). So
68 +# there may be cases where we "get it wrong" for some v6 other
72 +armv3l|armv4l|armv5l) arch=arm ;;
73 +armv6l|armv7l|armv8l) arch=armhfp ;;
79 @@ -50,7 +101,7 @@ configure_fedora()
88 @@ -59,12 +110,18 @@ EOF
90 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
96 + # set hostname on systemd Fedora systems
97 + if [ $release -gt 14 ]; then
98 + echo "${utsname}" > ${rootfs_path}/etc/hostname
102 cat <<EOF > $rootfs_path/etc/hosts
103 -127.0.0.1 localhost $name
104 +127.0.0.1 localhost.localdomain localhost $utsname
105 +::1 localhost6.localdomain6 localhost6
108 dev_path="${rootfs_path}/dev"
109 @@ -118,10 +175,27 @@ configure_fedora_systemd()
111 unlink ${rootfs_path}/etc/systemd/system/default.target
112 touch ${rootfs_path}/etc/fstab
113 - chroot ${rootfs_path} ln -s /dev/null //etc/systemd/system/udev.service
114 + chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/udev.service
115 chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
116 #dependency on a device unit fails it specially that we disabled udev
117 - sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
118 + # sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
120 + # Actually, the After=dev-%i.device line does not appear in the
121 + # Fedora 17 or Fedora 18 systemd getty\@.service file. It may be left
122 + # over from an earlier version and it's not doing any harm. We do need
123 + # to disable the "ConditionalPathExists=/dev/tty0" line or no gettys are
124 + # started on the ttys in the container. Lets do it in an override copy of
125 + # the service so it can still pass rpm verifies and not be automatically
126 + # updated by a new systemd version. -- mhw /\/\|=mhw=|\/\/
128 + sed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \
129 + -e 's/After=dev-%i.device/After=/' \
130 + < ${rootfs_path}/lib/systemd/system/getty\@.service \
131 + > ${rootfs_path}/etc/systemd/system/getty\@.service
132 + # Setup getty service on the 4 ttys we are going to allow in the
133 + # default config. Number should match lxc.tty
134 + ( cd ${rootfs_path}/etc/systemd/system/getty.target.wants
135 + for i in 1 2 3 4 ; do ln -sf ../getty\@.service getty@tty${i}.service; done )
139 @@ -142,27 +216,53 @@ download_fedora()
140 MIRRORLIST_URL="http://mirrors.fedoraproject.org/mirrorlist?repo=fedora-$release&arch=$arch"
143 - for trynumber in 1 2 3; do
145 + # We're splitting the old loop into two loops plus a directory retrival.
146 + # First loop... Try and retrive a mirror list with retries and a slight
147 + # delay between attempts...
148 + for trynumber in 1 2 3 4; do
149 [ $trynumber != 1 ] && echo "Trying again..."
150 - MIRROR_URL=$(curl -s -S -f "$MIRRORLIST_URL" | head -n2 | tail -n1)
151 - if [ $? -ne 0 ] || [ -z "$MIRROR_URL" ]; then
152 - echo "Failed to get a mirror"
155 + # This code is mildly "brittle" in that it assumes a certain
156 + # page format and parsing HTML. I've done worse. :-P
157 + MIRROR_URLS=$(curl -s -S -f "$MIRRORLIST_URL" | sed -e '/^http:/!d' -e '2,6!d')
158 + if [ $? -eq 0 ] && [ -n "$MIRROR_URLS" ]
163 + echo "Failed to get a mirror on try $trynumber"
167 + # This will fall through if we didn't get any URLS above
168 + for MIRROR_URL in ${MIRROR_URLS}
170 if [ "$release" -gt "16" ]; then
171 - RELEASE_URL="$MIRROR_URL/Packages/f/fedora-release-$release-1.noarch.rpm"
172 + RELEASE_URL="$MIRROR_URL/Packages/f"
174 - RELEASE_URL="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
175 + RELEASE_URL="$MIRROR_URL/Packages/"
177 - echo "Fetching from $RELEASE_URL"
178 - curl -f "$RELEASE_URL" > $INSTALL_ROOT/fedora-release-$release.noarch.rpm
180 + echo "Fetching rpm name from $RELEASE_URL..."
181 + # This code is mildly "brittle" in that it assumes a certain directory
182 + # page format and parsing HTML. I've done worse. :-P
183 + RELEASE_RPM=$(curl -L -f "$RELEASE_URL" | sed -e "/fedora-release-${release}-/!d" -e 's/.*<a href=\"//' -e 's/\">.*//' )
184 + if [ $? -ne 0 -o "${RELEASE_RPM}" = "" ]; then
185 + echo "Failed to identify fedora release rpm."
189 + echo "Fetching fedora release rpm from ${RELEASE_URL}/${RELEASE_RPM}......"
190 + curl -L -f "${RELEASE_URL}/${RELEASE_RPM}" > ${INSTALL_ROOT}/${RELEASE_RPM}
191 if [ $? -ne 0 ]; then
192 - echo "Failed to download fedora release rpm"
193 + echo "Failed to download fedora release rpm ${RELEASE_RPM}."
201 if [ $DOWNLOAD_OK != yes ]; then
204 @@ -170,7 +270,7 @@ download_fedora()
206 mkdir -p $INSTALL_ROOT/var/lib/rpm
207 rpm --root $INSTALL_ROOT --initdb
208 - rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm
209 + rpm --root $INSTALL_ROOT -ivh ${INSTALL_ROOT}/${RELEASE_RPM}
210 $YUM install $PKG_LIST
212 if [ $? -ne 0 ]; then
213 @@ -248,10 +348,13 @@ copy_configuration()
214 mkdir -p $config_path
215 grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "lxc.rootfs = $rootfs_path" >> $config_path/config
216 cat <<EOF >> $config_path/config
218 +lxc.utsname = $utsname
221 lxc.mount = $config_path/fstab
222 +lxc.cap.drop = sys_module mac_admin mac_override sys_time
224 +lxc.autodev = $auto_dev
226 # When using LXC with apparmor, uncomment the next line to run unconfined:
227 #lxc.aa_profile = unconfined
228 @@ -272,7 +375,7 @@ lxc.cgroup.devices.allow = c 1:8 rwm
229 lxc.cgroup.devices.allow = c 136:* rwm
230 lxc.cgroup.devices.allow = c 5:2 rwm
232 -lxc.cgroup.devices.allow = c 254:0 rwm
233 +lxc.cgroup.devices.allow = c 254:0 rm
236 cat <<EOF > $config_path/fstab
237 @@ -313,21 +416,23 @@ usage()
240 $1 -n|--name=<container_name>
241 - [-p|--path=<path>] [-c|--clean] [-R|--release=<Fedora_release>] [-A|--arch=<arch of the container>]
242 + [-p|--path=<path>] [-c|--clean] [-R|--release=<Fedora_release>] [--fqdn=<network name of container>] [-A|--arch=<arch of the container>]
245 -n,--name container name, used to as an identifier for that container from now on
247 - -p,--path path to where the container rootfs will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
248 + -p,--path path to where the container will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
249 + --rootfs path for actual rootfs.
250 -c,--clean clean the cache
251 - -R,--release Fedora release for the new container. if the host is Fedora, then it will defaultto the host's release.
252 + -R,--release Fedora release for the new container. if the host is Fedora, then it will default to the host's release.
253 + --fqdn fully qualified domain name (FQDN) for DNS and system naming
254 -A,--arch NOT USED YET. Define what arch the container will be [i686,x86_64]
255 -h,--help print this help
260 -options=$(getopt -o hp:n:cR: -l help,path:,name:,clean,release: -- "$@")
261 +options=$(getopt -o hp:n:cR: -l help,path:,rootfs:,name:,clean,release:,fqdn: -- "$@")
262 if [ $? -ne 0 ]; then
265 @@ -339,9 +444,11 @@ do
267 -h|--help) usage $0 && exit 0;;
268 -p|--path) path=$2; shift 2;;
269 + --rootfs) rootfs=$2; shift 2;;
270 -n|--name) name=$2; shift 2;;
271 -c|--clean) clean=$2; shift 2;;
272 -R|--release) release=$2; shift 2;;
273 + --fqdn) utsname=$2; shift 2;;
274 --) shift 1; break ;;
277 @@ -352,6 +459,29 @@ if [ ! -z "$clean" -a -z "$path" ]; then
281 +if [ -z "${utsname}" ]; then
285 +# This follows a standard "resolver" convention that an FQDN must have
286 +# at least two dots or it is considered a local relative host name.
287 +# If it doesn't, append the dns domain name of the host system.
289 +# This changes one significant behavior when running
290 +# "lxc_create -n Container_Name" without using the
294 +# utsname and hostname = Container_Name
296 +# utsname and hostname = Container_Name.Domain_Name
298 +if [ $(expr "$utsname" : '.*\..*\.') = 0 ]; then
299 + if [ -n "$(dnsdomainname)" ]; then
300 + utsname=${utsname}.$(dnsdomainname)
305 type yum >/dev/null 2>&1
306 if [ $? -ne 0 ]; then
307 @@ -365,33 +495,43 @@ fi
309 if [ -n "$needed_pkgs" ]; then
310 echo "Missing commands: $needed_pkgs"
311 - echo "Please install these using \"sudo apt-get install $needed_pkgs\""
312 + echo "Please install these using \"sudo yum install $needed_pkgs\""
316 if [ -z "$path" ]; then
318 + path=$default_path/$name
321 if [ -z "$release" ]; then
322 - if [ "$is_fedora" ]; then
323 - release=$(cat /etc/fedora-release |awk '/^Fedora/ {print $3}')
324 + if [ "$is_fedora" -a "$fedora_host_ver" ]; then
325 + release=$fedora_host_ver
327 - echo "This is not a fedora host and release missing, defaulting to 14. use -R|--release to specify release"
329 + echo "This is not a fedora host and release missing, defaulting to 18. use -R|--release to specify release"
334 +# Fedora 15 and above run systemd. We need autodev enabled to keep
335 +# systemd from causing problems.
336 +if [ $release -gt 14 ]; then
342 if [ "$(id -u)" != "0" ]; then
343 echo "This script should be run as 'root'"
348 -rootfs_path=$path/$name/rootfs
349 -# check for 'lxc.rootfs' passed in through default config by lxc-create
350 -if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
351 - rootfs_path=`grep 'lxc.rootfs =' $path/config | awk -F= '{ print $2 }'`
352 +if [ -z "$rootfs_path" ]; then
353 + rootfs_path=$path/rootfs
354 + # check for 'lxc.rootfs' passed in through default config by lxc-create
355 + if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
356 + rootfs_path=`grep 'lxc.rootfs =' $path/config | awk -F= '{ print $2 }'`
359 config_path=$default_path/$name
360 cache=$cache_base/$release
361 @@ -427,13 +567,20 @@ if [ $? -ne 0 ]; then
365 -type /bin/systemd >/dev/null 2>&1
366 -if [ $? -ne 0 ]; then
367 - configure_fedora_init
369 +# If the systemd configuration directory exists - set it up for what we need.
370 +if [ -d ${rootfs_path}/etc/systemd/system ]
372 configure_fedora_systemd
375 +# This configuration (rc.sysinit) is not inconsistent with the systemd stuff
376 +# above and may actually coexist on some upgraded systems. Let's just make
377 +# sure that, if it exists, we update this file, even if it's not used...
378 +if [ -f ${rootfs_path}/etc/rc.sysinit ]
380 + configure_fedora_init
383 if [ ! -z $clean ]; then