]> git.pld-linux.org Git - packages/lxc.git/blob - fedora-template.patch
update fedora template from fedora
[packages/lxc.git] / fedora-template.patch
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
5 @@ -26,18 +26,69 @@
6  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7  
8  #Configurations
9 -arch=$(arch)
10 +arch=$(uname -m)
11  cache_base=@LOCALSTATEDIR@/cache/lxc/fedora/$arch
12  default_path=@LXCPATH@
13  root_password=root
14  
15  # is this fedora?
16 -[ -f /etc/fedora-release ] && is_fedora=true
17 +# Alow for weird remixes like the Raspberry Pi
18 +#
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.
22 +
23 +if [ -e /etc/os-release ]
24 +then
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
27 +    . /etc/os-release
28 +    echo "Host CPE ID from /etc/os-release: ${CPE_NAME}"
29 +fi
30 +
31 +if [ "${CPE_NAME}" = "" -a -e /etc/system-release-cpe ]
32 +then
33 +    CPE_NAME=$(head -n1 /etc/system-release-cpe)
34 +    CPE_URI=$(expr ${CPE_NAME} : '\([^:]*:[^:*]\)')
35 +    if [ "${CPE_URI}" != "cpe:/o" ]
36 +    then
37 +        CPE_NAME=
38 +    else
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} : '[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\)')
45 +    fi
46 +fi
47  
48 -if [ "$arch" = "i686" ]; then
49 -    arch=i386
50 +if [ "${CPE_NAME}" != "" -a "${ID}" = "fedora" -a "${VERSION_ID}" != "" ]
51 +then
52 +    fedora_host_ver=${VERSION_ID}
53 +    is_fedora=true
54 +elif [ -e /etc/redhat-release ]
55 +then
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" != "" ]
59 +    then
60 +        is_fedora=true
61 +    fi
62  fi
63  
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
69 +# than RPi.
70 +case "$arch" in
71 +i686) arch=i386 ;;
72 +armv3l|armv4l|armv5l) arch=arm ;;
73 +armv6l|armv7l|armv8l) arch=armhfp ;;
74 +esac
75 +
76  configure_fedora()
77  {
78  
79 @@ -50,7 +101,7 @@ configure_fedora()
80  DEVICE=eth0
81  BOOTPROTO=dhcp
82  ONBOOT=yes
83 -HOSTNAME=${name}
84 +HOSTNAME=${utsname}
85  NM_CONTROLLED=no
86  TYPE=Ethernet
87  MTU=${MTU}
88 @@ -59,12 +110,18 @@ EOF
89      # set the hostname
90      cat <<EOF > ${rootfs_path}/etc/sysconfig/network
91  NETWORKING=yes
92 -HOSTNAME=${name}
93 +HOSTNAME=${utsname}
94  EOF
95  
96 +    # set hostname on systemd Fedora systems
97 +    if [ $release -gt 14 ]; then
98 +        echo "${utsname}" > ${rootfs_path}/etc/hostname
99 +    fi
100 +
101      # set minimal hosts
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
106  EOF
107  
108      dev_path="${rootfs_path}/dev"
109 @@ -118,10 +175,27 @@ configure_fedora_systemd()
110  {
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
119 +    #
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=|\/\/
127 +
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 )
136  }
137  
138  download_fedora()
139 @@ -142,27 +216,53 @@ download_fedora()
140      MIRRORLIST_URL="http://mirrors.fedoraproject.org/mirrorlist?repo=fedora-$release&arch=$arch"
141  
142      DOWNLOAD_OK=no
143 -    for trynumber in 1 2 3; do
144 +
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"
153 -            continue
154 -        fi
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" ]
159 +        then
160 +               break
161 +       fi
162 +
163 +        echo "Failed to get a mirror on try $trynumber"
164 +        sleep 3
165 +     done
166 +
167 +     # This will fall through if we didn't get any URLS above
168 +     for MIRROR_URL in ${MIRROR_URLS}
169 +     do
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"
173          else
174 -            RELEASE_URL="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
175 +            RELEASE_URL="$MIRROR_URL/Packages/"
176          fi
177 -        echo "Fetching from $RELEASE_URL"
178 -        curl -f "$RELEASE_URL" > $INSTALL_ROOT/fedora-release-$release.noarch.rpm
179 +
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."
186 +            continue
187 +        fi
188 +
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}."
194              continue
195          fi
196 +
197          DOWNLOAD_OK=yes
198          break
199      done
200 +
201      if [ $DOWNLOAD_OK != yes ]; then
202          echo "Aborting"
203          return 1
204 @@ -170,7 +270,7 @@ download_fedora()
205  
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
211  
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
217 -lxc.utsname = $name
218 +lxc.utsname = $utsname
219  lxc.tty = 4
220  lxc.pts = 1024
221  lxc.mount = $config_path/fstab
222 +lxc.cap.drop = sys_module mac_admin mac_override sys_time
223 +
224 +lxc.autodev = $auto_dev
225  
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
231  # rtc
232 -lxc.cgroup.devices.allow = c 254:0 rwm
233 +lxc.cgroup.devices.allow = c 254:0 rm
234  EOF
235  
236      cat <<EOF > $config_path/fstab
237 @@ -313,21 +416,23 @@ usage()
238      cat <<EOF
239  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>]
243          [-h|--help]
244  Mandatory args:
245    -n,--name         container name, used to as an identifier for that container from now on
246  Optional args:
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
256  EOF
257      return 0
258  }
259  
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
263      usage $(basename $0)
264      exit 1
265 @@ -339,9 +444,11 @@ do
266      case "$1" in
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 ;;
275          *)              break ;;
276      esac
277 @@ -352,6 +459,29 @@ if [ ! -z "$clean" -a -z "$path" ]; then
278      exit 0
279  fi
280  
281 +if [ -z "${utsname}" ]; then
282 +    utsname=${name}
283 +fi
284 +
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.
288 +#
289 +# This changes one significant behavior when running
290 +# "lxc_create -n Container_Name" without using the
291 +# --fqdn option.
292 +#
293 +# Old behavior:
294 +#    utsname and hostname = Container_Name
295 +# New behavior:
296 +#    utsname and hostname = Container_Name.Domain_Name
297 +
298 +if [ $(expr "$utsname" : '.*\..*\.') = 0 ]; then
299 +    if [ -n "$(dnsdomainname)" ]; then
300 +        utsname=${utsname}.$(dnsdomainname)
301 +    fi
302 +fi
303 +
304  needed_pkgs=""
305  type yum >/dev/null 2>&1
306  if [ $? -ne 0 ]; then
307 @@ -365,33 +495,43 @@ fi
308  
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\""
313      exit 1
314  fi
315  
316  if [ -z "$path" ]; then
317 -    path=$default_path
318 +    path=$default_path/$name
319  fi
320  
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
326      else
327 -        echo "This is not a fedora host and release missing, defaulting to 14. use -R|--release to specify release"
328 -        release=14
329 +        echo "This is not a fedora host and release missing, defaulting to 18. use -R|--release to specify release"
330 +        release=18
331      fi
332  fi
333  
334 +# Fedora 15 and above run systemd.  We need autodev enabled to keep
335 +# systemd from causing problems.
336 +if [ $release -gt 14 ]; then
337 +    auto_dev="1"
338 +else
339 +    auto_dev="0"
340 +fi
341 +
342  if [ "$(id -u)" != "0" ]; then
343      echo "This script should be run as 'root'"
344      exit 1
345  fi
346  
347  
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 }'`
357 +    fi
358  fi
359  config_path=$default_path/$name
360  cache=$cache_base/$release
361 @@ -427,13 +567,20 @@ if [ $? -ne 0 ]; then
362      exit 1
363  fi
364  
365 -type /bin/systemd >/dev/null 2>&1
366 -if [ $? -ne 0 ]; then
367 -    configure_fedora_init
368 -else
369 +# If the systemd configuration directory exists - set it up for what we need.
370 +if [ -d ${rootfs_path}/etc/systemd/system ]
371 +then
372      configure_fedora_systemd
373  fi
374  
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 ]
379 +then
380 +    configure_fedora_init
381 +fi
382 +
383  if [ ! -z $clean ]; then
384      clean || exit 1
385      exit 0
This page took 0.09401 seconds and 3 git commands to generate.