User Tools

Site Tools


OpenWRT上的时间胶囊 (支持Time Machine的AFP(Apple File Protocol)服务器)



Time Machine是苹果的备份软件。从10.5 "Leopard" 版本的Mac OS X开始它被包含在系统中。这款软件被设计为可以工作在内部或者外部的驱动上,也可以工作在苹果的时间胶囊上。除了其他方面来说,时间胶囊就是一个带硬盘的无线路由。它会在网络上广播自己,允许Mac电脑在很少的设置下通过无线使用Time Machine进行备份。这篇维基文档讲述了如何让你的OpenWRT路由器提供时间胶囊一样的功能。

下面的配置是为稳定版本Attitude Adjustment 12.09 (r36088)而写的。在一台运行OS X 10.8.3的Mac电脑上测试通过。这些指导在一台新安装了OpenWRT的Netgear WNDR3800上验证通过。网络上有很多例子讲述在Linux系统上配置时间胶囊。其中很多包含矛盾的、不正确的或者多余的信息。这篇维基试图阐述在OpenWRT上运行时间胶囊的最小配置。在配置时间胶囊时有三个必要的步骤:

  1. 格式化,挂载,配置外部USB驱动器
  2. 安装并配置netatalk
  3. 安装并配置avahi


Time Machine可以工作在其他文件系统上,但是Mac原生的HFS+文件系统是最为推荐的。如果你的路由器坏了,你可以简单的将外部驱动器插到你的Mac上来恢复数据。

把外部驱动器插到Mac上,用磁盘工具重建为一个单分区的磁盘。分区时选择GUID分区表,分区格式选择Mac OS 扩展(日志式)。完成这一步操作后,在你的Mac上使用以下命令关闭这块磁盘的日志:

/usr/sbin/diskutil disableJournal /Volumes/<卷名>


opkg update
opkg install shadow-useradd shadow-groupadd
groupadd timemachine
useradd -M -G timemachine tmuser
passwd tmuser

为这块驱动器创建一个挂载点,并将这个目录的拥有者设置为 timemachine 组。例如,要使用目录/mnt/TimeMachine。创建一个只读文件来避免磁盘没有挂载时写入到闪存中。

mkdir /mnt/TimeMachine
chown root:timemachine /mnt/TimeMachine
touch /mnt/TimeMachine/USB_DISK_NOT_PRESENT
chmod 444 /mnt/TimeMachine/USB_DISK_NOT_PRESENT

安装连接USB驱动器的必要软件包。查看 usb.overviewusb.essentials 以获取更多信息。

opkg install kmod-usb-storage kmod-fs-hfsplus block-mount

  • mkod-usb-storage: USB存储的内核模块,包括所有的依赖。
  • kmod-fs-hfsplus: HFS+文件系统的内核模块,这将会在时间胶囊中使用。
  • block-mount: 系统启动或插入时自动挂载驱动器的脚本。

插入外置驱动器,通过命令 blkid 查找到设备编号:

root@OpenWRT:~# blkid
/dev/mtdblock3: TYPE="squashfs"
/dev/sda1: LABEL="EFI" UUID="70D6-1701" TYPE="vfat"
/dev/sda2: UUID="b8212b1d-8d11-30e7-9506-eca9ac887034" LABEL="TimeMachine" TYPE="hfsplus"

配置驱动器在开机或者插入的时候自动挂载。添加以下配置到 /etc/config/fstab, 这里的 /dev/sda2 是通过 blkid 识别到的驱动器:

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


/etc/init.d/fstab start


修改挂载的驱动器的拥有者为 timemachine 组:

chown -R root: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. For the purposes of this installation, only a Time Machine share will be configured. Start by installing the netatalk package:

opkg install netatalk

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 -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 volsizlimit parameter. 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 Only three lines of the default file need changing:


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">
<name replace-wildcards="yes">Time Capsule</name>

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.

FIXME (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

Configure TimeMachine

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.

Restore From Your Server

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.

FIXME (fix mounting commands to work with username:password)

Restore from the USB Disk

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:

/etc/init.d/afpd start

Recovery from Disk Errors

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:

  1. Connect to the TimeMachine network share.
  2. Run /usr/bin/chflags -R nouchg "/Volumes/TimeMachine/[hostname].sparsebundle"
  3. Run /usr/bin/hdiutil attach -nomount -noverify -noautofsck "/Volumes/TimeMachine/[hostname].sparsebundle"
  4. /sbin/fsck_hfs -drfy /dev/disk3s2 # or whatever disk partition it is mounted as
  5. Run /usr/bin/sed -i "" -e's/<integer>2<\/integer>/<integer>0<\/integer>' /Volumes/TimeMachine/[hostname].sparsebundle/
  6. diskutil eject "/Volumes/TimeMachine/[hostname].sparsebundle"
  7. Try running your backup again.
zh-cn/timemachine.txt · Last modified: 2015/03/23 12:57 by ztech