#!/bin/sh # Find a set of image files and start Zope and MySQL. # # chkconfig: 2345 40 60 # description: ERP5 LiveCD Utility # ### BEGIN INIT INFO # Provides: erp5cdtool # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Should-Start: $network harddrake memcached # Short-Description: ERP5 LiveCD Utility # Description: Find a set of image files and start Zope and MySQL ### END INIT INFO # The function of this script is the followings: # # - Set up a local disk as a storage # - Control MySQL and Zope # # ERP5 LiveCD requires a local storage for MySQL, Zope and a swap file. # If this uses only memory as a storage, the memory will be exhausted very easily, # because the databases grow and grow, and Zope require much memory. So it needs # a disk to run on ordinary PCs, considering that they usually have only 256MB memory. # # ERP5 LiveCD does not start MySQL and Zope from /etc/rc.d/rc5.d automatically to make # sure that they start after mounting a local disk and they stop before unmounting the local # disk. So MySQL and Zope are started and stopped within this script. # Source function library. . /etc/rc.d/init.d/functions ERP5_DIR='/mnt/erp5_cd' MOUNT_DIR='/mnt/storage' MKFS=/sbin/mkfs.xfs NAME="erp5cdtool" # This is copied from GNU GRUB. Copyrighted by the Free Software Foundation, Inc. resolve_symlink () { tmp_fname=$1 # Resolve symlinks while test -L $tmp_fname; do tmp_new_fname=`ls -al $tmp_fname | sed -n 's%.*-> \(.*\)%\1%p'` if test -z "$tmp_new_fname"; then echo "Unrecognized ls output" 2>&1 exit 1 fi # Convert relative symlinks case $tmp_new_fname in /*) tmp_fname="$tmp_new_fname" ;; *) tmp_fname="`echo $tmp_fname | sed 's%/[^/]*$%%'`/$tmp_new_fname" ;; esac done # Canonicalize the name. tmp_IFS=$IFS IFS=/ set $tmp_fname IFS=$tmp_IFS tmp_new_fname="" while test $# -ne 0; do if test "x$1" = x -o "x$1" = x.; then : elif test "x$1" = x..; then tmp_new_fname=`echo $tmp_new_fname | sed -e 's|\(.*\)/.*|\1|'` else tmp_new_fname="$tmp_new_fname/$1" fi shift done echo "$tmp_new_fname" } # Get the list of disks in this computer. get_disks() { for dev in /dev/hd[a-z] /dev/sd[a-z]; do # If no /dev/sd* exist $dev="/dev/sd[a-z]" and same for hd*, so test it if test "$dev" != "/dev/hd[a-z]" && test "$dev" != "/dev/sd[a-z]"; then if test -e $dev; then disk_name=`echo $dev | cut -d '/' -f 3` media_info="/proc/ide/$disk_name/media" if test -e $media_info; then media_type=`cat $media_info` if test "x$media_type" = xdisk; then echo $dev fi # TODO: find a good way to test the media type of /dev/sdaX devices # Idea: we can use HAL. See http://lists.freedesktop.org/archives/hal/2005-October/003495.html else echo $dev fi fi fi done } # Get the list of partitions in a disk. get_partitions() { /sbin/fdisk -l $1 | sed -n '\|^/dev/|{s/\([^ ]*\).*/\1/;p}' } # Get the partition id of a given partition. To ease the programming, the hex value is returned # only with lower case. get_id() { partition=$1 disk=`echo $partition | sed 's/\(.*\)[0-9][0-9]*/\1/'` /sbin/fdisk -l $disk | grep ^$partition | awk '{ print ($2 == "*" ? $6 : $5 ); }' | tr 'A-Z' 'a-z' | sed -e 's/0\([0-9a-z]\)/\1/' } # Return the available space of a given filesystem with kilobyte as a unit. get_available_space() { df -k -P $1 | awk '{ print $4; }' | tail -1 } # Prepare image files. prepare() { dir=$1 # Set up a loop filesystem. fsfile=$dir/fs.img csum=`dd if=$fsfile bs=512 count=1 | md5sum | cut -f1 -d' '` if test $csum = bf619eac0cdf3f68d496ea9344137e8b; then $MKFS $fsfile fi /sbin/losetup /dev/loop1 $fsfile || { echo "Unable to activate loop device /dev/loop1"; failure; }; mount -o noatime,rw /dev/loop1 $ERP5_DIR # Set up a swap file. swapfile=$ERP5_DIR/swap.img dd if=/dev/zero of=$swapfile bs=1M count=256 /sbin/mkswap $swapfile && /sbin/swapon $swapfile } # Set up the already mounted partition. Note that this is called even when an already initialized # partition is mounted. So this must not destory the MySQL database and the Zope installation # unnecessarily. setup() { # Recover a configuration. if test -f $ERP5_DIR/config.xml; then cp -f $ERP5_DIR/config.xml /root/ /root/autoconf.py nexedi master fi if test -f $ERP5_DIR/config.tgz; then (cd /; tar zxpvf $ERP5_DIR/config.tgz) fi if test -f $ERP5_DIR/config.sh; then (cd /; /bin/sh $ERP5_DIR/config.sh) fi # Set up MySQL. test -d $ERP5_DIR/mysql || tar zxpf /usr/share/erp5cdtool/mysql.tgz -C $ERP5_DIR test -e /var/lib/mysql && rm -rf /var/lib/mysql chown -R mysql.mysql $ERP5_DIR/mysql ln -sf $ERP5_DIR/mysql /var/lib/mysql /etc/rc.d/init.d/mysqld-max start # Set up Zope. test -d $ERP5_DIR/zopelog || mkdir $ERP5_DIR/zopelog chown -R zope.zope $ERP5_DIR/zopelog test -e /var/log/zope && rm -rf /var/log/zope ln -sf $ERP5_DIR/zopelog /var/log/zope test -d $ERP5_DIR/zope || tar zxpf /usr/share/erp5cdtool/zope.tgz -C $ERP5_DIR chown -R zope.zope $ERP5_DIR/zope test -e /var/lib/zope && rm -rf /var/lib/zope ln -sf $ERP5_DIR/zope /var/lib/zope /etc/rc.d/init.d/zope start # Make a lock file. touch /var/lock/subsys/erp5cdtool } # Start this script. start() { gprintf "Starting ${NAME}: " if test -f /var/lock/subsys/erp5cdtool; then # Make sure not to be started twice. gprintf "Already running." exit 1 fi test -d $MOUNT_DIR || mkdir $MOUNT_DIR test -d $ERP5_DIR || mkdir $ERP5_DIR # Get some information. linux_partitions='' windows_partitions='' for disk in `get_disks`; do for partition in `get_partitions $disk`; do # Make sure that this partition is not mounted. This is necessary because the initrd produced by mklivecd # mounts some partitions automatically. cat /proc/mounts | grep "^$partition" | cut -f2 -d' ' | xargs -r umount id=`get_id $partition` # 6 = FAT16, b = W95 FAT32, c = W95 FAT32 (LBA), e = W95 FAT16 (LBA) # 7 = HPFS/NTFS # 83 = Linux if test $id = 6 -o $id = b -o $id = c -o $id = e -o $id = 7; then if test -n "$windows_partitions"; then windows_partitions="$windows_partitions $partition" else windows_partitions=$partition fi elif test $id = 83 -o $id = fd; then if test -n "$linux_partitions"; then linux_partitions="$linux_partitions $partition" else linux_partitions=$partition fi fi done done # Load the NTFS module explicitly for write support. /sbin/modprobe -r ntfs /sbin/modprobe ntfs ntfs_write_support=1 # This need a custom kernel: # diff -ru ./tmb-mdk/kernel/2.6.7-q2.tmb6/configs/i386.config ./tmb-nxd/kernel/2.6.7-q2.tmb6/configs/i386.config # --- ./tmb-mdk/kernel/2.6.7-q2.tmb6/configs/i386.config 2004-08-29 09:35:45.000000000 +0200 # +++ ./tmb-nxd/kernel/2.6.7-q2.tmb6/configs/i386.config 2004-12-26 01:08:10.000000000 +0100 # @@ -2736,7 +2736,7 @@ # CONFIG_VFAT_FS=m # CONFIG_NTFS_FS=m # # CONFIG_NTFS_DEBUG is not set # -# CONFIG_NTFS_RW is not set # +CONFIG_NTFS_RW=y # # # # # Pseudo filesystems if test -n "$linux_partitions"; then # Find an erp5 partition. for partition in $linux_partitions; do if mount -o ro $partition $MOUNT_DIR; then if test -f $MOUNT_DIR/.erp5cd; then # Found. . $MOUNT_DIR/.erp5cd umount $MOUNT_DIR break else umount $MOUNT_DIR fi fi done if test "x$storage_name" = "x"; then true else # Detect RAID devices automatically. if echo $storage_name | grep -q md; then echo 'DEVICE /dev/hd*[0-9] /dev/sd*[0-9]' > /etc/mdadm.conf /sbin/mdadm --examine --scan --config=/etc/mdadm.conf >> /etc/mdadm.conf /sbin/mdadm --assemble --scan fi if mount -o rw,noatime $storage_name $ERP5_DIR; then setup return fi fi fi if test -n "$windows_partitions"; then # Find an erp5cd directory. for partition in $windows_partitions; do if mount -o rw,noatime $partition $MOUNT_DIR; then # This part should be improved. if test -d $MOUNT_DIR/erp5cd -a -f $MOUNT_DIR/erp5cd/fs.img; then # Found. prepare $MOUNT_DIR/erp5cd setup return elif test -d $MOUNT_DIR/erp5cd/erp5cd -a -f $MOUNT_DIR/erp5cd/erp5cd/fs.img; then # Found. This is a typical mistake for Windows users. prepare $MOUNT_DIR/erp5cd/erp5cd setup return fi umount $MOUNT_DIR/erp5cd fi done fi if test -n "$linux_partitions"; then # Find an erp5cd directory. for partition in $linux_partitions; do if mount -o rw,noatime $partition $MOUNT_DIR; then # This part should be improved. if test -d $MOUNT_DIR/erp5cd; then # Found. test -f $MOUNT_DIR/erp5cd/fs.img || dd if=/dev/zero of=$MOUNT_DIR/erp5cd/fs.img bs=1M count=512 prepare $MOUNT_DIR/erp5cd setup return fi umount $MOUNT_DIR fi done fi ### Disable the automatic creation of the directory erp5cd. #if test -n "$linux_partitions"; then # # Create a new erp5cd directory in a partition with enough space. # for partition in $linux_partitions; do # if mount -o rw,noatime $partition $MOUNT_DIR; then # if test `get_available_space $MOUNT_DIR` -gt `expr 512 \* 1024`; then # # Good enough. # mkdir $MOUNT_DIR/erp5cd # dd if=/dev/zero of=$MOUNT_DIR/erp5cd/fs.img bs=1M count=512 # prepare $MOUNT_DIR/erp5cd # setup # return # fi # umount $MOUNT_DIR # fi # done #fi # Failed. exit 1 } # Stop this script. Quite simple. Just stop services and unmount the filesystem. stop() { gprintf "Stopping ${NAME}: " /etc/rc.d/init.d/zope stop /etc/rc.d/init.d/mysqld-max stop cat /proc/swaps | awk '{print $1;}' | grep $ERP5_DIR | xargs /sbin/swapoff umount $ERP5_DIR if cat /proc/mounts | grep -q $MOUNT_DIR; then /sbin/losetup -d /dev/loop1 umount $MOUNT_DIR fi # Remove the lock file. rm -f /var/lock/subsys/erp5cdtool } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; *) gprintf "Usage: %s {start|stop|restart}\n" "$0" exit 1 esac