樹莓派自建 NAS 雲盤之——樹莓派搭建網絡存儲盤

硬件首先需要準備硬件。本文所列方案只是其中一種示例,你也可以按不同的硬件方案進行採購。最主要的就是 樹莓派 3 ,它帶有四核 CPU、1G RAM,以及(比較)快速的網絡接口。數據將存儲在兩個 USB 磁盤驅動器上(這裡使用 1TB 磁盤);其中一個磁盤用於每天數據存儲,另一個用於數據備份。請務必使用有源 USB 磁盤驅動器或者帶附加電源的 USB 集線器,因為樹莓派無法為兩個 USB 磁盤驅動器供電。軟件在該社區中最活躍的操作系統當屬 Raspbian ,便於定製個性化項目。已經有很多 操作指南 講述如何在樹莓派中安裝 Raspbian 系統,所以這裡不再贅述。在撰寫本文時,最新的官方支持版本是 Raspbian Stretch ,它對我來說很好使用。到此,我將假設你已經配置好了基本的 Raspbian 系統並且可以通過 ssh 訪問到你的樹莓派。準備 USB 磁盤驅動器為了更好地讀寫數據,我建議使用 ext4 文件系統去格式化磁盤。首先,你必須先找到連接到樹莓派的磁盤。你可以在 /dev/sd/pi@raspberrypi:~ $ sudo fdisk -l Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xe8900690 Device Boot Start End Sectors Size Id Type /dev/sda1 2048 1953525167 1953523120 931.5G 83 Linux Disk /dev/sdb: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x6aa4f598 Device Boot Start End Sectors Size Id Type /dev/sdb1 * 2048 1953521663 1953519616 931.5G 83 Linux 由於這些設備是連接到樹莓派的唯一的 1TB 的磁盤,所以我們可以很容易的辨別出 /dev/sda 和 /dev/sdb 就是那兩個 USB 磁盤驅動器。每個磁盤末尾的分區表提示了在執行以下的步驟後如何查看,這些步驟將會格式化磁盤並創建分區表。為每個 USB 磁盤驅動器按以下步驟進行操作(假設你的磁盤也是 /dev/sda 和 /dev/sdb,第二次操作你只要替換命令中的 sda 為 sdb 即可)。首先,刪除磁盤分區表,創建一個新的並且只包含一個分區的新分區表。在 fdisk 中,你可以使用交互單字母命令來告訴程序你想要執行的操作。只需要在提示符 Command(m for help): 後輸入相應的字母即可(可以使用 m 命令獲得更多詳細信息):pi@raspberrypi:~ $ sudo fdisk /dev/sda Welcome to fdisk (util-linux 2.29.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): o Created a new DOS disklabel with disk identifier 0x9c310964. Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-1953525167, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-1953525167, default 1953525167): Created a new partition 1 of type 'Linux' and of size 931.5 GiB. Command (m for help): p Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x9c310964 Device Boot Start End Sectors Size Id Type /dev/sda1 2048 1953525167 1953523120 931.5G 83 Linux Command (m for help): w The partition table has been altered. Syncing disks.現在,我們將用 ext4 文件系統格式化新創建的分區 /dev/sda1:pi@raspberrypi:~ $ sudo mkfs.ext4 /dev/sda1 mke2fs 1.43.4 (31-Jan-2017) Discarding device blocks: done Allocating group tables: done Writing inode tables: done Creating journal (1024 blocks): done Writing superblocks and filesystem accounting information: done重複以上步驟後,讓我們根據用途來對它們建立標籤:pi@raspberrypi:~ $ sudo e2label /dev/sda1 data pi@raspberrypi:~ $ sudo e2label /dev/sdb1 backup現在,讓我們安裝這些磁盤並存儲一些數據。以我運營該系統超過一年的經驗來看,當樹莓派啟動時(例如在斷電後),USB 磁盤驅動器並不是總被掛載,因此我建議使用 autofs 在需要的時候進行掛載。首先,安裝 autofs 並創建掛載點:pi@raspberrypi:~ $ sudo apt install autofs pi@raspberrypi:~ $ sudo mkdir /nas然後添加下面這行來掛載設備 /etc/auto.master:/nas /etc/auto.usb如果不存在以下內容,則創建 /etc/auto.usb,然後重新啟動 autofs 服務:data -fstype=ext4,rw :/dev/disk/by-label/data backup -fstype=ext4,rw :/dev/disk/by-label/backup pi@raspberrypi3:~ $ sudo service autofs restart現在你應該可以分別訪問 /nas/data 以及 /nas/backup 磁盤了。顯然,到此還不會令人太興奮,因為你只是擦除了磁盤中的數據。不過,你可以執行以下命令來確認設備是否已經掛載成功:pi@raspberrypi3:~ $ cd /nas/data pi@raspberrypi3:/nas/data $ cd /nas/backup pi@raspberrypi3:/nas/backup $ mount /etc/auto.usb on /nas type autofs (rw,relatime,fd=6,pgrp=463,timeout=300,minproto=5,maxproto=5,indirect) /dev/sda1 on /nas/data type ext4 (rw,relatime,data=ordered) /dev/sdb1 on /nas/backup type ext4 (rw,relatime,data=ordered)首先進入對應目錄以確保 autofs 能夠掛載設備。autofs 會跟蹤文件系統的訪問記錄,並隨時掛載所需要的設備。然後 mount 命令會顯示這兩個 USB 磁盤驅動器已經掛載到我們想要的位置了。設置 autofs 的過程容易出錯,如果第一次嘗試失敗,請不要沮喪。你可以上網搜索有關教程。掛載網絡存儲現在你已經設置了基本的網絡存儲,我們希望將它安裝到遠程 Linux 機器上。這裡使用 NFS 文件系統,首先在樹莓派上安裝 NFS 服務器:pi@raspberrypi:~ $ sudo apt install nfs-kernel-server然後,需要告訴 NFS 服務器公開 /nas/data 目錄,這是從樹莓派外部可以訪問的唯一設備(另一個用於備份)。編輯 /etc/exports 添加如下內容以允許所有可以訪問 NAS 雲盤的設備掛載存儲:/nas/data *(rw,sync,no_subtree_check)更多有關限制掛載到單個設備的詳細信息,請參閱 man exports。經過上面的配置,任何人都可以訪問數據,只要他們可以訪問 NFS 所需的端口:111 和 2049。我通過上面的配置,只允許通過路由器防火牆訪問到我的家庭網絡的 22 和 443 端口。這樣,只有在家庭網絡中的設備才能訪問 NFS 服務器。如果要在 Linux 計算機掛載存儲,運行以下命令:you@desktop:~ $ sudo mkdir /nas/data you@desktop:~ $ sudo mount -t nfs <raspberry-pi-hostname-or-ip>:/nas/data /nas/data/<raspberry-pi-hostname-or-ip>同樣,我建議使用 autofs 來掛載該網絡設備。如果需要其他幫助,請參看 如何使用 Autofs 來掛載 NFS 共享 。現在你可以在遠程設備上通過 NFS 系統訪問位於你樹莓派 NAS 雲盤上的數據了。在後面一篇文章中,我將介紹如何使用 rsync 自動將數據備份到第二個 USB 磁盤驅動器。你將會學到如何使用 rsync 創建增量備份,在進行日常備份的同時還能節省設備空間。