#! /bin/sh
#
# Mount local filesystems and activate swap
#

source ${INIT_D:-/sbin/init.d}/functions

case "$1" in
  start)
     if [ ! -r /etc/fstab ]; then
         echo -n 'Cannot read /etc/fstab => cannot mount filesystems!'
         print_status failed
         exit 1
     fi
          
     need $checked_filesystems || exit_if_error

     echo 'Remounting root file system in read-write mode...'
     mount -n -o remount,rw /
     evaluate_retval || exit_if_failed
     
     #clear /etc/mtab and let mount rewrite it by -f(aking) the mount
     #from above. For the mount above, writing /etc/mtab was suppressed
     #with -n because root fs was still mounted read-only.
     if [ "z$USERSPACE_INIT" = "z" ]; then
       echo > /etc/mtab
       mount -f -o remount,rw /
     fi
     
     echo Adding swap space...
     dmesg -n6 #prevent message from swapon
     swapon -a
     evaluate_retval 
     dmesg -n7 #allow all messages again  
     
     echo 'Mounting local filesystems...'
     mount -a
     evaluate_retval || exit_if_failed
     ;;
  stop)
#The unmounting of filesystems is left to shutdown because it can
#guarantee that filesystems are not busy (it SIGKILLs every process)  
#     if [ ! -r /etc/fstab ]; then
#         echo -n 'Cannot read /etc/fstab => no filesystems will be unmounted!'
#	  print_status skipped
#         exit 0
#     fi
#     
#     echo 'Deactivating swap...'
#     swapoff -a
#     evaluate_retval
#     
#     #first fake the mount so that mtab is written properly as long
#     #as / still mounted rw
#     mount -f -o remount,ro /
#     
#     echo 'Unmounting local fs and remounting root read-only...'
#     umount -a -r
#     evaluate_retval
     ;;
  *)
     exit 1
     ;;
esac
    
exit 0


