Mounting a shared folder on ArchLinux VM

For this tutorial, we’ll be using Samba. As for what Samba really is though, the most I can figure out from my search on Google is that Samba is a free and open-source software that allows files to be shared across Windows and Linux systems simply and easily. To be exact, it is an open-source implementation of the SMB/CIFS protocol.

Setup

Install smbclient util with pacman

1
sudo pacman -S smbclient

Create shared directory

1
sudo mkdir /home/<user>/share

This directory is to be shared between the host and guest machines.

Edit the Samba configuration file

Open up the configuration file for samba:

1
sudo nano /etc/samba/smb.conf

Add these lines to the configuration file:

1
2
3
4
[share]
path = /home/<user>/share
read only = no
guest ok = yes

Create Samba password

1
sudo smbpasswd -a <user>

Restart the Samba service to refresh:

1
sudo systemctl restart smb.service

IP Configuration

In the UTM app on your MacBook, go to the settings for the ArchLinux VM and enable the “Port Forwarding” option. Add a new rule with the following settings:

Protocol: TCP
Host Port: 445
Guest IP: IP address of the ArchLinux VM
Guest Port: 445

To get Guest IP, run the following line:

1
[root@alarm ~]# ip addr | grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host 
inet 192.168.65.3/24 metric 1024 brd 192.168.65.255 scope global dynamic enp0s1
inet6 fde3:e977:b4d3:5c13:e0b4:51ff:fee5:2142/64 scope global dynamic mngtmpaddr noprefixroute 
inet6 fe80::e0b4:51ff:fee5:2142/64 scope link

In this case, it’s 192.168.65.3.

To get Host IP, run the following line:

1
2
3
ipconfig getifaddr en0

# 10.8.189.52

On your MacBook, open Finder and click “Go” > “Connect to Server” (Cmd + K).

Enter this address: smb://localhost:445/share

Enter the Samba username and password when prompted.

You can now access the shared directory and transfer files between the host and guest operating systems.

Mount /mnt/share

Normally during the boot-up process of our machines, the mounting up of disks and folders are done for us. However, I keep getting this error telling me:

[FAILED] Failed to mount /mnt/share.

So I have to mount this directory myself, by running:

1
sudo mount -t cifs //localhost/share /mnt/share -o user=<Samba username>,password=<Samba password>

This command mounts a Samba share located on the local machine (localhost) to a directory (/mnt/share) using the Common Internet File System (CIFS) protocol.

The -o option specifies additional mount options, in this case, it provides the Samba username and password to authenticate and access the share.


Enable Samba service

But before we run this command, we have to ensure that Samba service on the ArchLinux VM is up and running.

Check that the Samba service is running on the ArchLinux VM:

1
sudo systemctl status smb.service

If the service is not running, start it using the following command:

1
sudo systemctl start smb.service

To automatically start the Samba service on boot, you can enable the smb.service unit running the following command:

1
sudo systemctl enable smb.service

Make sure that the Samba username and password are correct.

1
smbclient //localhost/share -U mccranky

Restart the ArchLinux VM to test that the Samba service starts automatically:

1
sudo systemctl reboot

To check whether /mnt/share has been successfully mounted, run the following line:

1
mount | grep /mnt/share

If the directory is mounted, we should be able to see something similar to this:

//localhost/share on /mnt/share type cifs (rw,relatime,vers=3.1.1,cache=strict,username=mccranky,
uid=0,noforceuid,gid=0,noforcegid,addr=127.0.0.1,file_mode=0755,dir_mode=0755,soft,nounix,serveri
no,mapposix,rsize=4194304,wsize=4194304,bsize=1048576,echo_interval=60,actimeo=1,closetimeo=1,use
r=mccranky)

Mount /mnt/share on startup

To automatically mount the Samba shared directory on boot, you can add an entry to the /etc/fstab file.

1
sudo nano /etc/fstab

Then append the following line to the end of the file:

1
//localhost/share /mnt/share cifs user=mccranky,password=Rogue12 0 0

However, it seems that our new input comflicts with this line:

1
share /mnt/share 9p trans=virtio,nofail 0 0

The reason for this is because our new line mounts a CIFS file system from localhost onto the mount point /mnt/share, using the username mccranky and password Rogue12.

The old line mounts a 9p file system onto the same mount point /mnt/share, using the virtio transport and the nofail option.

Since our old entry gave us much headache, I’ll just swap it for the new one. :)

Let’s test the mount by running the following command:

1
sudo mount -a

This command will attempt to mount all entries in the /etc/fstab file. If there are any errors, it will display them in the terminal.

Now we run sudo systemctl reboot to see if everything has been resolved at boot-up. There should be no more failures.

Rollback

Disable Samba service

1
2
3
4
5
6
7
8
9
10
11
12
13
sudo systemctl stop smb.service

sudo systemctl disable smb.service

sudo pacman -R samba

sudo rm -rf /etc/samba
sudo rm -rf /var/cache/samba
sudo rm -rf /var/lib/samba

sudo userdel -r samba
sudo groupdel samba

Unmount /mnt/share

Edit the Samba configuration file /etc/samba/smb.conf using a text editor:

1
sudo nano /etc/samba/smb.conf

Find the section that defines the /mnt/share share. It should look something like this:

1
2
3
4
[mnt]
path = /mnt/share
read only = no
guest ok = yes

Remove the entire section, then restart the Samba service:

1
sudo systemctl start smb.service

The /mnt/share share should now be removed from Samba on Arch Linux.

Revoke Samba password

1
sudo smbpasswd -x <user>

This will remove the user’s password from the Samba password database, effectively undoing the sudo smbpasswd -a <user> command.