wangdamingll 2018-10-15
跟随这些逐步指导构建你自己的基于树莓派的 NAS 系统。
我将在接下来的这三篇文章中讲述如何搭建一个简便、实用的 NAS 云盘系统。我在这个中心化的存储系统中存储数据,并且让它每晚都会自动的备份增量数据。本系列文章将利用 NFS 文件系统将磁盘挂载到同一网络下的不同设备上,使用 Nextcloud 来离线访问数据、分享数据。
本文主要讲述将数据盘挂载到远程设备上的软硬件步骤。本系列第二篇文章将讨论数据备份策略、如何添加定时备份数据任务。最后一篇文章中我们将会安装 Nextcloud 软件,用户通过 Nextcloud 提供的 web 界面可以方便的离线或在线访问数据。本系列教程最终搭建的 NAS 云盘支持多用户操作、文件共享等功能,所以你可以通过它方便的分享数据,比如说你可以发送一个加密链接,跟朋友分享你的照片等等。
最终的系统架构如下图所示:
首先需要准备硬件。本文所列方案只是其中一种示例,你也可以按不同的硬件方案进行采购。
最主要的就是树莓派 3,它带有四核 CPU、1G RAM,以及(比较)快速的网络接口。数据将存储在两个 USB 磁盘驱动器上(这里使用 1TB 磁盘);其中一个磁盘用于每天数据存储,另一个用于数据备份。请务必使用有源 USB 磁盘驱动器或者带附加电源的 USB 集线器,因为树莓派无法为两个 USB 磁盘驱动器供电。
在该社区中最活跃的操作系统当属 Raspbian,便于定制个性化项目。已经有很多 操作指南 讲述如何在树莓派中安装 Raspbian 系统,所以这里不再赘述。在撰写本文时,最新的官方支持版本是 Raspbian Stretch,它对我来说很好使用。
到此,我将假设你已经配置好了基本的 Raspbian 系统并且可以通过 ssh
访问到你的树莓派。
为了更好地读写数据,我建议使用 ext4 文件系统去格式化磁盘。首先,你必须先找到连接到树莓派的磁盘。你可以在 /dev/sd/<x>
中找到磁盘设备。使用命令 fdisk -l
,你可以找到刚刚连接的两块 USB 磁盘驱动器。请注意,操作下面的步骤将会清除 USB 磁盘驱动器上的所有数据,请做好备份。
<span class="pln">pi@raspberrypi</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="kwd">fdisk</span><span class="pun">-</span><span class="pln">l</span>
<span class="pun"><...></span>
<span class="typ">Disk</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda</span><span class="pun">:</span><span class="lit">931.5</span><span class="typ">GiB</span><span class="pun">,</span><span class="lit">1000204886016</span><span class="pln"> bytes</span><span class="pun">,</span><span class="lit">1953525168</span><span class="pln"> sectors</span>
<span class="typ">Units</span><span class="pun">:</span><span class="pln"> sectors of </span><span class="lit">1</span><span class="pun">*</span><span class="lit">512</span><span class="pun">=</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="typ">Sector</span><span class="kwd">size</span><span class="pun">(</span><span class="pln">logical</span><span class="pun">/</span><span class="pln">physical</span><span class="pun">):</span><span class="lit">512</span><span class="pln"> bytes </span><span class="pun">/</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="pln">I</span><span class="pun">/</span><span class="pln">O </span><span class="kwd">size</span><span class="pun">(</span><span class="pln">minimum</span><span class="pun">/</span><span class="pln">optimal</span><span class="pun">):</span><span class="lit">512</span><span class="pln"> bytes </span><span class="pun">/</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="typ">Disklabel</span><span class="pln"> type</span><span class="pun">:</span><span class="pln"> dos</span>
<span class="typ">Disk</span><span class="pln"> identifier</span><span class="pun">:</span><span class="lit">0xe8900690</span>
<span class="typ">Device</span><span class="typ">Boot</span><span class="typ">Start</span><span class="typ">End</span><span class="typ">Sectors</span><span class="typ">Size</span><span class="typ">Id</span><span class="typ">Type</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1 </span><span class="lit">2048</span><span class="lit">1953525167</span><span class="lit">1953523120</span><span class="lit">931.5G</span><span class="lit">83</span><span class="typ">Linux</span>
<span class="typ">Disk</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sdb</span><span class="pun">:</span><span class="lit">931.5</span><span class="typ">GiB</span><span class="pun">,</span><span class="lit">1000204886016</span><span class="pln"> bytes</span><span class="pun">,</span><span class="lit">1953525168</span><span class="pln"> sectors</span>
<span class="typ">Units</span><span class="pun">:</span><span class="pln"> sectors of </span><span class="lit">1</span><span class="pun">*</span><span class="lit">512</span><span class="pun">=</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="typ">Sector</span><span class="kwd">size</span><span class="pun">(</span><span class="pln">logical</span><span class="pun">/</span><span class="pln">physical</span><span class="pun">):</span><span class="lit">512</span><span class="pln"> bytes </span><span class="pun">/</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="pln">I</span><span class="pun">/</span><span class="pln">O </span><span class="kwd">size</span><span class="pun">(</span><span class="pln">minimum</span><span class="pun">/</span><span class="pln">optimal</span><span class="pun">):</span><span class="lit">512</span><span class="pln"> bytes </span><span class="pun">/</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="typ">Disklabel</span><span class="pln"> type</span><span class="pun">:</span><span class="pln"> dos</span>
<span class="typ">Disk</span><span class="pln"> identifier</span><span class="pun">:</span><span class="lit">0x6aa4f598</span>
<span class="typ">Device</span><span class="typ">Boot</span><span class="typ">Start</span><span class="typ">End</span><span class="typ">Sectors</span><span class="typ">Size</span><span class="typ">Id</span><span class="typ">Type</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sdb1 </span><span class="pun">*</span><span class="lit">2048</span><span class="lit">1953521663</span><span class="lit">1953519616</span><span class="lit">931.5G</span><span class="lit">83</span><span class="typ">Linux</span>
由于这些设备是连接到树莓派的唯一的 1TB 的磁盘,所以我们可以很容易的辨别出 /dev/sda
和 /dev/sdb
就是那两个 USB 磁盘驱动器。每个磁盘末尾的分区表提示了在执行以下的步骤后如何查看,这些步骤将会格式化磁盘并创建分区表。为每个 USB 磁盘驱动器按以下步骤进行操作(假设你的磁盘也是 /dev/sda
和 /dev/sdb
,第二次操作你只要替换命令中的 sda
为 sdb
即可)。
首先,删除磁盘分区表,创建一个新的并且只包含一个分区的新分区表。在 fdisk
中,你可以使用交互单字母命令来告诉程序你想要执行的操作。只需要在提示符 Command(m for help):
后输入相应的字母即可(可以使用 m
命令获得更多详细信息):
<span class="pln">pi@raspberrypi</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="kwd">fdisk</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda</span>
<span class="typ">Welcome</span><span class="pln"> to </span><span class="kwd">fdisk</span><span class="pun">(</span><span class="pln">util</span><span class="pun">-</span><span class="pln">linux </span><span class="lit">2.29</span><span class="pun">.</span><span class="lit">2</span><span class="pun">).</span>
<span class="typ">Changes</span><span class="pln"> will remain </span><span class="kwd">in</span><span class="pln"> memory only</span><span class="pun">,</span><span class="kwd">until</span><span class="pln"> you decide to </span><span class="kwd">write</span><span class="pln"> them</span><span class="pun">.</span>
<span class="typ">Be</span><span class="pln"> careful before </span><span class="kwd">using</span><span class="pln"> the </span><span class="kwd">write</span><span class="pln"> command</span><span class="pun">.</span>
<span class="typ">Command</span><span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="pln"> o</span>
<span class="typ">Created</span><span class="pln"> a </span><span class="kwd">new</span><span class="pln"> DOS disklabel </span><span class="kwd">with</span><span class="pln"> disk identifier </span><span class="lit">0x9c310964</span><span class="pun">.</span>
<span class="typ">Command</span><span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="pln"> n</span>
<span class="typ">Partition</span><span class="pln"> type</span>
<span class="pln">p primary </span><span class="pun">(</span><span class="lit">0</span><span class="pln"> primary</span><span class="pun">,</span><span class="lit">0</span><span class="pln"> extended</span><span class="pun">,</span><span class="lit">4</span><span class="pln"> free</span><span class="pun">)</span>
<span class="pln">e extended </span><span class="pun">(</span><span class="pln">container </span><span class="kwd">for</span><span class="pln"> logical partitions</span><span class="pun">)</span>
<span class="typ">Select</span><span class="pun">(</span><span class="kwd">default</span><span class="pln"> p</span><span class="pun">):</span><span class="pln"> p</span>
<span class="typ">Partition</span><span class="pln"> number </span><span class="pun">(</span><span class="lit">1</span><span class="pun">-</span><span class="lit">4</span><span class="pun">,</span><span class="kwd">default</span><span class="lit">1</span><span class="pun">):</span>
<span class="typ">First</span><span class="pln"> sector </span><span class="pun">(</span><span class="lit">2048</span><span class="pun">-</span><span class="lit">1953525167</span><span class="pun">,</span><span class="kwd">default</span><span class="lit">2048</span><span class="pun">):</span>
<span class="typ">Last</span><span class="pln"> sector</span><span class="pun">,</span><span class="pun">+</span><span class="pln">sectors </span><span class="kwd">or</span><span class="pun">+</span><span class="kwd">size</span><span class="pun">{</span><span class="pln">K</span><span class="pun">,</span><span class="pln">M</span><span class="pun">,</span><span class="pln">G</span><span class="pun">,</span><span class="pln">T</span><span class="pun">,</span><span class="pln">P</span><span class="pun">}</span><span class="pun">(</span><span class="lit">2048</span><span class="pun">-</span><span class="lit">1953525167</span><span class="pun">,</span><span class="kwd">default</span><span class="lit">1953525167</span><span class="pun">):</span>
<span class="typ">Created</span><span class="pln"> a </span><span class="kwd">new</span><span class="pln"> partition </span><span class="lit">1</span><span class="pln"> of type </span><span class="str">'Linux'</span><span class="kwd">and</span><span class="pln"> of </span><span class="kwd">size</span><span class="lit">931.5</span><span class="typ">GiB</span><span class="pun">.</span>
<span class="typ">Command</span><span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="pln"> p</span>
<span class="typ">Disk</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda</span><span class="pun">:</span><span class="lit">931.5</span><span class="typ">GiB</span><span class="pun">,</span><span class="lit">1000204886016</span><span class="pln"> bytes</span><span class="pun">,</span><span class="lit">1953525168</span><span class="pln"> sectors</span>
<span class="typ">Units</span><span class="pun">:</span><span class="pln"> sectors of </span><span class="lit">1</span><span class="pun">*</span><span class="lit">512</span><span class="pun">=</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="typ">Sector</span><span class="kwd">size</span><span class="pun">(</span><span class="pln">logical</span><span class="pun">/</span><span class="pln">physical</span><span class="pun">):</span><span class="lit">512</span><span class="pln"> bytes </span><span class="pun">/</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="pln">I</span><span class="pun">/</span><span class="pln">O </span><span class="kwd">size</span><span class="pun">(</span><span class="pln">minimum</span><span class="pun">/</span><span class="pln">optimal</span><span class="pun">):</span><span class="lit">512</span><span class="pln"> bytes </span><span class="pun">/</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="typ">Disklabel</span><span class="pln"> type</span><span class="pun">:</span><span class="pln"> dos</span>
<span class="typ">Disk</span><span class="pln"> identifier</span><span class="pun">:</span><span class="lit">0x9c310964</span>
<span class="typ">Device</span><span class="typ">Boot</span><span class="typ">Start</span><span class="typ">End</span><span class="typ">Sectors</span><span class="typ">Size</span><span class="typ">Id</span><span class="typ">Type</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1 </span><span class="lit">2048</span><span class="lit">1953525167</span><span class="lit">1953523120</span><span class="lit">931.5G</span><span class="lit">83</span><span class="typ">Linux</span>
<span class="typ">Command</span><span class="pun">(</span><span class="pln">m </span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">):</span><span class="kwd">w</span>
<span class="typ">The</span><span class="pln"> partition table has been altered</span><span class="pun">.</span>
<span class="typ">Syncing</span><span class="pln"> disks</span><span class="pun">.</span>
现在,我们将用 ext4 文件系统格式化新创建的分区 /dev/sda1
:
<span class="pln">pi@raspberrypi</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="kwd">mkfs</span><span class="pun">.</span><span class="pln">ext4 </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1</span>
<span class="kwd">mke2fs</span><span class="lit">1.43</span><span class="pun">.</span><span class="lit">4</span><span class="pun">(</span><span class="lit">31</span><span class="pun">-</span><span class="typ">Jan</span><span class="pun">-</span><span class="lit">2017</span><span class="pun">)</span>
<span class="typ">Discarding</span><span class="pln"> device blocks</span><span class="pun">:</span><span class="kwd">done</span>
<span class="pun"><...></span>
<span class="typ">Allocating</span><span class="pln"> group tables</span><span class="pun">:</span><span class="kwd">done</span>
<span class="typ">Writing</span><span class="pln"> inode tables</span><span class="pun">:</span><span class="kwd">done</span>
<span class="typ">Creating</span><span class="pln"> journal </span><span class="pun">(</span><span class="lit">1024</span><span class="pln"> blocks</span><span class="pun">):</span><span class="kwd">done</span>
<span class="typ">Writing</span><span class="pln"> superblocks </span><span class="kwd">and</span><span class="pln"> filesystem accounting information</span><span class="pun">:</span><span class="kwd">done</span>
重复以上步骤后,让我们根据用途来对它们建立标签:
<span class="pln">pi@raspberrypi</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="pln"> e2label </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1 data</span>
<span class="pln">pi@raspberrypi</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="pln"> e2label </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sdb1 backup</span>
现在,让我们安装这些磁盘并存储一些数据。以我运营该系统超过一年的经验来看,当树莓派启动时(例如在断电后),USB 磁盘驱动器并不是总被挂载,因此我建议使用 autofs 在需要的时候进行挂载。
首先,安装 autofs 并创建挂载点:
<span class="pln">pi@raspberrypi</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="pln"> apt install autofs</span>
<span class="pln">pi@raspberrypi</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="kwd">mkdir</span><span class="pun">/</span><span class="pln">nas</span>
然后添加下面这行来挂载设备 /etc/auto.master
:
<span class="str">/nas /</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">auto</span><span class="pun">.</span><span class="pln">usb</span>
如果不存在以下内容,则创建 /etc/auto.usb
,然后重新启动 autofs 服务:
<span class="pln">data </span><span class="pun">-</span><span class="pln">fstype</span><span class="pun">=</span><span class="pln">ext4</span><span class="pun">,</span><span class="pln">rw </span><span class="pun">:</span><span class="str">/dev/</span><span class="pln">disk</span><span class="pun">/</span><span class="pln">by</span><span class="pun">-</span><span class="pln">label</span><span class="pun">/</span><span class="pln">data</span>
<span class="pln">backup </span><span class="pun">-</span><span class="pln">fstype</span><span class="pun">=</span><span class="pln">ext4</span><span class="pun">,</span><span class="pln">rw </span><span class="pun">:</span><span class="str">/dev/</span><span class="pln">disk</span><span class="pun">/</span><span class="pln">by</span><span class="pun">-</span><span class="pln">label</span><span class="pun">/</span><span class="pln">backup</span>
<span class="pln">pi@raspberrypi3</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="pln"> service autofs restart</span>
现在你应该可以分别访问 /nas/data
以及 /nas/backup
磁盘了。显然,到此还不会令人太兴奋,因为你只是擦除了磁盘中的数据。不过,你可以执行以下命令来确认设备是否已经挂载成功:
<span class="pln">pi@raspberrypi3</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">cd</span><span class="pun">/</span><span class="pln">nas</span><span class="pun">/</span><span class="pln">data</span>
<span class="pln">pi@raspberrypi3</span><span class="pun">:</span><span class="str">/nas/</span><span class="pln">data $ </span><span class="kwd">cd</span><span class="pun">/</span><span class="pln">nas</span><span class="pun">/</span><span class="pln">backup</span>
<span class="pln">pi@raspberrypi3</span><span class="pun">:</span><span class="str">/nas/</span><span class="pln">backup $ </span><span class="kwd">mount</span>
<span class="pun"><...></span>
<span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">auto</span><span class="pun">.</span><span class="pln">usb on </span><span class="pun">/</span><span class="pln">nas type autofs </span><span class="pun">(</span><span class="pln">rw</span><span class="pun">,</span><span class="pln">relatime</span><span class="pun">,</span><span class="pln">fd</span><span class="pun">=</span><span class="lit">6</span><span class="pun">,</span><span class="pln">pgrp</span><span class="pun">=</span><span class="lit">463</span><span class="pun">,</span><span class="kwd">timeout</span><span class="pun">=</span><span class="lit">300</span><span class="pun">,</span><span class="pln">minproto</span><span class="pun">=</span><span class="lit">5</span><span class="pun">,</span><span class="pln">maxproto</span><span class="pun">=</span><span class="lit">5</span><span class="pun">,</span><span class="pln">indirect</span><span class="pun">)</span>
<span class="pun"><...></span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1 on </span><span class="pun">/</span><span class="pln">nas</span><span class="pun">/</span><span class="pln">data type ext4 </span><span class="pun">(</span><span class="pln">rw</span><span class="pun">,</span><span class="pln">relatime</span><span class="pun">,</span><span class="pln">data</span><span class="pun">=</span><span class="pln">ordered</span><span class="pun">)</span>
<span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sdb1 on </span><span class="pun">/</span><span class="pln">nas</span><span class="pun">/</span><span class="pln">backup type ext4 </span><span class="pun">(</span><span class="pln">rw</span><span class="pun">,</span><span class="pln">relatime</span><span class="pun">,</span><span class="pln">data</span><span class="pun">=</span><span class="pln">ordered</span><span class="pun">)</span>
首先进入对应目录以确保 autofs 能够挂载设备。autofs 会跟踪文件系统的访问记录,并随时挂载所需要的设备。然后 mount
命令会显示这两个 USB 磁盘驱动器已经挂载到我们想要的位置了。
设置 autofs 的过程容易出错,如果第一次尝试失败,请不要沮丧。你可以上网搜索有关教程。
现在你已经设置了基本的网络存储,我们希望将它安装到远程 Linux 机器上。这里使用 NFS 文件系统,首先在树莓派上安装 NFS 服务器:
<span class="pln">pi@raspberrypi</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="pln"> apt install nfs</span><span class="pun">-</span><span class="pln">kernel</span><span class="pun">-</span><span class="pln">server</span>
然后,需要告诉 NFS 服务器公开 /nas/data
目录,这是从树莓派外部可以访问的唯一设备(另一个用于备份)。编辑 /etc/exports
添加如下内容以允许所有可以访问 NAS 云盘的设备挂载存储:
<span class="str">/nas/</span><span class="pln">data </span><span class="pun">*(</span><span class="pln">rw</span><span class="pun">,</span><span class="kwd">sync</span><span class="pun">,</span><span class="pln">no_subtree_check</span><span class="pun">)</span>
更多有关限制挂载到单个设备的详细信息,请参阅 man exports
。经过上面的配置,任何人都可以访问数据,只要他们可以访问 NFS 所需的端口:111
和 2049
。我通过上面的配置,只允许通过路由器防火墙访问到我的家庭网络的 22 和 443 端口。这样,只有在家庭网络中的设备才能访问 NFS 服务器。
如果要在 Linux 计算机挂载存储,运行以下命令:
<span class="pln">you@desktop</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="kwd">mkdir</span><span class="pun">/</span><span class="pln">nas</span><span class="pun">/</span><span class="pln">data</span>
<span class="pln">you@desktop</span><span class="pun">:~</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">t nfs </span><span class="pun"><</span><span class="pln">raspberry</span><span class="pun">-</span><span class="pln">pi</span><span class="pun">-</span><span class="kwd">hostname</span><span class="pun">-</span><span class="kwd">or</span><span class="pun">-</span><span class="kwd">ip</span><span class="pun">>:</span><span class="str">/nas/</span><span class="pln">data </span><span class="pun">/</span><span class="pln">nas</span><span class="pun">/</span><span class="pln">data</span>
同样,我建议使用 autofs 来挂载该网络设备。如果需要其他帮助,请参看 如何使用 Autofs 来挂载 NFS 共享。
现在你可以在远程设备上通过 NFS 系统访问位于你树莓派 NAS 云盘上的数据了。在后面一篇文章中,我将介绍如何使用 rsync
自动将数据备份到第二个 USB 磁盘驱动器。你将会学到如何使用 rsync
创建增量备份,在进行日常备份的同时还能节省设备空间。
via: https://opensource.com/article/18/7/network-attached-storage-Raspberry-Pi
作者:Manuel Dewald 选题:lujun9972 译者:jrg 校对:wxy