summaryrefslogtreecommitdiffstats
path: root/lxc-0.9.0-fedora-template.patch
blob: 48eff32f5299c9b1a6d1995f5e0ade5c06b98f46 (plain)
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
diff --git a/templates/lxc-fedora.in b/templates/lxc-fedora.in
index 684bb9c..841af16 100644
--- a/templates/lxc-fedora.in
+++ b/templates/lxc-fedora.in
@@ -26,18 +26,69 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 #Configurations
-arch=$(arch)
+arch=$(uname -m)
 cache_base=@LOCALSTATEDIR@/cache/lxc/fedora/$arch
 default_path=@LXCPATH@
 root_password=root
 
 # is this fedora?
-[ -f /etc/fedora-release ] && is_fedora=true
+# Alow for weird remixes like the Raspberry Pi
+#
+# Use the Mitre standard CPE identifier for the release ID if possible...
+# This may be in /etc/os-release or /etc/system-release-cpe.  We
+# should be able to use EITHER.  Give preference to /etc/os-release for now.
+
+if [ -e /etc/os-release ]
+then
+# This is a shell friendly configuration file.  We can just source it.
+# What we're looking for in here is the ID, VERSION_ID and the CPE_NAME
+    . /etc/os-release
+    echo "Host CPE ID from /etc/os-release: ${CPE_NAME}"
+fi
+
+if [ "${CPE_NAME}" = "" -a -e /etc/system-release-cpe ]
+then
+    CPE_NAME=$(head -n1 /etc/system-release-cpe)
+    CPE_URI=$(expr ${CPE_NAME} : '\([^:]*:[^:*]\)')
+    if [ "${CPE_URI}" != "cpe:/o" ]
+    then
+        CPE_NAME=
+    else
+        echo "Host CPE ID from /etc/system-release-cpe: ${CPE_NAME}"
+        # Probably a better way to do this but sill remain posix
+        # compatible but this works, shrug...
+        # Must be nice and not introduce convenient bashisms here.
+        ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:[^:]*:\([^:]*\)')
+        VERSION_ID=$(expr ${CPE_NAME} : '[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\)')
+    fi
+fi
 
-if [ "$arch" = "i686" ]; then
-    arch=i386
+if [ ${CPE_NAME} != "" -a ${ID} = "fedora" -a ${VERSION_ID} != "" ]
+then
+    fedora_host_ver=${VERSION_ID}
+    is_fedora=true
+elif [ -e /etc/redhat-release ]
+then
+    # Only if all other methods fail, try to parse the redhat-release file.
+    fedora_host_ver=$( sed -e '/^Fedora /!d' -e 's/Fedora.*\srelease\s*\([0-9][0-9]*\)\s.*/\1/' < /etc/redhat-release )
+    if [ "$fedora_host_ver" != "" ]
+    then
+        is_fedora=true
+    fi
 fi
 
+# Map a few architectures to their generic Fedora repository archs.
+# The two ARM archs are a bit of a guesstimate for the v5 and v6
+# archs.  V6 should have hardware floating point (Rasberry Pi).
+# The "arm" arch is safer (no hardware floating point).  So
+# there may be cases where we "get it wrong" for some v6 other
+# than RPi.
+case "$arch" in
+i686) arch=i386 ;;
+armv3l|armv4l|armv5l) arch=arm ;;
+armv6l|armv7l|armv8l) arch=armhfp ;;
+esac
+
 configure_fedora()
 {
 
@@ -50,7 +101,7 @@ configure_fedora()
 DEVICE=eth0
 BOOTPROTO=dhcp
 ONBOOT=yes
-HOSTNAME=${name}
+HOSTNAME=${utsname}
 NM_CONTROLLED=no
 TYPE=Ethernet
 MTU=${MTU}
@@ -59,12 +110,18 @@ EOF
     # set the hostname
     cat <<EOF > ${rootfs_path}/etc/sysconfig/network
 NETWORKING=yes
-HOSTNAME=${name}
+HOSTNAME=${utsname}
 EOF
 
+    # set hostname on systemd Fedora systems
+    if [ $release -gt 14 ]; then
+        echo "${utsname}" > ${rootfs_path}/etc/hostname
+    fi
+
     # set minimal hosts
     cat <<EOF > $rootfs_path/etc/hosts
-127.0.0.1 localhost $name
+127.0.0.1 localhost.localdomain localhost $utsname
+::1                 localhost6.localdomain6 localhost6
 EOF
 
     dev_path="${rootfs_path}/dev"
@@ -118,10 +175,27 @@ configure_fedora_systemd()
 {
     unlink ${rootfs_path}/etc/systemd/system/default.target
     touch ${rootfs_path}/etc/fstab
-    chroot ${rootfs_path} ln -s /dev/null //etc/systemd/system/udev.service
+    chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/udev.service
     chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
     #dependency on a device unit fails it specially that we disabled udev
-    sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
+    # sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
+    #
+    # Actually, the After=dev-%i.device line does not appear in the
+    # Fedora 17 or Fedora 18 systemd getty\@.service file.  It may be left
+    # over from an earlier version and it's not doing any harm.  We do need
+    # to disable the "ConditionalPathExists=/dev/tty0" line or no gettys are
+    # started on the ttys in the container.  Lets do it in an override copy of
+    # the service so it can still pass rpm verifies and not be automatically
+    # updated by a new systemd version.  --  mhw  /\/\|=mhw=|\/\/
+
+    sed -e 's/^ConditionPathExists=/# ConditionPathExists=/' \
+        -e 's/After=dev-%i.device/After=/' \
+	< ${rootfs_path}/lib/systemd/system/getty\@.service \
+	> ${rootfs_path}/etc/systemd/system/getty\@.service
+    # Setup getty service on the 4 ttys we are going to allow in the
+    # default config.  Number should match lxc.tty
+    ( cd ${rootfs_path}/etc/systemd/system/getty.target.wants
+        for i in 1 2 3 4 ; do ln -sf ../getty\@.service getty@tty${i}.service; done )
 }
 
 download_fedora()
@@ -142,27 +216,53 @@ download_fedora()
     MIRRORLIST_URL="http://mirrors.fedoraproject.org/mirrorlist?repo=fedora-$release&arch=$arch"
 
     DOWNLOAD_OK=no
-    for trynumber in 1 2 3; do
+
+    # We're splitting the old loop into two loops plus a directory retrival.
+    # First loop...  Try and retrive a mirror list with retries and a slight
+    # delay between attempts...
+    for trynumber in 1 2 3 4; do
         [ $trynumber != 1 ] && echo "Trying again..."
-        MIRROR_URL=$(curl -s -S -f "$MIRRORLIST_URL" | head -n2 | tail -n1)
-        if [ $? -ne 0 ] || [ -z "$MIRROR_URL" ]; then
-            echo "Failed to get a mirror"
-            continue
-        fi
+	# This code is mildly "brittle" in that it assumes a certain
+        # page format and parsing HTML.  I've done worse.  :-P
+        MIRROR_URLS=$(curl -s -S -f "$MIRRORLIST_URL" | sed -e '/^http:/!d' -e '2,6!d')
+        if [ $? -eq 0 ] && [ -n "$MIRROR_URLS" ]
+        then
+		break
+	fi
+
+        echo "Failed to get a mirror on try $trynumber"
+        sleep 3
+     done
+
+     # This will fall through if we didn't get any URLS above
+     for MIRROR_URL in ${MIRROR_URLS}
+     do
         if [ "$release" -gt "16" ]; then
-            RELEASE_URL="$MIRROR_URL/Packages/f/fedora-release-$release-1.noarch.rpm"
+            RELEASE_URL="$MIRROR_URL/Packages/f"
         else
-            RELEASE_URL="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
+            RELEASE_URL="$MIRROR_URL/Packages/"
         fi
-        echo "Fetching from $RELEASE_URL"
-        curl -f "$RELEASE_URL" > $INSTALL_ROOT/fedora-release-$release.noarch.rpm
+
+        echo "Fetching rpm name from $RELEASE_URL..."
+	# This code is mildly "brittle" in that it assumes a certain directory
+        # page format and parsing HTML.  I've done worse.  :-P
+	RELEASE_RPM=$(curl -L -f "$RELEASE_URL" | sed -e "/fedora-release-${release}-/!d" -e 's/.*<a href=\"//' -e 's/\">.*//' )
+        if [ $? -ne 0  -o "${RELEASE_RPM}" = "" ]; then
+            echo "Failed to identify fedora release rpm."
+            continue
+        fi
+
+        echo "Fetching fedora release rpm from ${RELEASE_URL}/${RELEASE_RPM}......"
+        curl -L -f "${RELEASE_URL}/${RELEASE_RPM}" > ${INSTALL_ROOT}/${RELEASE_RPM}
         if [ $? -ne 0 ]; then
-            echo "Failed to download fedora release rpm"
+            echo "Failed to download fedora release rpm ${RELEASE_RPM}."
             continue
         fi
+
         DOWNLOAD_OK=yes
         break
     done
+
     if [ $DOWNLOAD_OK != yes ]; then
         echo "Aborting"
         return 1
@@ -170,7 +270,7 @@ download_fedora()
 
     mkdir -p $INSTALL_ROOT/var/lib/rpm
     rpm --root $INSTALL_ROOT  --initdb
-    rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm
+    rpm --root $INSTALL_ROOT -ivh ${INSTALL_ROOT}/${RELEASE_RPM}
     $YUM install $PKG_LIST
 
     if [ $? -ne 0 ]; then
@@ -248,10 +348,13 @@ copy_configuration()
     mkdir -p $config_path
     grep -q "^lxc.rootfs" $config_path/config 2>/dev/null || echo "lxc.rootfs = $rootfs_path" >> $config_path/config
     cat <<EOF >> $config_path/config
-lxc.utsname = $name
+lxc.utsname = $utsname
 lxc.tty = 4
 lxc.pts = 1024
 lxc.mount = $config_path/fstab
+lxc.cap.drop = sys_module mac_admin mac_override sys_time
+
+lxc.autodev = $auto_dev
 
 # When using LXC with apparmor, uncomment the next line to run unconfined:
 #lxc.aa_profile = unconfined
@@ -272,7 +375,7 @@ lxc.cgroup.devices.allow = c 1:8 rwm
 lxc.cgroup.devices.allow = c 136:* rwm
 lxc.cgroup.devices.allow = c 5:2 rwm
 # rtc
-lxc.cgroup.devices.allow = c 254:0 rwm
+lxc.cgroup.devices.allow = c 254:0 rm
 EOF
 
     cat <<EOF > $config_path/fstab
@@ -313,21 +416,23 @@ usage()
     cat <<EOF
 usage:
     $1 -n|--name=<container_name>
-        [-p|--path=<path>] [-c|--clean] [-R|--release=<Fedora_release>] [-A|--arch=<arch of the container>]
+        [-p|--path=<path>] [-c|--clean] [-R|--release=<Fedora_release>] [--fqdn=<network name of container>] [-A|--arch=<arch of the container>]
         [-h|--help]
 Mandatory args:
   -n,--name         container name, used to as an identifier for that container from now on
 Optional args:
-  -p,--path         path to where the container rootfs will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
+  -p,--path         path to where the container will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
+  --rootfs          path for actual rootfs.
   -c,--clean        clean the cache
-  -R,--release      Fedora release for the new container. if the host is Fedora, then it will defaultto the host's release.
+  -R,--release      Fedora release for the new container. if the host is Fedora, then it will default to the host's release.
+     --fqdn         fully qualified domain name (FQDN) for DNS and system naming
   -A,--arch         NOT USED YET. Define what arch the container will be [i686,x86_64]
   -h,--help         print this help
 EOF
     return 0
 }
 
-options=$(getopt -o hp:n:cR: -l help,path:,name:,clean,release: -- "$@")
+options=$(getopt -o hp:n:cR: -l help,path:,rootfs:,name:,clean,release:,fqdn: -- "$@")
 if [ $? -ne 0 ]; then
     usage $(basename $0)
     exit 1
@@ -339,9 +444,11 @@ do
     case "$1" in
         -h|--help)      usage $0 && exit 0;;
         -p|--path)      path=$2; shift 2;;
+        --rootfs)       rootfs=$2; shift 2;;
         -n|--name)      name=$2; shift 2;;
         -c|--clean)     clean=$2; shift 2;;
         -R|--release)   release=$2; shift 2;;
+        --fqdn)         utsname=$2; shift 2;;
         --)             shift 1; break ;;
         *)              break ;;
     esac
@@ -352,6 +459,29 @@ if [ ! -z "$clean" -a -z "$path" ]; then
     exit 0
 fi
 
+if [ -z "${utsname}" ]; then
+    utsname=${name}
+fi
+
+# This follows a standard "resolver" convention that an FQDN must have
+# at least two dots or it is considered a local relative host name.
+# If it doesn't, append the dns domain name of the host system.
+#
+# This changes one significant behavior when running
+# "lxc_create -n Container_Name" without using the
+# --fqdn option.
+#
+# Old behavior:
+#    utsname and hostname = Container_Name
+# New behavior:
+#    utsname and hostname = Container_Name.Domain_Name
+
+if [ $(expr "$utsname" : '.*\..*\.') = 0 ]; then
+    if [ -n "$(dnsdomainname)" ]; then
+        utsname=${utsname}.$(dnsdomainname)
+    fi
+fi
+
 needed_pkgs=""
 type yum >/dev/null 2>&1
 if [ $? -ne 0 ]; then
@@ -365,33 +495,43 @@ fi
 
 if [ -n "$needed_pkgs" ]; then
     echo "Missing commands: $needed_pkgs"
-    echo "Please install these using \"sudo apt-get install $needed_pkgs\""
+    echo "Please install these using \"sudo yum install $needed_pkgs\""
     exit 1
 fi
 
 if [ -z "$path" ]; then
-    path=$default_path
+    path=$default_path/$name
 fi
 
 if [ -z "$release" ]; then
-    if [ "$is_fedora" ]; then
-        release=$(cat /etc/fedora-release |awk '/^Fedora/ {print $3}')
+    if [ "$is_fedora" -a "$fedora_host_ver" ]; then
+        release=$fedora_host_ver
     else
-        echo "This is not a fedora host and release missing, defaulting to 14. use -R|--release to specify release"
-        release=14
+        echo "This is not a fedora host and release missing, defaulting to 18. use -R|--release to specify release"
+        release=18
     fi
 fi
 
+# Fedora 15 and above run systemd.  We need autodev enabled to keep
+# systemd from causing problems.
+if [ $release -gt 14 ]; then
+    auto_dev="1"
+else
+    auto_dev="0"
+fi
+
 if [ "$(id -u)" != "0" ]; then
     echo "This script should be run as 'root'"
     exit 1
 fi
 
 
-rootfs_path=$path/$name/rootfs
-# check for 'lxc.rootfs' passed in through default config by lxc-create
-if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
-    rootfs_path=`grep 'lxc.rootfs =' $path/config | awk -F= '{ print $2 }'`
+if [ -z "$rootfs_path" ]; then
+    rootfs_path=$path/rootfs
+    # check for 'lxc.rootfs' passed in through default config by lxc-create
+    if grep -q '^lxc.rootfs' $path/config 2>/dev/null ; then
+        rootfs_path=`grep 'lxc.rootfs =' $path/config | awk -F= '{ print $2 }'`
+    fi
 fi
 config_path=$default_path/$name
 cache=$cache_base/$release
@@ -427,13 +567,20 @@ if [ $? -ne 0 ]; then
     exit 1
 fi
 
-type /bin/systemd >/dev/null 2>&1
-if [ $? -ne 0 ]; then
-    configure_fedora_init
-else
+# If the systemd configuration directory exists - set it up for what we need.
+if [ -d ${rootfs_path}/etc/systemd/system ]
+then
     configure_fedora_systemd
 fi
 
+# This configuration (rc.sysinit) is not inconsistent with the systemd stuff
+# above and may actually coexist on some upgraded systems.  Let's just make
+# sure that, if it exists, we update this file, even if it's not used...
+if [ -f ${rootfs_path}/etc/rc.sysinit ]
+then
+    configure_fedora_init
+fi
+
 if [ ! -z $clean ]; then
     clean || exit 1
     exit 0