Automatic provision of external HD windows partition to virtualbox windows vm

April 23, 2014

Following the previous post I wanted to make it possible to automatically provide the vfat partition I have to the windows VM. The best way to accomplish that was to use the shared folder abstraction from virtualbox together with the udev automounting. The solution is as follows:

  1. Create the correct /etc/fstab entry:
    UUID="XXXX-XXXX"                /media/external-win    vfat    rw,users,noauto,uid=YYYY,gid=ZZZZZ         0 0
    

    where the XXXX-XXXX is the UUID of the vfat partition found by blkid, YYYY and ZZZZ are the uid and gid of the user that is using it.

  2. Create the corresponding entries for the udev rules. (file /etc/udev/rules.d/10-externalhd.rules )
    KERNEL=="sd?2", ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idVendor}=="lllll", ATTRS{idProduct}=="mmmm", ATTRS{serial}=="kkkkklllllmmmmm", RUN+="/bin/mount /media/external-win", RUN+="/bin/su - user -c '/home/user/bin/windows-attach.sh start'"
    KERNEL=="sd?2", ACTION=="remove", SUBSYSTEMS=="usb", ATTRS{idVendor}=="lllll", ATTRS{idProduct}=="mmmm", ATTRS{serial}=="kkkkklllllmmmmm", RUN+="/bin/mount /media/external-win", RUN+="/bin/su - user -c '/home/user/bin/windows-attach.sh stop'"
    
    

    where user is the username you use. And the remove action is done to remove the folder when the HD is removed.

  3. The script that is run as the user. The script checks whether the windows vm is running  and if yes uses the sharedfolder virtual box command to attach the folder to the vm. It is saved under /home/user/bin/windows-attach.sh
    #!/bin/sh
    
    PATH=/bin:/usr/bin
    
    
    case "$1" in
        start)
    	vboxmanage list runningvms | grep 'windows-vm' > /dev/null
    	if [ $? -eq 0 ]
    	then 
    	    vboxmanage sharedfolder add windows-vm --name "external-win" --hostpath /media/external-win --transient --automount
    	fi
    	;;
        stop)
    	vboxmanage list runningvms | grep 'windows-vm' > /dev/null
    	if [ $? -eq 0 ]
    	then
    	    vboxmanage showvminfo windows-vm | grep '/media/external-win' > /dev/null
    	    if [ $? -eq 0 ]
    	    then
    		vboxmanage sharedfolder remove windows-vm --name "external-win" --transient
    	    fi
    	fi
    	;;
        *)
    	echo "Usage: $0 {start|stop}" >&2
    	exit 3
    	;;
    esac
    
    

This way in the windows vm when I plug the HD, I can get at NETWORK \\VBOXSRV\external-win the vfat partition. The automount feature doesn’t work as I would wish together with the the transient option, but perhaps having a script on windows to monitor the existence of this directory and provide the automatic mapping, could possibly even solve this situation.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: