Brief Overview
I have Ubuntu 9.10 as 64-bit Guest OS running on top of Windows 7 as 64-bit Host OS using VirtualBox 3.1.2. Now, I have also added 3 folders from Windows Filesystem for sharing with Guest OS. As you know, I have to mount the folders by running commands in Terminal for accessing data in shared folders. So, I have to do it manually every time I start the VM.
Latest Updates
Chances are you might not be able to run the following scripts without root privileges, as commands like mount need root privileges. So, refer this blog entry for more details to solve the problem.
Problem Description
I was looking forward to automate this and I came across “System > Preferences > Startup Applications” in my Guest OS. However, I need to run the mount command 3 times for 3 shared folders I have. So, I wrote the following scripts to automate the task and assigning just one script in “Startup Applications”.
Also, I need this because I have created playlists for videos (Movie Player/VLC) and music (Audacious 2), also set desktop background of my choice. These files are only available in shared folders. Now, using single startup script, I would be able to make a copy of music, pictures, videos, etc. every time I login with specific account after starting VM.
Getting Started
To do the needful, I came up with the following script, which would run two more scripts in background, when I login with specific user account. Let’s get started;
share_hostos_data.sh
#!/bin/sh
nohup $HOME/mnt_cp_hostos_data.sh A B C > /dev/null
nohup $HOME/unmnt_hostos_data.sh A B C > /dev/null
Now, that we have startup script ready that will first mount shared folders and then, unmount them. This seems to be stupid if you don’t go below for more details about those two scripts.
Mount Shared Folders and Copy Them Recursively
Now, the following script will accept N arguments where each argument will be the name assigned to shared folder path in Host OS. So, the script will not run with ZERO arguments and it will make the directory under /media/win7-share in Guest OS with the argument name and then, mount the folder before copying the data in the Public directory of specific account’s $HOME.
mnt_cp_hostos_data.sh
#!/bin/sh
if [ $# -eq 0 ]; then
echo "Exiting.." > /dev/null
exit 0
fi
for dname in $@
do
SHARE_DIR=/media/win7-share/$dname
if [ ! -d ${SHARE_DIR} ]; then
mkdir ${SHARE_DIR}
if [ $? -eq 0 ]; then
echo "${SHARE_DIR} created successfully.."
else
exit 1
fi
fi
mount -t vboxsf $dname ${SHARE_DIR}
if [ $? -eq 0 ]; then
echo "Mounted $dname successfully.."
else
exit 1
fi
cp -nr --target-directory=$HOME/Public/windows7/ ${SHARE_DIR}
if [ $? -eq 0 ]; then
echo "Copied data into $HOME/Public/windows7/$dname successfully.."
else
exit 1
fi
done
Unmount Shared Folders..
The following script also accepts N arguments where each argument will be the name assigned to shared folder path in Host OS. So, the script will not run with ZERO arguments and it is just going to unmount the directory made the directory under /media/win7-share in Guest OS with the argument name.
unmnt_hostos_data.sh
#!/bin/sh
if [ $# -eq 0 ]; then
echo "Exiting.." > /dev/null
exit 0
fi
for dname in $@
do
SHARE_DIR=/media/win7-share/$dname
if [ -d ${SHARE_DIR} ]; then
umount -f ${SHARE_DIR}
if [ $? -eq 0 ]; then
echo "Unmounted ${SHARE_DIR} successfully.."
else
exit 1
fi
fi
done
Work Ahead
As you might have understood by know the shared folders names are A, B, C (see share_hostos_data.sh). That’s something I had to hardcode inside the script for passing them as arguments to mnt_cp* and unmnt* scripts. I ran this command VBoxMange showvminfo “<VM-Name>” in my Host OS command prompt, to find out the names of shared folders.
If I could access the names somehow, then I can pick up these names and supply them as an argument to the necessary scripts created above. That’s all for today, have a great time..