summaryrefslogtreecommitdiff
path: root/articles/mdadm-raid.html
blob: dd2a014897dc606dad4e6f32d7fc1e9c9358727d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!DOCTYPE html>
<html lang=en>
    <head>
        <title></title>
        <meta charset="utf-8"/>
        <link rel="shortcut icon" href="favicon.ico"/>
        <link rel='stylesheet' href='../style.css'/>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
<body>
    <header><h1>Linux Software RAID</h1></header>
    <main>
			<p><strong>mdadm</strong> is a tool that allows for creation and 
			management of software RAID arrays on Linux. Creating an array 
			is a rather straightforward process.</p>

			<h2>Install packages</h2>
			<p>The only package needed is mdadm itself</p>
			<pre><code>apt install mdadm</code></pre>

			<h2>Partition disks</h2>
			<p>We'll need to parition the disks to be used in the array before 
			creating it. This isn't anything complicated, we will just be 
			creating a single partition using all the space on each disk.</p>

			<p>Use <strong>lsblk</strong> to get a list of disks attached to your system.</p>
			
			<img src=../images/raid/lsblk.png alt="lsblk command">

			<p>Then use <strong>fdisk</strong> to edit the partition table of the 
			first disk. In my case this would be <strong>/dev/sdb</strong>. 
			Be sure that you are selecting the correct disk as selecting the 
			wrong one can result in data being lost.</p>

			<pre><code>fdisk /dev/sdb</code></pre>

			<p>Use <strong>g</strong> to create a new GUID partition table.
			Use <strong>n</strong> to create a new partition, and then just press 
			enter at all of the prompts to accept the defaults. Finally use 
			<strong>w</strong> to write the changes. You can use lsblk again 
			to verify the change and you should see that /dev/sdb now has a 
			partition <strong>/dev/sdb1</strong>.</p>

			<img src=../images/raid/fdisk.png alt="fdisk command">

			<p>Repeat this process for your other disks before continuing.</p>


			<h2>Create the array</h2>
			<p>Creating the array is done with a single command, but takes just a 
			bit of planning.</p>
			<ul>
					<li>Define an ID for the array, which is just a 
					number identifier. I use <em>0</em> in the command.</li>
					<li>Determine the RAID level you want to use. I am going to 
					use RAID5 in this example. The argument to --level is the 
					number of the RAID level</li> 
					<li>Determine the devices that will be used in the array. 
					Somewhat contrary to the argument name, these devices 
					will actually be the partitions of the disks and not the 
					disks themselves. In the command, give the number of 
					partitions (<em>3</em>) and then a space-separated list of the 
					partitions that will be active in the array 
					(<em>/dev/sdb1 /dev/sdc1 /dev/sdd1</em>).</li> 
					<li>If you want to have any spare devices in the array you 
					will define them with the --spare-devices argument. These are 
					defined in the exact same way as the active RAID devices. In 
					the example I use <em>1</em> spare <em>/dev/sde1</em>. Spare 
					devices are hot-spares that will automatically be inserted into 
					the array if one of the disks fails.</li> 
			</ul>

			<pre><code>mdadm --create /dev/md<em>0</em> --level=<em>5</em> --raid-devices=<em>3</em> <em>/dev/sdb1 /dev/sdc1 /dev/sdd1</em> --spare-devices=<em>1</em> <em>/dev/sde1</em></code></pre>

			<p>After creating the array run the following command to get details 
			and the status of the array. It will take a bit to initialize the 
			array, you will know this is done when the state is clean. You do not 
			need to wait for the array to completely intialize to continue.</p>

			<pre><code>mdadm --detail /dev/md<em>0</em></code></pre>

			<p>Take note of the UUID and name values as we will need them in the 
			next step.</p>

			<p>Before moving on we need to make a filesystem on the array. I'm 
			going to make a simple EXT4 filesystem here.</p>

			<pre><code>mkfs.ext4 /dev/md<em>0</em></code></pre>

			
			<h2>Configuration Files</h2>
			
			<p>Open the mdadm configuration file at 
			<strong>/etc/mdadm/mdadm.conf</strong> and append this line. Replace 
			<em>uuid</em> and <em>name</em> with the values you got when running 
			mdadm --detail, replace <em>0</em> whatever ID you chose.</p>

			<pre><code>ARRAY /dev/md<em>0</em> metadata=1.2 UUID=<em>uuid</em> name=<em>name</em></code></pre>

			<p>Optionally, create an <strong>/etc/fstab</strong> entry for 
			automounting of the array. Replace /mnt/raid with the directory 
			where you want to mount the array. If you made a filesystem other than 
			ext4 make sure to change that value.</p>

			<pre><code>/dev/md<em>0</em> /mnt/raid ext4 defaults 0 1</code></pre>

<p>
<hr>
Consider <a href=../donate.html>donating</a> if this article was useful.
<a class=qr href=../images/bitcoin.png>[BTC]</a>
</p>
 	</main>
    <footer>
	<a href=../kb.html>Knowledge Base</a>
	<br>
	<a href=../index.html>www.chudnick.com</a>
	</footer>
</body>
</html>