Good catch. In the first option there may be times in which the network services are up but your shared file system may not be ready. May I ask, how are you sharing the file system? NFS? Also what distro are you using?
I'm going to assume that you are using Centos/Rhel/Fedora since those are the distros I'm most familiar with. I'm also going to assume that you are using NFS to share the file system.
We can make the following changes to option 1 to check and make sure that nfs service is running on the VM:
Code:
#!/bin/bash
#put code to start VM here
#Now test if VM started using this code
if [ $? -eq 0 ]; then
VM=192.168.1.5
#Lets use the while loop to ping the server, if is pingable then we will mount the FS
while true; do
if ( ping -c 1 -w 1 $VM >/dev/null 2>&1 ); then
ssh root@$VM /sbin/service nfs status | grep running > /dev/null 2>&1 && MOUNT COMMAND GOES HERE && exit
fi
sleep 1
done
fi
The following line assumes that an ssh server is enabled on the VM, it will then check that nfs service is running, if it is it will mount your file system and then exit. In this case the ssh server is your VM and the host is the client.
Code:
if ( ping -c 1 -w 1 $VM >/dev/null 2>&1 ); then
ssh root@$VM /sbin/service nfs status | grep running > /dev/null 2>&1 && MOUNT COMMAND GOES HERE && exit
You will need to set up ssh-key authentication. On the ssh client do:
Code:
ssh-keygen
#assuming that you created an rsa key and that you are running the script as root, do the following
ln -s /root/.ssh/id_rsa.pub /root/.ssh/identity.pub
#Then copy the key to the VM:
ssh-copy-id VM
If ssh-copy-id does not work, you can manually copy the contents of /root/.ssh/id_rsa.pub from the host to /root/.ssh/authorized_keys on the VM. Make sure that authorized_keys has a 400 permission once you are done copying the keys. Also use an empty passphrase when creating the ssh-key pair.
As for option two. You are right, if you normally suspend your VM, option 2 will not work for you. I hope I didn't confuse you.