igor@0: #!/bin/sh
igor@0: 
igor@0: DYNAMIPS_BUILD=yes
igor@0: XENOMIPS_FS_BUILD=yes
igor@15: CONFIG_FS_BUILD=yes
igor@15: IOS_FS_BUILD=yes
igor@0: 
igor@7: XENOMIPS_DIR="files/"
igor@15: XENOMIPS_IMAGES_DIR="../xenomips-images/"
igor@15: IOS_DIR="ios/"
igor@15: SSH_OPEN_KEY=files/id_dsa.pub
igor@7: 
igor@0: #Xenomips FS
igor@0: XENOMIPS_FS_SIZE=2048k                  # size in 1k blocks
igor@7: XENOMIPS_FS_NAME=${XENOMIPS_IMAGES_DIR}/xenomips1.img
igor@0: 
igor@0: # Config FS
igor@7: CONFIG_FS_SIZE=1000k
igor@18: CONFIG_FS_NAME=${XENOMIPS_IMAGES_DIR}/xenomips-configXXX.img  # XXX will be automagically substitued with 
igor@18: CONFIG_FS_NUMBER=5                                            # the number of the Xenomips instance
igor@0: 
igor@0: # IOS FS
igor@0: IOS_FS_SIZE=2048k
igor@7: IOS_FS_NAME=${XENOMIPS_IMAGES_DIR}/xenomips-ios1.img
igor@0: 
igor@0: DEBIAN_MIRROR=http://debian.org.ua/debian
igor@0: DISTRO=sid
igor@18: PACKAGES="openssh-server libc6-xen libelf1 libpcap0.8 screen nfs-common quagga tcpdump"
igor@0: DYNAMIPS_SOURCES=http://www.ipflow.utc.fr/dynamips/dynamips-0.2.7.tar.gz
igor@0: DYNAMIPS_BUILD_DIR=dynamips/
igor@0: KERNEL_VERSION=`uname -r`
igor@0: 
igor@0: 
igor@0: WORK_DIR=${XENOMIPS_FS_NAME%%.img}
igor@7: WORK_DIR2=/tmp/xenomips-build
igor@0: 
igor@17: myecho()
igor@17: {
igor@17:     echo '**************************'
igor@17:     echo "$@"
igor@17:     echo '**************************'
igor@17: }
igor@17: 
igor@0: build_and_install_dynamips()
igor@0: {
igor@0:     OLD_PWD="`pwd`"
igor@20:     DESTDIR=${WORK_DIR}/usr
igor@0: 
igor@0:     if echo $DYNAMIPS_BUILD | grep -qi ^y
igor@0:     then
igor@0:         rm -rf ${DYNAMIPS_BUILD_DIR}/
igor@0:         mkdir -p ${DYNAMIPS_BUILD_DIR}/
igor@0:         cd ${DYNAMIPS_BUILD_DIR}/
igor@0:         wget $DYNAMIPS_SOURCES
igor@0:         tar xfz dynamips*tar*
igor@0:         cd dynamips*/
igor@0:         perl -p -i -e s@^PCAP_LIB=.*@PCAP_LIB=-lpcap@ Makefile
igor@0:         make
igor@0:     else
igor@0:         cd ${DYNAMIPS_BUILD_DIR}/dynamips*/
igor@0:     fi
igor@20:     make install DESTDIR=${DESTDIR}
igor@0:     cd ${OLD_PWD}
igor@0: }
igor@0: 
igor@18: myecho Preparing the Xenomips filesystem
igor@17: dd if=/dev/zero of=${XENOMIPS_FS_NAME} seek=${XENOMIPS_FS_SIZE} bs=1024 count=1
igor@0: mkfs.ext3 -F ${XENOMIPS_FS_NAME}
igor@7: mkdir -p ${WORK_DIR} ${WORK_DIR2}
igor@0: mount -o loop ${XENOMIPS_FS_NAME} ${WORK_DIR}
igor@0: debootstrap  ${DISTRO} ${WORK_DIR} ${DEBIAN_MIRROR} 
igor@0: cat <<SCRIPT | chroot ${WORK_DIR} sh -s
igor@0: unset LANG
igor@0: apt-get -y --force-yes install ${PACKAGES}
igor@0: /etc/init.d/ssh stop
igor@0: cat <<FSTAB > /etc/fstab
igor@11: proc            /proc           proc            defaults                0       0
igor@11: tmpfs           /tmp            tmpfs           defaults,nosuid,nodev   0       0
igor@11: 
igor@0: /dev/hda1       /                       ext3    ro              0       0
igor@0: /dev/hda2       /xenomips/ios           ext3    ro              0       0
igor@0: /dev/hda3       /xenomips/config        ext3    rw              0       0
igor@0: FSTAB
igor@0: mkdir -p /data/Cisco_IOS /xenomips
igor@0: 
igor@0: cat <<'INTERFACES' > /etc/network/interfaces
igor@0: auto lo eth0
igor@0: 
igor@0: iface lo inet loopback 
igor@0:         up for i in \`ifconfig -a | grep 'eth.*Link encap' | awk '{print \$1}'\`; do ifconfig \$i up ; done || true
igor@0: 
igor@13: #iface eth0 inet dhcp
igor@0: INTERFACES
igor@0: 
igor@0: echo /usr/local/bin/xenomips.sh > /etc/rc.local
igor@0: cd /etc/rcS.d; ln -s ../init.d/xenomips-init S35xenomips-init
igor@0: rm /etc/rc2.d/S*quagga
igor@0: 
igor@7: #perl -i -p -e s@C_PATH=/etc/quagga@C_PATH=/xenomips/etc/quagga@ /etc/init.d/quagga
igor@7: mv /etc/quagga /etc/quagga.ORIG
igor@7: ln -s /xenomips/config/etc/quagga /etc/quagga
igor@0: 
igor@7: mkdir -p /xenomips/{ios,config,dynamips} /var/lib/xenomips /root/.ssh
igor@0: 
igor@7: cat <<PROFILE >> /root/.profile
igor@7: TERM=linux
igor@0: screen -ls | grep -q Attached && screen -x || screen -ls | grep -q Detached && screen -r
igor@7: PROFILE
igor@0: SCRIPT
igor@0: 
igor@11: for kernel_version in ${KERNEL_VERSION} 2.6.18-without-PAE-4-xen-686
igor@11: do
igor@11:     cp -R /lib/modules/${KERNEL_VERSION} ${WORK_DIR}/lib/modules
igor@11: done
igor@0: 
igor@0: cp ${XENOMIPS_DIR}/xenomips-init ${WORK_DIR}/etc/init.d/
igor@0: cp ${XENOMIPS_DIR}/xenomips.sh ${WORK_DIR}/usr/local/bin
igor@0: sudo chmod +x  ${WORK_DIR}/usr/local/bin/xenomips.sh ${WORK_DIR}/etc/init.d/xenomips-init
igor@0: 
igor@0: cat ${SSH_OPEN_KEY} > ${WORK_DIR}/root/.ssh/authorized_keys
igor@0: chmod 600  ${WORK_DIR}/root/.ssh/authorized_keys
igor@0: 
igor@0: build_and_install_dynamips
igor@0: 
igor@9: mv ${WORK_DIR}/etc/quagga.ORIG ${WORK_DIR2}/quagga
igor@11: perl -i -p -e 's/=no/=yes/' ${WORK_DIR2}/quagga/daemons
igor@11: touch ${WORK_DIR2}/quagga/{zebra,bgpd,ospfd,ripd,ripngd,ospf6d,isisd}.conf
igor@0: umount ${WORK_DIR}
igor@0: 
igor@0: if  echo $CONFIG_FS_BUILD | grep -qi ^y
igor@0: then
igor@18:     myecho Preparing the Xenomips config filesystem
igor@0:     dd if=/dev/zero of=${CONFIG_FS_NAME} bs=1k count=${CONFIG_FS_SIZE}
igor@0:     mkfs.ext3 -F ${CONFIG_FS_NAME}
igor@14: 
igor@14:     mount -o loop ${CONFIG_FS_NAME} ${WORK_DIR}
igor@14:     mkdir -p ${WORK_DIR}/etc
igor@14:     mv ${WORK_DIR2}/quagga ${WORK_DIR}/etc
igor@14:     umount ${WORK_DIR}
igor@14: 
igor@18:     myecho Creating $CONFIG_FS_NUMBER copies of the Xenomips config filesystem
igor@18:     for i in `seq 1 $CONFIG_FS_NUMBER`
igor@18:     do
igor@18:         cp ${CONFIG_FS_NAME} `echo ${CONFIG_FS_NAME} | sed s/XXX/$i/`
igor@18:     done
igor@0: fi
igor@0: 
igor@0: 
igor@0: if echo $IOS_FS_BUILD | grep -qi ^y
igor@0: then
igor@18:     myecho Preparing the filesystem with the IOSes
igor@18:     dd if=/dev/zero of=${IOS_FS_NAME} bs=1k count=1 seek=${CONFIG_FS_SIZE}
igor@0:     mkfs.ext3 -F ${IOS_FS_NAME}
igor@0:     mount -o loop ${IOS_FS_NAME} ${WORK_DIR}
igor@0:     cp ${IOS_DIR}/* ${WORK_DIR}
igor@0:     umount ${WORK_DIR}
igor@0: fi
igor@0: