Documentation

Raspberry Pi Mac Time Machine

Using the RADXA Quad Sata Hat

Joep on 15.04.24

Configure your Raspberry Pi 4 alongside the ROCK Pi Dual/Quad SATA HAT to create a solution for a macOS Time Machine backup or NAS (Network Attached Storage). For this setup, we'll be using Raspberry Pi OS Lite 64-bit and Netatalk.

Assuming the Raspberry Pi is already running and the SATA HAT with disks is properly connected, we can proceed with configuring the system.

Access your Raspberry Pi via SSH, then begin by ensuring it is up to date with the following commands:

sudo apt update
sudo apt upgrade

Once updated, you can proceed using the following command for setting up the SATA HAT:

curl -sL https://rock.sh/get-rockpi-sata | sudo -E bash -

Create a folder for the disk to mount:

sudo mkdir /disk1

The next step is to identify the connected storage devices by executing the following command:

ls /dev/sd*

The output could be similar to this:
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdc /dev/sdc1 /dev/sdd
where the number /dev/sda1 and /dev/sda2 are partitions of the disk /dev/sda.

As an example, we choose the /dev/sda disk for the purpose to use for Time Machine. Proceed with caution as the following action will result in the loss of all data on the disk.

First, we clear any existing filesystem signatures on the disk using the command:

sudo wipefs -a /dev/sda

Then, we create a new btrfs filesystem on the disk with the command:

sudo mkfs.btrfs /dev/sda

To ensure we mount the correct disk, it's crucial to retrieve the UUID of the /dev/sda disk, which can be done using the following command:

sudo blkid -s UUID -o value /dev/sda

The output could be similar to this:
0ae2c52a-ba36-4f01-80d6-e975ade41f8a

We need to create a .mount, .automount and .service file to ensure seamless integration and automated mounting of the external drives after the SATA HAT is properly initialized and accessible for use. You can name these files as you prefer, like disk1.mount, disk1.automount, and disk1.service.

Create and open the .mount file:

sudo nano /etc/systemd/system/disk1.mount

Copy following code, edit with the right UUID and mount folder, paste it into the .mount file and save the file.

[Unit]
Description = mount file for disk 1
After = rockpi-sata.service
Requires = rockpi-sata.service

[Mount]
What = /dev/disk/by-uuid/0ae2c52a-ba36-4f01-80d6-e975ade41f8a
Where = /disk1
Type = btrfs
Options = defaults,nofail
LazyUnmount = True

[Install]
WantedBy = multi-user.target

Create and open the .automount file:

sudo nano /etc/systemd/system/disk1.automount

Copy following code, edit with the right mount folder, paste it into the .automount file and save the file.

[Automount]
Where=/disk1

[Install]
WantedBy=multi-user.target

Create and open the .service file:

sudo nano /etc/systemd/system/disk1.service

Copy following code, edit with the right UUID, paste it into the .service file and save the file.

[Unit]
Description = service file for disk 1
After = disk1.mount
Requires = disk1.mount

[Service]
Type = oneshot
ExecStartPre = /usr/sbin/hdparm -S 12 /dev/disk/by-uuid/0ae2c52a-ba36-4f01-80d6-e975ade41f8a
ExecStart = /usr/sbin/hdparm -B 127 /dev/disk/by-uuid/0ae2c52a-ba36-4f01-80d6-e975ade41f8a

[Install]
WantedBy = multi-user.target

After creating, editing and saving the .mount, .automount and .service file, you'll need to enable and start each corresponding systemd unit:

sudo systemctl daemon-reload
sudo systemctl enable disk1.mount && sudo systemctl start disk1.mount
sudo systemctl enable disk1.automount && sudo systemctl start disk1.automount
sudo systemctl enable disk1.service && sudo systemctl start disk1.service

Checks

Let us follow up with some checks.

List all mounted file systems:

df -h

The output should contain your filesystem and mount folder. It should be similar to this:
/dev/sda        932G   1.3M   932G     1%   /disk1

Display the logs related to the disk1.service

journalctl -u disk1.service

The output should be similar to this:
Apr 26 08:21:11 raspberrypi systemd[1]: Starting disks.service - Set services for disk1...
Apr 26 08:21:11 raspberrypi hdparm[2358]: /dev/disk/by-uuid/0ae2c52a-ba36-4f01-80d6-e975ade41f8a:
Apr 26 08:21:11 raspberrypi hdparm[2358]:  setting standby to 12 (1 minute)
Apr 26 08:21:11 raspberrypi hdparm[2361]: /dev/disk/by-uuid/0ae2c52a-ba36-4f01-80d6-e975ade41f8a:
Apr 26 08:21:11 raspberrypi hdparm[2361]:  setting Advanced Power Management level to 0x7f (127)
Apr 26 08:21:11 raspberrypi hdparm[2361]:  APM_level        = 127
Apr 26 08:21:11 raspberrypi systemd[1]: disk1.service: Deactivated successfully.

Netatalk

With Netatalk, Mac users can perform tasks such as browsing directories, copying files, and backing up data to Unix/Linux servers directly from their macOS devices.

Install Netatalk:

sudo apt install netatalk

After installing open the configuration file:

sudo nano /etc/netatalk/afp.conf

Customize and add following code at the bottom of the file and save it.

[Disk1]
path = /disk1
time machine = yes
valid users = your_username

Then restart the Netatalk service:

sudo systemctl restart netatalk

Ownership

Set the ownership of the disk and mount directory:

sudo chown your_username /dev/sda
sudo chown -R your_username /disk1

Now in macOS go to Preferences, to Time Machine and add a network disk. 'Disk1' should show up in the list.