#!/bin/sh
# aginies ___ateu_ mdv.com
# quick script to setup a cluster chroot
# and create a dolly image
# first arg can be the name of wanted RPMS

# path to the chroot
CHROOT=/root/chroot_test
HOSTNAME=`hostname`

# full path to get the rescue image
RESCUE=/var/install/cluster/install/stage2/rescue.clp
# temp ISO
TISO=/tmp/t.iso
# where to mount temp ISO
TLOOP=/tmp/tiso

# image and path to image
img=diskless_node.img
PATH_MOUNT=/tmp/diskless_node

# mandatory RPMS !
MANDATORY="net-tools passwd clusterscripts-client kernel-smp-2.6.12.26mdk-1-1mdk lam mpich openssh-server ganglia-core"

mkdir -p $TLOOP $PATH_MOUNT $CHROOT

case $1 in
    create|c)
	shift	
	;;
    *)
    	echo " Usage:"
	echo " $0 create package_name package_name2"
	echo
	echo "Auto installed RPMS are:"
	echo $MANDATORY
	exit 1
	;;
esac

# first arg is list of wanted RPMS
WANTED=$*


clean_chroot() {
    echo " - cleaning old chroot"
    if [ ! $CHROOT ];then exit 1; fi
    rm -rf $CHROOT/*
}

need_rescue() {
    echo " - needed a basesystem based on rescue"
    if [ ! -f "/usr/bin/extract_compressed_fs" ]; then urpmi cloop ; fi
    extract_compressed_fs $RESCUE > $TISO
    mount -o loop $TISO $TLOOP
    
    cp -af $TLOOP/* $CHROOT/
    echo " - Copy existing /dev"
    cp -af /dev/* $CHROOT/dev/
    umount $TLOOP
}

install_rpm() {
    echo " - install wanted RPMS"
    echo "mandatory RPMS: $MANDATORY"
    echo "user choice: $WANTED"
    urpmi --auto --ignoresize --split-length 0 --excludedocs --no-verify-rpm --root $CHROOT $MANDATORY $WANTED
}

adjust_scripts() {
    echo " - special rc.sysinit and go scripts"
    if [ ! -f "/etc/rc.sysinit_diskless" ];then echo "can't find /etc/rc.sysinit_diskless !" ; exit 1; fi
    cp -avf /etc/rc.sysinit_diskless $CHROOT/etc/rc.sysinit
    chmod 755 $CHROOT/etc/rc.sysinit
    cp -avf /etc/profile $CHROOT/etc/profile
    echo "clear" >> $CHROOT/etc/profile
    echo 'export PS1="[\u@\h \W]\$ "' >> $CHROOT/etc/profile
    echo "echo  --                       --- " >> $CHROOT/etc/profile
    echo "echo  -- CLUSTER diskless mode --- " >> $CHROOT/etc/profile
    echo "echo  --                       --- " >> $CHROOT/etc/profile
    echo "echo" >> $CHROOT/etc/profile
    

    cat >$CHROOT/ka/go<<EOF
#!/bin/sh
service portmap restart
setup_client_cluster.pl doall
service ypbind restart
service autofs restart
service authd restart
service gmond restart
service xinetd restat
service sshd restart
EOF

    echo "SWAP=\$(lsparts | grep swap | cut -d ":" -f 1)" >> $CHROOT/ka/go
    echo "swapon -v /dev/\$SWAP" >> $CHROOT/ka/go

    chmod 755 $CHROOT/ka/go
    mkdir $CHROOT/root
    cp -avf /root/.bashrc $CHROOT/root
    mkdir -p $CHROOT/var/lock/subsys/
    touch $CHROOT/etc/redhat-release
# can connect on nodes with empty password
    echo "PermitEmptyPasswords yes" >> $CHROOT/etc/ssh/sshd_config
    chroot $CHROOT /usr/sbin/useradd nobody -d /tmp/nobody -u 1285
    chroot $CHROOT /usr/sbin/groupadd -g 100 all
    chroot $CHROOT pwconv
    chmod 777 $CHROOT/tmp
}

remove_X() {
    echo
    echo " ??? - Do you want to remove all X componments ? (y/n)"
    read $AN
    if [ -f "$CHROOT/usr/X11R6/bin/mpi-x-povray" ]; then AN="no" ;fi
    if [ -f "$CHROOT/usr/bin/blender" ]; then AN="no" ;fi
    if [ $AN == "y" ]; then
	echo " "
	rm -rf $CHROOT/usr/X11R6
        rm -rf $CHROOT/etc/X11
    fi
}


reduce_chroot_size() {
    echo " - try to reduce the size of the chroot"
    rm -rf $CHROOT/usr/share/doc
    rm -rf $CHROOT/boot/*
    rm -rf $CHROOT/core*
    rm -rf $CHROOT/lib/modules/2.6.12-26mdksmp/kernel/3rdparty
    rm -rf $CHROOT/lib/modules/2.6.12-26mdksmp/kernel/sound
    rm -rf $CHROOT/lib/grub
    rm -rf $CHROOT/usr/share/man
    rm -rf $CHROOT/usr/share/mdk/backgrounds
    rm -rf $CHROOT/usr/share/gtk-doc/html/pango/*.png
    rm -rf $CHROOT/usr/share/gtk-doc/html
    rm -rf $CHROOT/usr/lib/perl5/5.8.7/unicore/*.txt
    remove_X
}

remove_rpm() {
    echo " - remove unwanted RPM"
    chroot $CHROOT rpm -e --nodeps -a \
	desktop-common-data mandriva-theme-screensaver \
	qiv mkinitrd libpango1.0_0 libcairo2-1.0.0 libatk1.0_0 libgpm1 \
	rpm-mandriva-setup popt-data chkconfig cloop-utils smartmontools mandriva-theme \
	bootsplash wireless-tools sound-scripts gcc-cpp aumix-text soundwrapper mdk-menu-messages \
	clone urpmi fbgrab gexec authd
}

clean_rpmdatabase() {
    echo " - now we can clean the RPM database"
    rm -rf $CHROOT/usr/share/rpm-helper
    rm -rf $CHROOT/usr/lib/rpm
    rm -rf $CHROOT/var/lib/rpm
}

chroot_size() {
    echo " - size of chroot:"
    du -sh $CHROOT
}

time_to_change() {
    echo 
    echo
    echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
    echo " If you want to change something in the chroot, do it now"
    echo $CHROOT
    echo "Else just press [ENTER]"
    echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
    read
}

create_image() {
    echo " - create image $img"
    SIZE=`du -sh $CHROOT | cut -d "M" -f 1`
    
    dd if=/dev/zero of=$img count=${SIZE}900 bs=1024
    
    echo " - format image $img"
    mkfs.ext2 -m 0 -L "cluster diskless" $img

    echo " - mount loop $img"
    mount -o loop $img $PATH_MOUNT
    
    echo " - copy all data to image"
    cp -a $CHROOT/* $PATH_MOUNT

    echo " - umount $img"
    umount $PATH_MOUNT
    
    echo " - size of $img"
    du -h $img

    echo " - if you want to edit your diskless image, just mount it loop:"
    echo "mount -o loop $img $PATH_MOUNT"
}

end() {
    echo " - create dolly conf like this one:"
    echo "----------------------------"
    echo "infile /root/$img
outfile /dev/ram3
server $HOSTNAME
firstclient 12.12.12.1
lastclient 12.12.12.3
clients 3
12.12.12.1
12.12.12.2
12.12.12.3
endconfig"
    echo "----------------------------"

    echo
    echo " - Set your PXE server default boot to dolly"
    echo " ! DONT forget to adjust the ramsize parameter !"
    echo 
    echo " - now launch dolly to copy diskless image on nodes:"
    echo "----------------------------"
    echo "dolly -v -s -f dolly.cfg"
    echo "----------------------------"
    echo
}


# main
clean_chroot
need_rescue
install_rpm
adjust_scripts
reduce_chroot_size
remove_rpm
clean_rpmdatabase
time_to_change
chroot_size
create_image
end
