From a14202510b2f33a8a0d501d6434b7e94d277ed40 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Tue, 21 Jan 2014 17:37:51 +0100 Subject: [PATCH] use a simpler setup through virbr0 and dhcp for build-oriented containers --- lbuild-initvm.sh | 172 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 117 insertions(+), 55 deletions(-) diff --git a/lbuild-initvm.sh b/lbuild-initvm.sh index 3e7ba80d..29d6d4ba 100755 --- a/lbuild-initvm.sh +++ b/lbuild-initvm.sh @@ -21,6 +21,7 @@ COMMAND_LTEST="ltest-initvm.sh" ########## # when creating build boxes we use private NAT'ed addresses for the VMs # as per virbr0 that is taken care of by libvirt at startup +PRIVATE_BRIDGE="virbr0" PRIVATE_PREFIX="192.168.122." PRIVATE_GATEWAY="192.168.122.1" # beware that changing this would break the logic of random_private_byte... @@ -30,7 +31,7 @@ PRIVATE_MASKLEN=24 PRIVATE_ATTEMPTS=20 # constant -INTERFACE_BRIDGE=br0 +PUBLIC_BRIDGE=br0 # the network interface name as seen from the container VIF_GUEST=eth0 @@ -110,7 +111,7 @@ function create_bridge_if_needed() { set -x # already created ? - we're done - ip addr show $INTERFACE_BRIDGE >& /dev/null && { + ip addr show $PUBLIC_BRIDGE >& /dev/null && { echo "Bridge already set up - skipping create_bridge_if_needed" return 0 } @@ -154,20 +155,20 @@ function create_bridge_if_needed() { # creating the bridge - echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE" - brctl addbr $INTERFACE_BRIDGE - brctl addif $INTERFACE_BRIDGE $if_lan + echo "Creating bridge PUBLIC_BRIDGE=$PUBLIC_BRIDGE" + brctl addbr $PUBLIC_BRIDGE + brctl addif $PUBLIC_BRIDGE $if_lan echo "Activating promiscuous mode if_lan=$if_lan" ip link set $if_lan up promisc on sleep 2 # rely on dhcp to re assign IP.. - echo "Starting dhclient on $INTERFACE_BRIDGE" - dhclient $INTERFACE_BRIDGE + echo "Starting dhclient on $PUBLIC_BRIDGE" + dhclient $PUBLIC_BRIDGE sleep 1 #Reconfigure the routing table echo "Configuring gateway=$gateway" - ip route add default via $gateway dev $INTERFACE_BRIDGE + ip route add default via $gateway dev $PUBLIC_BRIDGE ip route del default via $gateway dev $if_lan # at this point we have an extra route like e.g. ## ip route show @@ -175,7 +176,7 @@ function create_bridge_if_needed() { #138.96.112.0/21 dev em1 proto kernel scope link src 138.96.112.57 #138.96.112.0/21 dev br0 proto kernel scope link src 138.96.112.57 #192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 - route_dest=$(ip route show | grep -v default | grep "dev $INTERFACE_BRIDGE" | awk '{print $1;}') + route_dest=$(ip route show | grep -v default | grep "dev $PUBLIC_BRIDGE" | awk '{print $1;}') ip route del $route_dest dev $if_lan echo "========== $COMMAND: exiting create_bridge - beg" @@ -209,36 +210,21 @@ function configure_fedora() { mkdir -p $rootfs_path/selinux echo 0 > $rootfs_path/selinux/enforce - # configure the network - - cat < ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-$VIF_GUEST -DEVICE=$VIF_GUEST -BOOTPROTO=static -ONBOOT=yes -HOSTNAME=$HOSTNAME -IPADDR=$IP -NETMASK=$NETMASK -GATEWAY=$GATEWAY -NM_CONTROLLED=no -TYPE=Ethernet -MTU=1500 -EOF - # set the hostname case "$fcdistro" in f18|f2?) cat < ${rootfs_path}/etc/hostname -$HOSTNAME +$GUEST_HOSTNAME EOF echo ;; *) cat < ${rootfs_path}/etc/sysconfig/network NETWORKING=yes -HOSTNAME=$HOSTNAME +HOSTNAME=$GUEST_HOSTNAME EOF # set minimal hosts cat < $rootfs_path/etc/hosts -127.0.0.1 localhost $HOSTNAME +127.0.0.1 localhost $GUEST_HOSTNAME EOF echo ;; esac @@ -560,6 +546,11 @@ function setup_lxc() { ;; esac + # rpm --rebuilddb + chroot $rootfs_path /bin/rpm --rebuilddb + + configure_yum_in_lxc $lxc $fcdistro $pldistro + # Enable cgroup -- xxx -- is this really useful ? mkdir $rootfs_path/cgroup @@ -572,9 +563,25 @@ function setup_lxc() { mkdir $rootfs_path/root/.ssh cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys - # copy libvirt xml template - tmpl_name="$lxc.xml" - cat > $config_path/$tmpl_name< $config_xml + write_guest_ifcfg_build > $guest_ifcfg + else + write_lxc_xml_test $lxc > $config_xml + write_guest_ifcfg_test > $guest_ifcfg + fi + + # define lxc container for libvirt + virsh -c lxc:// define $config_xml + + return 0 +} + +function write_lxc_xml_test () { + lxc=$1; shift + cat < $lxc 524288 @@ -597,7 +604,7 @@ function setup_lxc() { - + @@ -605,20 +612,71 @@ function setup_lxc() { host-bridge - + EOF +} - # define lxc container for libvirt - virsh -c lxc:// define $config_path/$tmpl_name - - # rpm --rebuilddb - chroot $rootfs_path /bin/rpm --rebuilddb +function write_lxc_xml_build () { + lxc=$1; shift + cat < + $lxc + 524288 + + exe + /sbin/init + + + + + 1 + + destroy + restart + destroy + + /usr/libexec/libvirt_lxc + + + + + + + + + + +EOF +} - configure_yum_in_lxc $lxc $fcdistro $pldistro +# this one is dhcp-based +function write_guest_ifcfg_build () { + cat <