Working fine with 'force' mount option in fstab
Time Machine is Apple's backup software. It is included with Mac OS X and was introduced with the 10.5 "Leopard" release of Mac OS X. The software is designed to work with internal or external drives, as well as with Apple's Time Capsule. Time Capsule is (among other things) a wireless router with a built in hard drive. It will advertize itself on a network and allow Mac computers to back up wirelessly using Time Machine with a minimum of user configuration. This wiki documents how to make your OpenWRT router behave like a Time Capsule.
This following configuration is for the stable release of Attitude Adjustment 12.09 (r36088). It was developed and tested with a Mac running OSX 10.8.3. These directions have been verified on a Netgear WNDR3800 using a new installation of OpenWRT. There are many examples on the web for configuring a Time Capsule on linux systems. Many of these contain conflicting, incorrect, or superfluous information. This wiki is an attempt to detail the minimum configuration required to run a Time Capsule on OpenWRT. There are three basic steps to configuring Time Capsule:
Time Machine will work with other filesystems, but the Mac native HFS+ is recommended. If your router should die, you can simply plug the external drive into your Mac to recover the data.
Plug the external drive to a Mac and use the Disk Utility to create a single partition. Use the GUID layout for the partitioning and select the "Mac OS Extended (Journalled)" format. When this is complete, use the following command (on the Mac) to turn off journalling:
/usr/sbin/diskutil disableJournal /Volumes/<name_of_volume>
A user name and password will be required to connect to the Time Capsule. Rather than running as root, create a new user and group. For the purposes of this example, the user name is "tmuser" and the group is "timemachine". This can be done manually, or by installing two packages and using the following commands:
opkg update opkg install shadow-useradd shadow-groupadd groupadd timemachine useradd -M -G timemachine tmuser passwd tmuser
Create a mount point for the drive and set the ownership of the directory to the timemachine group. For this example, the directory /mnt/TimeMachine will be used. Create the read-only file to prevent writing to flash if the disk is not mounted.
mkdir /mnt/TimeMachine chown root:timemachine /mnt/TimeMachine touch /mnt/TimeMachine/USB_DISK_NOT_PRESENT chmod 444 /mnt/TimeMachine/USB_DISK_NOT_PRESENT
opkg install kmod-usb-storage kmod-fs-hfsplus block-mount
Plug in the external drive and find the device number using the block info command (on older installations of OpenWRT you may have to use the command blkid instead):
root@OpenWrt:~# block info /dev/mtdblock2: UUID="1e0c82a7-150c1d32-b3d5c71c-917a14ed" VERSION="4.0" TYPE="squashfs" /dev/mtdblock3: TYPE="jffs2" /dev/sda2: TYPE="hfsplus" root@OpenWrt:~#
Configure the drive to be automounted on startup or when plugged in. Add the following to /etc/config/fstab, where /dev/sda2 is the drive identified using block info:
config mount option target /mnt/TimeMachine option device /dev/sda2 option fstype hfsplus option options force,rw,sync option enabled 1 option enabled_fsck 0
Mount the drive.
If this is successful, you may want to reboot the router to confirm that the drive is mounted on startup.
Change the ownership of the mounted disk to the timemachine group:
chown -R tmuser:timemachine /mnt/TimeMachine
Netatalk is the linux package that supports the Apple File Protocol on linux systems, allowing for file serving, printing, and time servers. It can also be configured to support Time Machine over a network, replicating the features of Apple's Time Capsule. Starting with version 2.2, Netatalk supports the latest AFP protocol level 3.3, required to support Time Machine on OSX 10.7 (Lion) and above. Thankfully, netatalk 2.2.1-5 is included in Attitude Adjustment. If you are using Barrier Breaker, note that netatalk was removed from this version, so you will need to grab an old package from Attitude Adjustment
For the purposes of this installation, only a Time Machine share will be configured. Start by installing the netatalk package:
opkg install netatalk
If this command fails, grab the old version of this package from Attitude Adjustment (you will need to change the ar71xx part of the URL to match the architecture of your router):
opkg install http://downloads.openwrt.org/attitude_adjustment/12.09/ar71xx/generic/packages/netatalk_2.2.1-5_ar71xx.ipk cd /usr/lib ln -s libgcrypt.so.20 libgcrypt.so.11
Configure netatalk by adding the line below to /etc/netatalk/afpd.conf. Comment out all other lines. This will configure a server named "TimeMachine" to use the Diffie-Hellman eXchange 2 (DHX2) for authentication. Refer to the man page for afpd.conf for further details on the other options.
"TimeMachine" -uampath /usr/lib/uams -uamlist uams_dhx2.so -nodebug -nouservol -icon -nosavepassword -mimicmodel RackMac
The volumes netatalk will make available are defined in /etc/netatalk/AppleVolumes.default. Add the line below to /etc/netatalk/AppleVolumes.default to create the AFP time machine share, commenting out all other lines.
/mnt/TimeMachine TimeMachine volsizelimit:150000 allow:@timemachine rwlist:@timemachine cnidscheme:dbd options:searchdb,usedots,invisibledots,tm
The first parameter specifies the location of the shared volume, while the second specifies the name. The Time Machine uses a CNID database to reference files on the shared volume. By default, this database will be located in the root directory of the shared volume. The size of the shared volume reported to time machine is set using the volsizelimit parameter. The size is given in MiB. Set this parameter to less than the full size of the disk to reserve space for the database. (If the database can't be written to or is corrupted, the entire Time Machine repository may be lost) The allow and rwlist parameters specify which users can access and write to the time machine. Either specify a user name directly, or just use @timemachine to allow access for users in the timemachine group. Reference the AppleVolumes.default man page for further information on options.
Restart netatalk and enable on boot:
/etc/init.d/afpd stop /etc/init.d/afpd start /etc/init.d/afpd enable
Avahi implements a protocol known as Zeroconf or Bonjour to advertise your volume on the network. Install the package:
opkg install avahi-daemon
Avahi is configured via /etc/avahi/avahi-daemon.conf, see http://linux.die.net/man/5/avahi-daemon.conf. Only three lines of the default file need changing:
host-name=TimeMachine enable-dbus=no allow-interfaces=br-lan
The first line defines the host name, which will be displayed on client computers before logging in. The second line is not in the default file and must be added to disable dbus support. dbus is a protocol for communication which can be used to communicate between Avahi and Netatalk however, for the purposes of setting up a Time Capsule, it is not required. Instead, the services that Avahi advertises are defined via the XML files in /etc/avahi/services. Create the file /etc/avahi/services/afpd.service, with the following content:
<?xml version="1.0" standalone="no"?> <!DOCTYPE service-group SYSTEM "avahi-service.dtd"> <service-group> <name replace-wildcards="yes">Time Capsule</name> <service> <type>_afpovertcp._tcp</type> <port>548</port> </service> <service> <type>_device-info._tcp</type> <port>0</port> <txt-record>model=TimeCapsule</txt-record> </service> <service> <type>_adisk._tcp</type> <port>9</port> <txt-record>sys=waMA=XX:XX:XX:XX:XX:XX,adVF=0x100</txt-record> <txt-record>dk1=adVF=0x83,adVN=TimeMachine</txt-record> </service> </service-group>
This file defines a service group named "Time Capsule", divided into three sections. The first two sections enable the AFP shared volume. The text "model=TimeCapsule" defines the volume as a time capsule, so that the time capsule icon is displayed in the finder. The third section attaches the disk to the service group so that it shows up under the "Select Disk" window in the time machine preferences. The "XX:XX:XX:XX:XX:XX" in the example above should be replaced with the MAC address of the router's LAN interface. The section "adVN=TimeMachine" should match the name of the volume defined in afpd.conf and AppleVolumes.default.
(Provide better explanation of XML file details.)
Restart Avahi and enable on boot:
/etc/init.d/avahi-daemon stop /etc/init.d/avahi-daemon start /etc/init.d/avahi-daemon enable
You can now use the TimeMachine preferences pane on your client Mac to define your advertised volume as destination drive. When prompted for a user name and password, use tmuser and the password set above. TimeMachine will save your login information.
If the harddisk of your Mac dies and needs reinstallation through TimeMachine, your crippled Mac must first see your OpenWRT box. Boot your Mac with a DVD or similar, open a Terminal and type
mkdir /Volumes/TimeMachine mount -t afp "afp://<your OpenWRT route>/TimeMachine" /Volumes/TimeMachine
You can then use TimeMachine to read back all your saved data from the remote drive.
(fix mounting commands to work with username:password)
Since the USB disk was formatted using the HFS+ format, umount it from the router and plug it in directly to your Mac for a faster restore.
/etc/init.d/afpd stop umount /mnt/TimeMachine
When finished, plug it back into the router and restart Netatalk:
If the backup process is interrupted, the database can become corrupt and unusable. Time Machine will report that the backup is unusable and will suggest creating a new backup.
Use the following steps on your Mac as root:
/usr/bin/chflags -R nouchg "/Volumes/TimeMachine/[hostname].sparsebundle"
/usr/bin/hdiutil attach -nomount -noverify -noautofsck "/Volumes/TimeMachine/[hostname].sparsebundle"
/sbin/fsck_hfs -drfy /dev/disk3s2# or whatever disk partition it is mounted as
/usr/bin/sed -i "" -e's/<integer>2<\/integer>/<integer>0<\/integer>' /Volumes/TimeMachine/[hostname].sparsebundle/com.apple.TimeMachine.MachineID.plist
diskutil eject "/Volumes/TimeMachine/[hostname].sparsebundle"