How to setup a raspi VM using QEMU on OSX and use ssh and nfs. Extended from tinjaw's Gist. I decided to create a Repo instead of a Gist because I am expecting to add more stuff including host/guest related scripts or other source code over time.
Have home brew installed (see https://brew.sh/)
- Clone this repo
- On OSX host start setup script:
cd OSXraspi-emu
./setupRaspiEmu.sh
Grab a coffee/tea/beer because this step will take a while, this script downloads and installs all the necessary bits.
On OSX host
cd qemu-rpi
./runme
It everything worked out, you should see a QEMU application window with Raspbian booted up as well as a raspberry login prompt in the OSX console window in which you started runme
:
After the start of the Raspi VM, you can log into your Raspi by logging into either windows.
On your Raspi VM:
- Enter
sudo raspi-config
in a terminal window - Select 'Interfacing Options'
- Navigate to and select 'SSH'
- Choose 'Yes'
- Select 'Ok'
- Choose 'Finish'
To connect from the host to your Raspi VM:
ssh -p 5022 pi@localhost
The runme.sh script maps port 5022 on the host to port 22 on the Raspi VM. If you like to change it to a different port, just edit the line '-net user,hostfwd=tcp::5022-:22 -net nic' accordingly.
Remember, for a fresh Raspbian instance the default username is 'pi' and the default password is 'raspberry'. You probably want to change that soon.
-
Add the following line to /etc/exports:
/Users/tomr/Dev/Projects -network 127.0.0.0 -mask 255.255.240.0 -mapall=<username on OSX server>
-
/etc/nfs.conf add line nfs.server.mount.require_resv_port = 0
-
restart nfs server sudo nfsd restart
Notes:
-
QEMU VMs are starting user networking by default, see also https://wiki.qemu.org/Documentation/Networking#User_Networking_.28SLIRP.29. That means host-to-guest traffic happens through the loopback interface and the host's ports need to be mapped to ports on the guest (i.e. host:5022 is mapped to guest:22 for ssh). Guest-to-host traffic goes through 10.0.2.2, so if you try to access netwerk services on the host, go through 10.0.2.2 (i.e. you would access your OSX host from your Raspi VM via ssh 10.0.2.2).
-
The Rspi VM's nfs mount request comes from an unprivileged port, which is why OSX' nfs setup (/etc/nfs.conf) needs to be modified, see also https://superuser.com/questions/183588/nfs-share-from-os-x-snow-leopard-to-ubuntu-linux.
sudo apt-get install nfs-common -y
- edit /etc/exports as superuser and add the following line:
<share folder> -network 127.0.0.0 -mask 255.255.240.0 -mapall=<OSX user name>
<share folder>
= Path to the folder on the OSX host you would like to share with the Raspi VM
<OSX user name>
= user name on your OSX server
i.e.: /home/johndoe -network 127.0.0.0 -mask 255.255.240.0 -mapall=johndoe
- restart nfs daemon
sudo nfsd restart
sudo mount -t nfs -o rw 10.0.2.2:/Users/tomr/Dev/Projects /mnt
10.0.2.2 is the hosts IP address from your your Raspi VM.
On the Raspi VM add following line to /etc/fstab:
10.0.2.2:<share folder> <mount location> nfs rw 0 0
<share folder>
= Path to the folder on the OSX host you would like to share with the Raspi VM
<mount location>
= location on the Raspi VM where you would like to mount the OSX nfs share (i.e. /mnt
)
Tested out with Raspbian Stretch full