# 為私有雲建立raid磁碟

**要作為正式用途，例如家裡老舊電腦用來作家中儲存雲，我們需要利用先前介紹的資料映射方法，將上傳的資料存於外部目錄，與容器本身分離，避免勿刪容器導致所有資料不見**<br>

**讓nextcloud的docker容器成為執行環境，所有資料則儲存於外部raid模式的硬碟，這樣，就可以建構簡易的家庭或學校的儲存雲**

**製作raid1的磁碟**

**當我們將ubuntu系統與docker安裝於一個系統跌後，我們可以使用兩顆空硬碟，並把他們做成raid1格式，然後作為nexttcloud資料存放之處，可有效保護資料安全**

**我們可以先關閉VM，然後在上面建立兩顆空磁碟做為模擬使用**

![](https://lh5.googleusercontent.com/UJoifTMSpPLWJWaLwwGGMfJwNHWQ4Bmq2wD0zbEtZ3OBg-klqgYeQfqtLG2zn1-O3HKx7qMQ8JthBU1bUGUOUr9jFhtyIrIu4Wiy42L-DaER_qFjimRuwwfQ-Sf265geJhRv4ITl)

**重新啟動ubuntu**

**sudo fdisk -l**

**Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors**\
\
**Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors**<br>

**兩顆空磁碟，分別為sdb sdc**<br>

**製作raid1**

**sudo fdisk /dev/sdb**\
\
**disk table複製**

```
sudo sfdisk -d /dev/sdb | sudo sfdisk /dev/sdc
```

**# metadata 鏡像，資料也鏡像**\
**sudo mkfs.btrfs -m raid1 -d raid1 /dev/sdb1 /dev/sdc1 -L bt0**

**sudo btrfs fi show**\
**Label: 'bt0'  uuid: 994bc666-5d3a-45f9-99e4-ef9e64931966**\
&#x20;**Total devices 2 FS bytes used 186.21MiB**\
&#x20;**devid    1 size 10.00GiB used 2.01GiB path /dev/sdb1**\
&#x20;**devid    2 size 10.00GiB used 2.01GiB path /dev/sdc1**\ <br>

**測試btrfs raid1鏡像功能**

**掛載其中一個 device 就可以了：**

**sudo mkdir /mnt/raid**\
**sudo mount /dev/sdb1 /mnt/raid**

**cd /mnt/raid**

**產生多個檔案 test1 test2 ......**

**sudo touch test{1..100}**<br>

**測試作為raid1的另一顆磁碟sdc1是否有相同的檔案**

**sudo mkdir /mnt/temp**

**sudo mount /dev/sdc1 /mnt/temp**

**cd /mnt/temp**

**ls後你可以看到相同的檔案產生**<br>

**設定開機自動掛載**&#x20;

**sudo vi /etc/fstab**

**加入這行設定**

**UUID=994bc666-5d3a-45f9-99e4-ef9e64931966  /mnt/raid btrfs defaults 0 2**\ <br>

**擴充：後來多一顆硬碟**

<div align="left"><img src="/files/-LkcKyMxpYkPDk7IfOzw" alt=""></div>

**sudo fdisk -l**

![](/files/-LkcLSFebriC9-XjyCpV)

```
sudo sfdisk -d /dev/sdb | sud0 sfdisk /dev/sdd
sudo btrfs device add /dev/sdd1 /mnt/raid1

sudo btrfs fi show
Label: 'bt0'  uuid: 2cf15b5b-63b2-420a-afa8-e5ec4af595e4
    Total devices 3 FS bytes used 640.00KiB
    devid    1 size 10.00GiB used 2.01GiB path /dev/sdb1
    devid    2 size 10.00GiB used 2.01GiB path /dev/sdc1
    devid    3 size 10.00GiB used 0.00B path /dev/sdd1

sudo btrfs balance start --full-balance /mnt/raid1
Done, had to relocate 3 out of 3 chunks
```

\
**失效回充：忽然硬碟壞掉**

**假設今天sdb壞了硬碟都讀不到，假設原sdc1硬碟損毀**\
\
**btrfs fi show 中顯示 \*\*\* Some devices missing**\
\
**mount -t btrfs -o degraded /dev/sdb1 /root/temp     #把sdb1掛在/mnt下**\
\
**btrfs device add /dev/sdc1 /root/temp                        #將新的sdc1加入**\
\
**btrfs device delete missing /root/temp                        #移除消失的device**\
**或**\
**btrfs device delete 1 /root/temp #1是missing的deviceid**\
\
\
**sudo btrfs balance start /root/temp                                       #平衡硬碟間的資料**\ <br>

**修復完成，重新開機**<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kawsing.gitbook.io/opensystem/docker-cong-an-zhuang-dao-ying-yong-ru-men-pian/wei-si-you-yun-jian-li-raid-ci-die.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
