On Mon, May 30, 2011 at 11:00 AM, Daniel Lezcano <span dir="ltr"><<a href="mailto:daniel.lezcano@free.fr">daniel.lezcano@free.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div class="h5">On 05/30/2011 09:32 AM, Ramez Hanna wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
hi,<br>
<br>
here is my lxc-fedora script again based on request from Daniel Lezcano<br>
it has been tested to work on fedora and ubuntu hosts<br>
it was tested to create fedora 14 and 13 guests (not f15 yet)<br>
<br>
i had submitted it as a merge request earlier to gitorious repo lxc-mainline<br>
<br>
this script has extra args to the other scripts so it won't work directly<br>
through the lxc-create -t<br>
it can be modified to do that but i am not sure if i should spin off several<br>
ones with the release hardcoded in them like with debian/ubuntu templates<br>
</blockquote>
<br></div></div>
Yep, there is a some work to do with the ubuntu templates to factor the code.<br>
I would suggest you default to one fedora version if no release version is specified.<br>
<br>
<br>
I inlined the code in the email so it will be easier to review.<br>
Please in the future make sure the patch is inlined and conforming to the CONTRIBUTING patch submit, that is with the author, subject and signed-off-by.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
#!/bin/bash<br>
<br>
#<br>
# template script for generating fedora container for LXC<br>
#<br>
<br>
#<br>
# lxc: linux Container library<br>
<br>
# Authors:<br>
# Daniel Lezcano <<a href="mailto:daniel.lezcano@free.fr" target="_blank">daniel.lezcano@free.fr</a>><br>
# Ramez Hanna <<a href="mailto:rhanna@informatiq.org" target="_blank">rhanna@informatiq.org</a>><br>
<br>
# This library is free software; you can redistribute it and/or<br>
# modify it under the terms of the GNU Lesser General Public<br>
# License as published by the Free Software Foundation; either<br>
# version 2.1 of the License, or (at your option) any later version.<br>
<br>
# This library is distributed in the hope that it will be useful,<br>
# but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
# Lesser General Public License for more details.<br>
<br>
# You should have received a copy of the GNU Lesser General Public<br>
# License along with this library; if not, write to the Free Software<br>
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br>
<br>
#Configurations<br>
arch=$(arch)<br>
cache_base=/var/cache/lxc/fedora/$arch<br>
</blockquote>
<br>
shouldn't it be /var/cache/lxc/$release/$arch ?<br></blockquote><div>no because later cache=$cache_base/$release when release is actually known </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
default_path=/var/lib/lxc<br>
root_password=rooter<br>
lxc_network_type=veth<br>
lxc_network_link=virbr0<br>
<br>
# is this fedora?<br>
[ -f /etc/fedora-release ] && is_fedora=true<br>
<br>
configure_fedora()<br>
{<br>
<br>
# disable selinux in fedora<br>
mkdir -p $rootfs_path/selinux<br>
echo 0 > $rootfs_path/selinux/enforce<br>
<br>
# configure the network using the dhcp<br>
cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-eth0<br>
DEVICE=eth0<br>
BOOTPROTO=dhcp<br>
ONBOOT=yes<br>
HOSTNAME=${UTSNAME}<br>
NM_CONTROLLED=no<br>
TYPE=Ethernet<br>
MTU=${MTU}<br>
EOF<br>
<br>
# set the hostname<br>
cat <<EOF > ${rootfs_path}/etc/sysconfig/network<br>
NETWORKING=yes<br>
HOSTNAME=${UTSNAME}<br>
EOF<br>
<br>
# set minimal hosts<br>
cat <<EOF > $rootfs_path/etc/hosts<br>
127.0.0.1 localhost $name<br>
EOF<br>
<br>
sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit<br>
sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit<br>
chroot ${rootfs_path} chkconfig udev-post off<br>
chroot ${rootfs_path} chkconfig network on<br>
<br>
dev_path="${rootfs_path}/dev"<br>
rm -rf $dev_path<br>
mkdir -p $dev_path<br>
mknod -m 666 ${dev_path}/null c 1 3<br>
mknod -m 666 ${dev_path}/zero c 1 5<br>
mknod -m 666 ${dev_path}/random c 1 8<br>
mknod -m 666 ${dev_path}/urandom c 1 9<br>
mkdir -m 755 ${dev_path}/pts<br>
mkdir -m 1777 ${dev_path}/shm<br>
mknod -m 666 ${dev_path}/tty c 5 0<br>
mknod -m 666 ${dev_path}/tty0 c 4 0<br>
mknod -m 666 ${dev_path}/tty1 c 4 1<br>
mknod -m 666 ${dev_path}/tty2 c 4 2<br>
mknod -m 666 ${dev_path}/tty3 c 4 3<br>
mknod -m 666 ${dev_path}/tty4 c 4 4<br>
mknod -m 600 ${dev_path}/console c 5 1<br>
mknod -m 666 ${dev_path}/full c 1 7<br>
mknod -m 600 ${dev_path}/initctl p<br>
mknod -m 666 ${dev_path}/ptmx c 5 2<br>
<br>
echo "setting root passwd to $root_password"<br>
echo "root:$root_password" | chroot $rootfs_path chpasswd<br>
<br>
return 0<br>
}<br>
<br>
download_fedora()<br>
{<br>
<br>
# check the mini fedora was not already downloaded<br>
INSTALL_ROOT=$cache/partial<br>
mkdir -p $INSTALL_ROOT<br>
if [ $? -ne 0 ]; then<br>
echo "Failed to create '$INSTALL_ROOT' directory"<br>
return 1<br>
fi<br>
<br>
# download a mini fedora into a cache<br>
echo "Downloading fedora minimal ..."<br>
YUM="yum --installroot $INSTALL_ROOT -y --nogpgcheck"<br>
PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils"<br>
RELEASE_URL="<a href="http://ftp.funet.fi/pub/mirrors/fedora.redhat.com/pub/fedora/linux/releases/$release/Everything/x86_64/os/Packages/fedora-release-$release-1.noarch.rpm" target="_blank">http://ftp.funet.fi/pub/mirrors/fedora.redhat.com/pub/fedora/linux/releases/$release/Everything/x86_64/os/Packages/fedora-release-$release-1.noarch.rpm</a>"<br>
</blockquote>
<br> s/x86_64/$arch ? </blockquote><div> </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
curl $RELEASE_URL > $INSTALL_ROOT/fedora-release-$release.noarch.rpm<br>
<br>
mkdir -p $INSTALL_ROOT/var/lib/rpm<br>
rpm --root $INSTALL_ROOT --initdb<br>
rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm<br>
$YUM install $PKG_LIST<br>
<br>
if [ $? -ne 0 ]; then<br>
echo "Failed to download the rootfs, aborting."<br>
return 1<br>
fi<br>
<br>
mv "$INSTALL_ROOT" "$cache/rootfs"<br>
echo "Download complete."<br>
<br>
return 0<br>
}<br>
<br>
copy_fedora()<br>
{<br>
<br>
# make a local copy of the minifedora<br>
echo -n "Copying rootfs to $rootfs_path ..."<br>
#cp -a $cache/rootfs-$arch $rootfs_path || return 1<br>
# i prefer rsync (no reason really)<br>
mkdir -p $rootfs_path<br>
rsync -a $cache/rootfs/ $rootfs_path/<br>
return 0<br>
}<br>
<br>
update_fedora()<br>
{<br>
chroot $cache/rootfs yum -y update<br>
}<br>
<br>
install_fedora()<br>
{<br>
mkdir -p /var/lock/subsys/<br>
(<br>
flock -n -x 200<br>
if [ $? -ne 0 ]; then<br>
echo "Cache repository is busy."<br>
return 1<br>
fi<br>
<br>
echo "Checking cache download in $cache/rootfs ... "<br>
if [ ! -e "$cache/rootfs" ]; then<br>
download_fedora<br>
if [ $? -ne 0 ]; then<br>
echo "Failed to download 'fedora base'"<br>
return 1<br>
fi<br>
else<br>
echo "Cache found. Updating..."<br>
update_fedora<br>
if [ $? -ne 0 ]; then<br>
echo "Failed to update 'fedora base', continuing with last known good cache"<br>
else<br>
echo "Update finished"<br>
fi<br>
fi<br>
<br>
echo "Copy $cache/rootfs to $rootfs_path ... "<br>
copy_fedora<br>
if [ $? -ne 0 ]; then<br>
echo "Failed to copy rootfs"<br>
return 1<br>
fi<br>
<br>
return 0<br>
<br>
) 200>/var/lock/subsys/lxc<br>
<br>
return $?<br>
}<br>
<br>
copy_configuration()<br>
{<br>
<br>
mkdir -p $config_path<br>
cat <<EOF >> $config_path/config<br>
lxc.utsname = $name<br>
lxc.tty = 4<br>
lxc.pts = 1024<br>
lxc.rootfs = $rootfs_path<br>
lxc.mount = $config_path/fstab<br>
#networking<br>
lxc.network.type = $lxc_network_type<br>
lxc.network.flags = up<br>
lxc.network.link = $lxc_network_link<br>
<a href="http://lxc.network.name" target="_blank">lxc.network.name</a> = eth0<br>
lxc.network.mtu = 1500<br>
#cgroups<br>
lxc.cgroup.devices.deny = a<br>
# /dev/null and zero<br>
lxc.cgroup.devices.allow = c 1:3 rwm<br>
lxc.cgroup.devices.allow = c 1:5 rwm<br>
# consoles<br>
lxc.cgroup.devices.allow = c 5:1 rwm<br>
lxc.cgroup.devices.allow = c 5:0 rwm<br>
lxc.cgroup.devices.allow = c 4:0 rwm<br>
lxc.cgroup.devices.allow = c 4:1 rwm<br>
# /dev/{,u}random<br>
lxc.cgroup.devices.allow = c 1:9 rwm<br>
lxc.cgroup.devices.allow = c 1:8 rwm<br>
lxc.cgroup.devices.allow = c 136:* rwm<br>
lxc.cgroup.devices.allow = c 5:2 rwm<br>
# rtc<br>
lxc.cgroup.devices.allow = c 254:0 rwm<br>
EOF<br>
<br>
cat <<EOF > $config_path/fstab<br>
proc $rootfs_path/proc proc nodev,noexec,nosuid 0 0<br>
devpts $rootfs_path/dev/pts devpts defaults 0 0<br>
sysfs $rootfs_path/sys sysfs defaults 0 0<br>
EOF<br>
<br>
if [ $? -ne 0 ]; then<br>
echo "Failed to add configuration"<br>
return 1<br>
fi<br>
<br>
return 0<br>
}<br>
<br>
clean()<br>
{<br>
<br>
if [ ! -e $cache ]; then<br>
exit 0<br>
fi<br>
<br>
# lock, so we won't purge while someone is creating a repository<br>
(<br>
flock -n -x 200<br>
if [ $? != 0 ]; then<br>
echo "Cache repository is busy."<br>
exit 1<br>
fi<br>
<br>
echo -n "Purging the download cache for Fedora-$release..."<br>
rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1<br>
exit 0<br>
<br>
) 200>/var/lock/subsys/lxc<br>
}<br>
<br>
usage()<br>
{<br>
cat <<EOF<br>
usage:<br>
$1 -n|--name=<container_name><br>
[-p|--path=<path>] [-c|--clean] [-R|--release=<Fedora_release>] [-A|--arch=<arch of the container>]<br>
[-h|--help]<br>
Mandatory args:<br>
-n,--name container name, used to as an identifier for that container from now on<br>
Optional args:<br>
-p,--path path to where the container rootfs will be created, defaults to /var/lib/lxc. The container config will go under /var/lib/lxc in and case<br>
-c,--clean clean the cache<br>
-R,--release Fedora release for the new container. if the host is Fedora, then it will defaultto the host's release.<br>
-A,--arch NOT USED YET. Define what arch the container will be [i686,x86_64]<br>
-h,--help print this help<br>
EOF<br>
return 0<br>
}<br>
<br>
options=$(getopt -o hp:n:cR: -l help,path:,name:,clean,release: -- "$@")<br>
if [ $? -ne 0 ]; then<br>
usage $(basename $0)<br>
exit 1<br>
fi<br>
eval set -- "$options"<br>
<br>
while true<br>
do<br>
case "$1" in<br>
-h|--help) usage $0 && exit 0;;<br>
-p|--path) path=$2; shift 2;;<br>
-n|--name) name=$2; shift 2;;<br>
-c|--clean) clean=$2; shift 2;;<br>
-R|--release) release=$2; shift 2;;<br>
--) shift 1; break ;;<br>
*) break ;;<br>
esac<br>
done<br>
<br>
if [ ! -z "$clean" -a -z "$path" ]; then<br>
clean || exit 1<br>
exit 0<br>
fi<br>
<br>
type yum >/dev/null 2>&1<br>
if [ $? -ne 0 ]; then<br>
echo "'yum' command is missing"<br>
exit 1<br>
fi<br>
<br>
if [ -z "$path" ]; then<br>
path=$default_path<br>
fi<br>
<br>
if [ -z "$release" ]; then<br>
if [ "$is_fedora" ]; then<br>
release=$(cat /etc/fedora-release |awk '/^Fedora/ {print $3}')<br>
else<br>
echo "This is not a fedora host and release missing, use -R|--release to specify release"<br>
exit 1<br>
fi<br>
fi<br>
<br>
if [ "$(id -u)" != "0" ]; then<br>
echo "This script should be run as 'root'"<br>
exit 1<br>
fi<br>
<br>
rootfs_path=$path/$name/rootfs<br>
config_path=$default_path/$name<br>
cache=$cache_base/$release<br>
<br>
if [ -f $config_path/config ]; then<br>
echo "A container with that name exists, chose a different name"<br>
exit 1<br>
fi<br>
<br>
install_fedora<br>
if [ $? -ne 0 ]; then<br>
echo "failed to install fedora"<br>
exit 1<br>
fi<br>
<br>
configure_fedora<br>
if [ $? -ne 0 ]; then<br>
echo "failed to configure fedora for a container"<br>
exit 1<br>
fi<br>
<br>
copy_configuration<br>
if [ $? -ne 0 ]; then<br>
echo "failed write configuration file"<br>
exit 1<br>
fi<br>
<br>
if [ ! -z $clean ]; then<br>
clean || exit 1<br>
exit 0<br>
fi<br>
echo "container rootfs and config created"<br>
echo "container is configured for lxc.network.type=veth and lxc.network.link=virbr0 (which is default if you have libvirt runnig)"<br>
</blockquote>
<br>
<br>
</blockquote><div><br></div><div>Signed-off-by: Ramez Hanna <<a href="mailto:rhanna@informatiq.org">rhanna@informatiq.org</a>><br></div><div><br></div><div>diff --git a/templates/<a href="http://lxc-fedora.in">lxc-fedora.in</a> b/templates/<a href="http://lxc-fedora.in">lxc-fedora.in</a><br>
index f4f19c0..0262013 100644<br>--- a/templates/<a href="http://lxc-fedora.in">lxc-fedora.in</a><br>+++ b/templates/<a href="http://lxc-fedora.in">lxc-fedora.in</a><br>@@ -9,6 +9,7 @@<br> <br> # Authors:<br> # Daniel Lezcano <<a href="mailto:daniel.lezcano@free.fr">daniel.lezcano@free.fr</a>><br>
+# Ramez Hanna <<a href="mailto:rhanna@informatiq.org">rhanna@informatiq.org</a>><br> <br> # This library is free software; you can redistribute it and/or<br> # modify it under the terms of the GNU Lesser General Public<br>
@@ -24,117 +25,106 @@<br> # License along with this library; if not, write to the Free Software<br> # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br> <br>-DISTRO=fedora-10<br>+#Configurations<br>
+arch=$(arch)<br>+cache_base=/var/cache/lxc/fedora/$arch<br>+default_path=/var/lib/lxc<br>+root_password=rooter<br>+lxc_network_type=veth<br>+lxc_network_link=virbr0<br>+<br>+# is this fedora?<br>+[ -f /etc/fedora-release ] && is_fedora=true<br>
<br> configure_fedora()<br> {<br>- rootfs=$1<br>- hostname=$2<br> <br> # disable selinux in fedora<br>- mkdir -p $rootfs/selinux<br>- echo 0 > $rootfs/selinux/enforce<br>+ mkdir -p $rootfs_path/selinux<br>
+ echo 0 > $rootfs_path/selinux/enforce<br> <br> # configure the network using the dhcp<br>- cat <<EOF > $rootfs/etc/network/interfaces<br>-auto lo<br>-iface lo inet loopback<br>-<br>-auto eth0<br>-iface eth0 inet dhcp<br>
+ cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-eth0<br>+DEVICE=eth0<br>+BOOTPROTO=dhcp<br>+ONBOOT=yes<br>+HOSTNAME=${UTSNAME}<br>+NM_CONTROLLED=no<br>+TYPE=Ethernet<br>+MTU=${MTU}<br> EOF<br>
<br> # set the hostname<br>- cat <<EOF > $rootfs/etc/hostname<br>-$hostname<br>-EOF<br>- # set minimal hosts<br>- cat <<EOF > $rootfs/etc/hosts<br>-127.0.0.1 localhost $hostname<br>-EOF<br>
-<br>- # provide the lxc service<br>- cat <<EOF > $rootfs/etc/init/lxc.conf<br>-# fake some events needed for correct startup other services<br>-<br>-description "Container Upstart"<br>-<br>-start on startup<br>
-<br>-script<br>- rm -rf /var/run/*.pid<br>- rm -rf /var/run/network/*<br>- /sbin/initctl emit stopped JOB=udevtrigger --no-wait<br>- /sbin/initctl emit started JOB=udev --no-wait<br>-end script<br>
-EOF<br>-<br>- cat <<EOF > $rootfs/etc/init/console.conf<br>-# console - getty<br>-#<br>-# This service maintains a console on tty1 from the point the system is<br>-# started until it is shut down again.<br>-<br>
-start on stopped rc RUNLEVEL=[2345]<br>-stop on runlevel [!2345]<br>-<br>-respawn<br>-exec /sbin/getty -8 38400 /dev/console<br>+ cat <<EOF > ${rootfs_path}/etc/sysconfig/network<br>+NETWORKING=yes<br>+HOSTNAME=${UTSNAME}<br>
EOF<br> <br>- cat <<EOF > $rootfs/lib/init/fstab<br>-# /lib/init/fstab: lxc system fstab<br>-none /spu spufs gid=spu,optional 0 0<br>-none /tmp none defaults 0 0<br>
-none /var/lock tmpfs nodev,noexec,nosuid,showthrough 0 0<br>-none /lib/init/rw tmpfs mode=0755,nosuid,optional 0 0<br>+ # set minimal hosts<br>
+ cat <<EOF > $rootfs_path/etc/hosts<br>+127.0.0.1 localhost $name<br> EOF<br> <br>- # reconfigure some services<br>- if [ -z "$LANG" ]; then<br>- chroot $rootfs locale-gen en_US.UTF-8<br>- chroot $rootfs update-locale LANG=en_US.UTF-8<br>
- else<br>- chroot $rootfs locale-gen $LANG<br>- chroot $rootfs update-locale LANG=$LANG<br>- fi<br>-<br>- # remove pointless services in a container<br>- chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove<br>
-<br>- chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'<br>- chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'<br>
- chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'<br>- chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'<br>
- chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'<br>-<br>- echo "Please change root-password !"<br>- echo "root:root" | chroot $rootfs chpasswd<br>
+ sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit<br>+ sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit<br>+ chroot ${rootfs_path} chkconfig udev-post off<br>+ chroot ${rootfs_path} chkconfig network on<br>
+<br>+ dev_path="${rootfs_path}/dev"<br>+ rm -rf $dev_path<br>+ mkdir -p $dev_path<br>+ mknod -m 666 ${dev_path}/null c 1 3<br>+ mknod -m 666 ${dev_path}/zero c 1 5<br>+ mknod -m 666 ${dev_path}/random c 1 8<br>
+ mknod -m 666 ${dev_path}/urandom c 1 9<br>+ mkdir -m 755 ${dev_path}/pts<br>+ mkdir -m 1777 ${dev_path}/shm<br>+ mknod -m 666 ${dev_path}/tty c 5 0<br>+ mknod -m 666 ${dev_path}/tty0 c 4 0<br>+ mknod -m 666 ${dev_path}/tty1 c 4 1<br>
+ mknod -m 666 ${dev_path}/tty2 c 4 2<br>+ mknod -m 666 ${dev_path}/tty3 c 4 3<br>+ mknod -m 666 ${dev_path}/tty4 c 4 4<br>+ mknod -m 600 ${dev_path}/console c 5 1<br>+ mknod -m 666 ${dev_path}/full c 1 7<br>
+ mknod -m 600 ${dev_path}/initctl p<br>+ mknod -m 666 ${dev_path}/ptmx c 5 2<br>+<br>+ echo "setting root passwd to $root_password"<br>+ echo "root:$root_password" | chroot $rootfs_path chpasswd<br>
<br> return 0<br> }<br> <br> download_fedora()<br> {<br>- cache=$1<br>- arch=$2<br> <br> # check the mini fedora was not already downloaded<br>- mkdir -p "$cache/partial-$arch"<br>+ INSTALL_ROOT=$cache/partial<br>
+ mkdir -p $INSTALL_ROOT<br> if [ $? -ne 0 ]; then<br>- echo "Failed to create '$cache/partial-$arch' directory"<br>+ echo "Failed to create '$INSTALL_ROOT' directory"<br> return 1<br>
fi<br> <br> # download a mini fedora into a cache<br> echo "Downloading fedora minimal ..."<br>- febootstrap $DISTRO $cache/partial-$arch<br>+ YUM="yum --installroot $INSTALL_ROOT -y --nogpgcheck"<br>
+ PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils"<br>+ RELEASE_URL="<a href="http://ftp.funet.fi/pub/mirrors/fedora.redhat.com/pub/fedora/linux/releases/$release/Everything/$arch/os/Packages/fedora-release-$release-1.noarch.rpm">http://ftp.funet.fi/pub/mirrors/fedora.redhat.com/pub/fedora/linux/releases/$release/Everything/$arch/os/Packages/fedora-release-$release-1.noarch.rpm</a>"<br>
+ curl $RELEASE_URL > $INSTALL_ROOT/fedora-release-$release.noarch.rpm<br>+<br>+ mkdir -p $INSTALL_ROOT/var/lib/rpm<br>+ rpm --root $INSTALL_ROOT --initdb<br>+ rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm<br>
+ $YUM install $PKG_LIST<br>+<br> if [ $? -ne 0 ]; then<br> echo "Failed to download the rootfs, aborting."<br> return 1<br> fi<br> <br>- mv "$1/partial-$arch" "$1/rootfs-$arch"<br>
+ mv "$INSTALL_ROOT" "$cache/rootfs"<br> echo "Download complete."<br> <br> return 0<br>@@ -142,20 +132,23 @@ download_fedora()<br> <br> copy_fedora()<br> {<br>- cache=$1<br>- arch=$2<br>
- rootfs=$3<br> <br> # make a local copy of the minifedora<br>- echo -n "Copying rootfs to $rootfs ..."<br>- cp -a $cache/rootfs-$arch $rootfs || return 1<br>+ echo -n "Copying rootfs to $rootfs_path ..."<br>
+ #cp -a $cache/rootfs-$arch $rootfs_path || return 1<br>+ # i prefer rsync (no reason really)<br>+ mkdir -p $rootfs_path<br>+ rsync -a $cache/rootfs/ $rootfs_path/<br> return 0<br> }<br> <br>+update_fedora()<br>
+{<br>+ chroot $cache/rootfs yum -y update<br>+}<br>+<br> install_fedora()<br> {<br>- cache="/var/cache/lxc/fedora"<br>- rootfs=$1<br> mkdir -p /var/lock/subsys/<br> (<br> flock -n -x 200<br>@@ -164,19 +157,25 @@ install_fedora()<br>
return 1<br> fi<br> <br>- arch=$(arch)<br>-<br>- echo "Checking cache download in $cache/rootfs-$arch ... "<br>- if [ ! -e "$cache/rootfs-$arch" ]; then<br>- download_fedora $cache $arch<br>
+ echo "Checking cache download in $cache/rootfs ... "<br>+ if [ ! -e "$cache/rootfs" ]; then<br>+ download_fedora<br> if [ $? -ne 0 ]; then<br> echo "Failed to download 'fedora base'"<br>
return 1<br> fi<br>+ else<br>+ echo "Cache found. Updating..."<br>+ update_fedora<br>+ if [ $? -ne 0 ]; then<br>+ echo "Failed to update 'fedora base', continuing with last known good cache"<br>
+ else<br>+ echo "Update finished"<br>+ fi<br> fi<br> <br>- echo "Copy $cache/rootfs-$arch to $rootfs ... "<br>- copy_fedora $cache $arch $rootfs<br>+ echo "Copy $cache/rootfs to $rootfs_path ... "<br>
+ copy_fedora<br> if [ $? -ne 0 ]; then<br> echo "Failed to copy rootfs"<br> return 1<br>@@ -191,20 +190,21 @@ install_fedora()<br> <br> copy_configuration()<br> {<br>- path=$1<br>- rootfs=$2<br>
- name=$3<br> <br>- cat <<EOF >> $path/config<br>+ mkdir -p $config_path<br>+ cat <<EOF >> $config_path/config<br> lxc.utsname = $name<br>-<br> lxc.tty = 4<br> lxc.pts = 1024<br>-lxc.rootfs = $rootfs<br>
-lxc.mount = $path/fstab<br>-<br>-lxc.console = /dev/console<br>-<br>+lxc.rootfs = $rootfs_path<br>+lxc.mount = $config_path/fstab<br>+#networking<br>+lxc.network.type = $lxc_network_type<br>+lxc.network.flags = up<br>+lxc.network.link = $lxc_network_link<br>
+<a href="http://lxc.network.name">lxc.network.name</a> = eth0<br>+lxc.network.mtu = 1500<br>+#cgroups<br> lxc.cgroup.devices.deny = a<br> # /dev/null and zero<br> lxc.cgroup.devices.allow = c 1:3 rwm<br>@@ -223,10 +223,10 @@ lxc.cgroup.devices.allow = c 5:2 rwm<br>
lxc.cgroup.devices.allow = c 254:0 rwm<br> EOF<br> <br>- cat <<EOF > $path/fstab<br>-proc $rootfs/proc proc nodev,noexec,nosuid 0 0<br>-devpts $rootfs/dev/pts devpts defaults 0 0<br>
-sysfs $rootfs/sys sysfs defaults 0 0<br>+ cat <<EOF > $config_path/fstab<br>+proc $rootfs_path/proc proc nodev,noexec,nosuid 0 0<br>+devpts $rootfs_path/dev/pts devpts defaults 0 0<br>
+sysfs $rootfs_path/sys sysfs defaults 0 0<br> EOF<br> <br> if [ $? -ne 0 ]; then<br>@@ -239,7 +239,6 @@ EOF<br> <br> clean()<br> {<br>- cache="/var/cache/lxc/fedora"<br> <br> if [ ! -e $cache ]; then<br>
exit 0<br>@@ -253,7 +252,7 @@ clean()<br> exit 1<br> fi<br> <br>- echo -n "Purging the download cache..."<br>+ echo -n "Purging the download cache for Fedora-$release..."<br> rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1<br>
exit 0<br> <br>@@ -263,12 +262,23 @@ clean()<br> usage()<br> {<br> cat <<EOF<br>-$1 -h|--help -p|--path=<path> --clean<br>+usage:<br>+ $1 -n|--name=<container_name> <br>+ [-p|--path=<path>] [-c|--clean] [-R|--release=<Fedora_release>] [-A|--arch=<arch of the container>]<br>
+ [-h|--help] <br>+Mandatory args:<br>+ -n,--name container name, used to as an identifier for that container from now on<br>+Optional args:<br>+ -p,--path path to where the container rootfs will be created, defaults to /var/lib/lxc. The container config will go under /var/lib/lxc in and case<br>
+ -c,--clean clean the cache<br>+ -R,--release Fedora release for the new container. if the host is Fedora, then it will defaultto the host's release.<br>+ -A,--arch NOT USED YET. Define what arch the container will be [i686,x86_64]<br>
+ -h,--help print this help<br> EOF<br> return 0<br> }<br> <br>-options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")<br>+options=$(getopt -o hp:n:cR: -l help,path:,name:,clean,release: -- "$@")<br>
if [ $? -ne 0 ]; then<br> usage $(basename $0)<br> exit 1<br>@@ -282,6 +292,7 @@ do<br> -p|--path) path=$2; shift 2;;<br> -n|--name) name=$2; shift 2;;<br> -c|--clean) clean=$2; shift 2;;<br>+ -R|--release) release=$2; shift 2;;<br>
--) shift 1; break ;;<br> *) break ;;<br> esac<br>@@ -292,15 +303,23 @@ if [ ! -z "$clean" -a -z "$path" ]; then<br> exit 0<br> fi<br> <br>-type febootstrap<br>
+type yum >/dev/null 2>&1<br> if [ $? -ne 0 ]; then<br>- echo "'febootstrap' command is missing"<br>+ echo "'yum' command is missing"<br> exit 1<br> fi<br> <br> if [ -z "$path" ]; then<br>
- echo "'path' parameter is required"<br>- exit 1<br>+ path=$default_path<br>+fi<br>+<br>+if [ -z "$release" ]; then<br>+ if [ "$is_fedora" ]; then<br>+ release=$(cat /etc/fedora-release |awk '/^Fedora/ {print $3}')<br>
+ else<br>+ echo "This is not a fedora host and release missing, use -R|--release to specify release"<br>+ exit 1<br>+ fi<br> fi<br> <br> if [ "$(id -u)" != "0" ]; then<br>
@@ -308,21 +327,28 @@ if [ "$(id -u)" != "0" ]; then<br> exit 1<br> fi<br> <br>-rootfs=$path/rootfs<br>+rootfs_path=$path/$name/rootfs<br>+config_path=$default_path/$name<br>+cache=$cache_base/$release<br>
+<br>+if [ -f $config_path/config ]; then<br>+ echo "A container with that name exists, chose a different name"<br>+ exit 1<br>+fi<br> <br>-install_fedora $rootfs<br>+install_fedora <br> if [ $? -ne 0 ]; then<br>
echo "failed to install fedora"<br> exit 1<br> fi<br> <br>-configure_fedora $rootfs $name<br>+configure_fedora <br> if [ $? -ne 0 ]; then<br> echo "failed to configure fedora for a container"<br>
exit 1<br> fi<br> <br>-copy_configuration $path $rootfs $name<br>+copy_configuration<br> if [ $? -ne 0 ]; then<br> echo "failed write configuration file"<br> exit 1<br>@@ -332,3 +358,5 @@ if [ ! -z $clean ]; then<br>
clean || exit 1<br> exit 0<br> fi<br>+echo "container rootfs and config created"<br>+echo "container is configured for lxc.network.type=veth and lxc.network.link=virbr0 (which is default if you have libvirt runnig)"</div>