This series is about how to use a Raspberry Pi to build a low-cost home server in thePrevious Post.In this article, we've installed Ubuntu and setup ssh on our Raspberry Pi, and now we're going to explore how to setup a simple Raspberry Pi NAS system. We'll start by understanding what a NAS and RAID1 are, and then go step by step through the implementation process, including installing the necessary software, formatting the hard disk, and creating a RAID1 array.
Table of Contents
What's NAS?
NAS (Network-Attached Storage) is a hardware device designed to store and share data, usually connected to your home network, allowing multiple devices to use and store files on them at the same time. The existence of such a system simplifies the process of sharing and backing up files, making it easy for home or small office users to access their data.
There are many NAS products on the market, such as Synology, which is the most popular among newbies. The price of the cheaper 2 Bay entry level version is about $$250 at the time of writing this article. This is the price before the hard disk is included.
The good thing about NAS systems on the market is that you don't need to configure them yourself, and there are a lot of good software packages available. But today we are going to set up a RAID 1 NAS with two USB external hard disks using our Raspberry Pi.
What is RAID1?
RAID (Redundant Array of Independent Disks) is a technology used for data storage that allows data to be distributed across multiple hard disks using a special data storage method. This approach improves the efficiency of data reads and writes, as well as increasing data redundancy, thereby reducing the risk of data loss.
There are different levels of RAID technology, each offering different performance and redundancy characteristics. Common RAID levels include:
- RAID 0: The Striping method improves performance without redundancy. Failure of a hard disk can result in complete data loss.
- RAID 1: Mirroring provides full redundancy, i.e., the same data is stored on two disks, which is secure but more expensive to store.
- RAID 5: Striping + distributed parity for performance and redundancy, usually requires at least three hard disks.
- RAID 6: Similar to RAID 5, but provides greater redundancy and can tolerate two drive failures.
- RAID 10 (1+0): A combination of RAID 1 mirroring and RAID 0 striping provides high performance and redundancy.
RAID typically uses multiple hard disk drives to create an array of hard disks. These hard disks can be independent disks or disks managed by a hard disk array controller. Here we will configure a RAID1 disk array using a USB external hard disk. Higher levels of RAID are not used because they are not usually needed for very high storage performance for home use, and RAID1 only requires two hard disks. A RAID configuration is not necessary, but it avoids the possibility of data loss in the event of a hard disk failure, which is a common consideration when purchasing a NAS system.
Raspberry Pi NAS Implementation Steps
First of all, let's prepare two USB hard disks of the same size to be connected to the Raspberry Pi. The hardware we use can depend on our needs, for example, we can buy two USB SSDs of the same size, the most common USB external hard disk I use is Sandisk SSD, which is light and small, but the disadvantage is that it is more expensive.
If you don't care about portability (after all, it's just a NAS at home) and want a cheaper, larger space, you can consider buying a 3.5" hard drive and using a USB dock to connect it. It is important to note that if you are going to use a large-capacity hard drive with this external dock, you need to make sure that the specifications are supported. I prefer to buy NAS Server class hard disk, you can decide the capacity class according to your own needs.
Split disk
If you are using a brand new disk, we first partition the hard disk. First use lsblk to find the labels of both hard disks.
lsblk
The result will be similar. Assuming that we have just connected a 2T hard disk, we are looking for a hard disk called sda. Assuming you don't have any other external hard disk in your Raspberry Pi system, the two hard disks should be sda and sdb.
A brand new hard disk may not have been partitioned. Disk Partitioning is the process of dividing a physical hard disk into logical areas. For a new hard disk, we need to create a partition on each of the two hard disks first. We use gdisk to accomplish this step.
sudo gdisk /dev/sda
gdisk
Detailed information about the hard disk, including partition table information if available, will be displayed. This will show you the existing partitions on the hard disk. If the hard disk is brand new or has no partitions, press the o
key to create a new split table.
Next, use the n
gdisk will ask for the start and end positions of the partition. Since we are creating a partition that will occupy the entire hard disk, just press Enter to accept the default values. The next step is to select the type of partition. You can press Enter to accept the default Linux partition type (8300). This completes the partition setup, use the w
key to write the changes to the hard disk and exit. gdisk
The If you change your mind, you can use the q
key to exit without saving the changes.
The same steps we have to do again on sdb.
sudo gdisk /dev/sdb
Option 1: General Formatting
After partitioning the disk, the next step is to format the partitioned disk. If you are not planning to do RAID1, you can format the hard disk directly with mkfs:
sudo mkfs.ext4 /dev/sda1
sudo mkfs.ext4 /dev/sdb1
Option 2: Format as a RAID1 disk array
To do RAID, before you start, you need to install the mdadm software, which is used to manage software RAID arrays.
sudo apt install madam
Use the following command to merge the two partitions into one RAID1 array /dev/md/myraid.
sudo mdadm --create --verbose /dev/md/myraid --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1
sudo mdadm --detail /dev/md/myraid
Now let's format /dev/md/myraid: /dev/md/myraid
sudo mkfs.ext4 /dev/md/myraid
Mounting partitions to folders
In order to access the available space, we must mount the formatted space on the Ubuntu system. Mounting is the process of connecting a file system (usually a hard disk partition or a file system on a storage device) to a specified directory (called a mount point) on a Linux or Unix system so that the system can access and manage the files and directories on that file system. In this case, we are going to mount a freshly formatted normal or RAID1 file system.
First prepare an empty folder.
mkdir /mnt/myraid
Then we will mount the system we just made to the path of the empty folder.
sudo mount /dev/md/myraid /mnt/myraid
Set the ownership of this folder to the original user.
sudo chown -R ubuntu:ubuntu /mnt/myraid
Any access to the /mnt/myraid folder at this point is actually file access to the attached hard disk or array.
However, this manual mount will have to be reloaded every time the computer is rebooted, so if the above tests are OK, we can go to the /etc/fstab file to set up automatic mounting:
sudo nano /etc/fstab
Add a line to the file so that the auto-mount is complete.
/dev/md/myraid /mnt/myraid ext4 defaults 0 0
Accessing the NAS using SFTP
If there is a change in thePrevious Post.In the middle, you have already set up ssh and fixed IP, actually you have finished a basic YANGCHUN NAS setup, because the ssh server has SFTP support, so you can connect to the file system that we just set up from any computer in the intranet by SFTP.
I will only provide a brief description here:
- Download and install FileZilla: Locate the other computer, and if FileZilla is not already installed, go to the official website (https://filezilla-project.org/) Download
- Open FileZilla: After the installation is complete, open the FileZilla software.
- Connect to an SFTP server: Use the following information to connect:
- Host: Enter the address of the SFTP server, this is the IP address we set for the Raspberry Pi.
- Username: Enter your username on the server.
- Password: Enter your account password.
- Port: 22
- Switch to the corresponding path∶ Once the connection is successful, you will see the files and directories on the server in the Remote Browser pane on the right. Since we're trying to access the external hard disk we just set up, fill in the path field on the right with the corresponding path from the Raspberry Pi, /mnt/myraid, and press enter.
- Transferring files:In the Local Browser pane (left) you see the local files and in the Remote Browser pane (right) you find the files on the server. To transfer files from one side to the other, simply drag and drop them into place.
Other access methods
If you want to use this RAID as a network disk that can be dragged and dropped directly, you can use Samba, an open source software suite that enables file and printer sharing between different operating systems. It allows sharing of files and resources between Linux and Unix systems (including macOS) and Windows operating systems. I won't discuss it in this article, but if you want to set up Samba and are interested, you can refer toThis one.The
Management of disk arrays
The advantage of using RAID arrays is that when one hard disk goes bad, the system can still use the other one, we just need to replace the bad one.
In the future, if you find that one of the disks is broken, you can remove it from the array. For example, let's say the sdb is broken:
sudo mdadm --manage /dev/md/myraid --remove /dev/sdb
Then replace the hard disk with a good one, partition it and add it back to the array.
sudo mdadm --manage /dev/md/myraid --add /dev/sdb
Thank you for reading this post. If you like my post, please follow up withFacebook Fan Specialist,Twitter,IGThe