Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have written the following script which you may find useful if running Ubuntu on VirtualBox and make use of NetBeans. The scripts may even be used for other VirtualBox needs as it tries to:

  1. Install an application (NetBeans).
  2. Tests if Ubuntu is running with guest additions.
  3. Tries to mount a shared folder based on the default project name which typically remains the same on any system (host or guest).
  4. Writes the mount to rc.local for use friendliness.
  5. Creates and/or appends to a file in the users bin to allow the folder to be unmounted.

The script works flawlessly unless you upgrade. In such a scenario, multiple mounts will be done and the unmount script will not function as intended as there will be multiple entries in /etc/mtab.

This cannot be solved with use of umount flags like -f -l -a -t as typically this may result in all mounts being unmounted. umount -a -t vboxsf on the /target Is not the desired solution either as it may not be necessary to unmount all shared folders.

I have come up with a solution, but am new to bash/scripting and don't know how to implement it. My solution is to test rc.local for potential duplicate lines to avoid duplicate mounting:

#!/bin/bash
#Author: Yucca Nel http://thejarbar.org
#Will restart system
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH

#Modify these variables as needed...
tempWork=/tmp/work
startupScript=/etc/init.d/rc.local
defaultNetBeansVersion=7.0.1

echo "Provide NetBeans version (7.0.1 is default) then hit [Enter] :"
  read NetBeansVersion

  if [ -z "$NetBeansVersion" ]
    then
    $NetBeansVersion=$defaultNetBeansVersion
  fi

mkdir -p /$tempWork;
cd /$tempWork;

wget http://dlc.sun.com.edgesuite.net/netbeans/7.0.1/final/bundles/netbeans-$NetBeansVersion-ml-javase-linux.sh;
sh $tempWork/*sh;


#Add Netbeans launcher to your PATH. Doing so allows you to run 'netbeans' command from the terminal
#This line will need to be changed if you changed the default install location (IOW Netbeans is not in ~/)
sudo ln -f -s ~/netbeans-$NetBeansVersion/bin/netbeans /usr/bin/;

#If you use VirtualBox , you can share your projects between Host and guest. Name of shared
#folder must match 'NetBeansProjects'
mkdir -p $HOME/NetBeansProjects

if [ -f /sbin/mount.vboxsf ]
then
    sudo /sbin/umount /home/$HOME/NetBeansProjects
    sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects
fi

if mountpoint -q ~/NetBeansProjects
then
#Add it to the universal start script to automate process...
    sudo sed -ie '$d' $startupScript
    echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects"| sudo tee -a $startupScript
    echo "exit 0"| sudo tee -a $startupScript
    sudo chmod +x $startupScript

#Create a mount and unmount script file and add it to users local bin
    rm -rf $tempWork/*
    echo '#!/bin/bash' > $tempWork/netbeans-mount.sh
    echo '#!/bin/bash' > $tempWork/netbeans-umount.sh
    echo '#!/bin/bash' > $tempWork/mount-from-host.sh
    echo '#!/bin/bash' > $tempWork/unmount-from-host.sh
    echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" >> $tempWork/netbeans-mount.sh
    echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" >> $tempWork/mount-from-host.sh
    echo "sudo umount $HOME/NetBeansProjects" >> $tempWork/netbeans-umount.sh
    echo "sudo umount $HOME/NetBeansProjects" >> $tempWork/unmount-from-host.sh
    echo "exit 0" >> $tempWork/unmount-from-host.sh
    echo "exit 0" >> $tempWork/mount-from-host.sh
    echo "exit 0" >> $tempWork/netbeans-mount.sh
    echo "exit 0" >> $tempWork/netbeans-umount.sh

    sudo chmod +x $tempWork/*
    sudo mv -f $tempWork/*.sh /usr/local/bin
    rm -rf $tempWork
fi

#This function is used to cleanly exit with an error code.
function error_exit {
    sleep 7
    exit 1
}
#restart
sudo reboot
exit 0

Any pointers? My goal is to write a super scripts for Java developers that downloads most needed tools onto any Linux (not just Ubuntu) and mount potential things that don't need to be reinstalled if you have an existing development host. Things like Maven, Tomcat, SVN, JBoss don't need special guest install if they are already on host system and there are further benefits in combining different systems into one; e.g. Windows can run Photoshop and Safari browser, but Linux provides better customization and tools like ssh out the box.

share|improve this question
add comment

1 Answer 1

I am not sure if I understand... but here I count the number of unique lines from grep output.

grep "sudo /sbin/mount.vboxsf" /etc/rc.local | sort | uniq -c | wc -l

Two come from the echo lines, and two from the command lines. It should always equal four, yes?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.