The trouble with a Virtual Machine is that it’s, well, virtual. Sooner or later, you’re going to want to transfer some files back to the host.
Unfortunately, gedit seems to have a bit of a problem getting it’s head around this process. When you do try to change a file in the shared directory, gedit complains :
Could not save file path to file on shared folder Unexpected error: Error renaming temporary file : Text file busy
All is not lost however. Yes, this does appear to be a bug, but there is a solution that will enable you to continue using your favourite Gnome editor inside Virtual land.
For this you will need :
- a local folder on the Guest OS
- a shell script
- an alias in .bashrc
- some sticky-back plastic
OK, so I made that last one up.
The Cunning Plan
What we need to do is to create a local folder on the guest, which gedit will write to without complaint.
The shell script will simply sync files between the local folder and the shared folder. This should save us a lot of mucking about copying files manually.
First up, create the folder ( I did this under my home directory) :
mkdir share_local
Next step is to write the script.
Before we get into that, I recently discovered the notify-send utility which allows you to display a pop-up on the desktop from a shell script. It’s probably just a bit of programmer fluff for this particular exercise, but hey, I’ve decided to use it to display a pop-up notifcation once the synchronization is completed.
To use this utility, you need the notify-bin package :
sudo apt-get install libnotify-bin
Now for the script itself. We can either pass in an argument on the command line, or wait for the prompt ( depending on whether we remember which argument synchs which way)…
#!/bin/sh
#
# Set the local and host directory locations here so we only have one place
# to look if we ever need to change them
#
localDir=$HOME/share_local
hostShare=$HOME/Desktop/host_share
#
# set the icon we're going to use for the notification
#
iconPath="/usr/share/icons/gnome/32x32/status/"
infoIcon="info.png"
errIcon="error.png"
# Initially, we'll assume it's all going to go without a hitch so initialize the icon to info.png
icon=$infoIcon
#
# We can either
# 1) sync all the files from local to host (h) or
# 2) from host to local (l)
# Check to see if we've got an argument on the command line. If not then
# prompt for one
#
if [ -z "$1" ]; then
echo "Enter 'h' to sync to the host share or 'l' to sync to the local share : "
read direction
else
direction=$1
fi
# Check the direction we're synching to
if [ "$direction" = "l" ]; then
cmd="cp -r $hostShare/* $localDir"
msg="Files in $hostShare copied to $localDir"
elif [ "$direction" = "h" ]; then
cmd="cp -r $localDir/* $hostShare"
msg="Files in $localDir copied to $hostShare"
else
# Invalid or no option specified
echo "Valid options are 'h' or 'l'"
exit 1;
fi
# Run the command
$cmd
# Was the command successful (i.e. exit status of 0) ?
if [ $? -ne 0 ]; then
msg="Error synching files"
icon=$errIcon
fi
# Issue the notification with the appropriate message and icon
notify-send -t 1000 -i "$iconPath/$icon" 'Share Synch' "$msg"
exit 0
All fairly straightforward – define the shared folder and it’s local equivalent and then issue a command to copy everything from one to the other.
Oh, and display a fluffy little notification once you’re done.
If you want to make life slightly simpler for yourself, you could add an alias to your .bashrc so that you can just run it in terminal – without having to remember where you put it :
alias sharesync=$HOME/scripts/sharesync.sh
As you can see, I’ve saved the file in $HOME/scripts. Just replace this with the path where you’ve saved the script and you’re all set.
If you’re feeling really lazy, you could just add two menu options to the Appllications Menu ( one for each runtime switch), so you don’t even have to open a terminal to run it. Check out this post if you fancy having a go.
Tags: bash, gedit, notify-send, VirtualBox
December 16, 2011 at 7:08 pm |
An easier make sure the ‘Create backup copy’
editor mode is checked,
and do the save twice (ignoring the error on the first…)
December 29, 2011 at 7:43 pm |
Thanks for posting about this!!