8bd3733349c577834993ea5ae2eb07da3500b9ca
[ikiwiki.git] / docs / how_to_implement_hammer_pseudo_file_system__40___pfs___41___slave_mirroring_from_pfs_master / index.mdwn
1 [[!toc]]
2 #Scenario
3 I have two 500 GB hard disks both with the Hammer file system. I want to create a master PFS in one hard disk and a slave PFS in the other disk. I want to mirror the data continuously from the master PFS to the slave PFS. This will help me avoid long 'fsck' and RAID parity rewrite times after an unclean shut down, and also will give me a setup somewhat like RAID 1.
4
5 #Preparing the Disks
6
7 In this example we will be using ad4s1h and ad6s1h but you're disks are most likely going to be different. To find your disks you can scan through dmesg.
8     
9     $ dmesg | less
10
11 You can then press / and type "ad" or "da" (without the quotes) depending on the type of controller you have.
12
13 ##Creating Disk Labels
14 If your disks are brand new they'll need to be formatted as either GPT or MBR prior to creating the slices/partitions. Here are some resources for creating a disklabel and adding a slice/partition on each disk.
15
16 <https://www.dragonflybsd.org/docs/handbook/UnixBasics/#index22h3>
17
18 <https://www.dragonflybsd.org/~labthug/handbook/disks-adding.html>
19
20 ##Encryption (Optional)
21 This step is not necessary but if you are looking to encrypt your data you'll have to do it now. You can either encrypt your disks with a password a key or both I will show an example of each below:
22
23
24 ###Password (you will be prompted for your password at boot)
25 ####Create encrypted containers with password
26
27     # cryptsetup luksFormat /dev/ad4s1h
28     # cryptsetup luksFormat /dev/ad6s1h
29
30 ####Open encrypted container with password
31
32     # cryptsetup luksOpen /dev/ad4s1h crypt-master
33     # cryptsetup luksOpen /dev/ad6s1h crypt-slave
34
35
36 ###Keyfile (keyfiles on removable media are outside the scope of this guide.)
37 ####Create a keyfile and use it to create encrypted containers
38
39     # mkdir /root/keys && chmod 700 /root/keys
40     # dd if=/dev/urandom of=/root/keys/data.key bs=512 count=4
41     # chmod 400 /root/keys/data.key
42     # cryptsetup luksFormat /dev/ad4s1h /root/keys/data.key
43     # cryptsetup luksFormat /dev/ad6s1h /root/keys/data.key
44
45 ####Open encrypted containers with keyfile
46
47     # cryptsetup luksOpen /dev/ad4s1h --key-file /root/keys/data.key crypt-master
48     # cryptsetup luksOpen /dev/ad6s1h --key-file /root/keys/data.key crypt-slave
49
50
51 ###Locating Serial Numbers
52 Locate the Serial Number for your disk partitions/slices. To find the serno you can look in dmesg and match it with the entries in /dev/serno. After locating them add them to the /etc/crypttab.(if you are unable to copy and paste you can do the following and then just remove what you don't need)
53     
54     # ls /dev/serno >> /etc/crypttab
55
56
57 ###Edit /etc/crypttab with your editor of choice and add the following lines:
58 ####Password
59
60     crypt-master /dev/serno/WD-WCC3F0PLTCZD.s1h none none
61     crypt-slave  /dev/serno/WD-WCC6Y1AEVTK0.s1h none none
62
63
64 ####Keyfile
65
66     crypt-master /dev/serno/WD-WCC3F0PLTCZD.s1h /root/keys/data.key none
67     crypt-slave  /dev/serno/WD-WCC6Y1AEVTK0.s1h /root/keys/data.key none
68
69
70 ###Notice
71 If you have chosen to encrypt your data mirror you will need to make your HAMMER file systems on the newly created encrypted volumes instead of the slices/partitions themselves. So instead of /dev/ad4s1h and /dev/ad6s1h below you would be using /dev/mapper/crypt-master and /dev/mapper/crypt-slave respectively.
72
73
74 #Creating HAMMER file system
75
76     newfs_hammer -L DATA /dev/ad4s1h
77     newfs_hammer -L DATA /dev/ad6s1h
78
79 #Creating the master PFS on Disk 1
80
81 The Hammer file systems on Disk 1 and Disk 2 are mounted in '/etc/fstab' according to the following.
82
83     /dev/ad4s1h             /Backup1        hammer  rw              2       2
84     /dev/ad6s1h             /Backup2        hammer  rw              2       2
85
86 Go to the Hammer file system on Disk 1. We will be creating a master PFS called 'test' and will be mounting it using a null mount. If you don't have a directory called 'pfs' under the Hammer file system you should create it.
87
88     # pwd
89     /Backup1
90     # mkdir pfs
91
92 If you already have the pfs directory under the Hammer file system you can skip the above step and continue.
93
94     # hammer pfs-master /Backup1/pfs/test
95     Creating PFS #3 succeeded!
96     /Backup1/pfs/test
97     sync-beg-tid=0x0000000000000001
98     sync-end-tid=0x000000013f644ce0
99     shared-uuid=9043570e-b3d9-11de-9bef-011617202aa6
100     unique-uuid=9043574c-b3d9-11de-9bef-011617202aa6
101     label=""
102     prune-min=00:00:00
103     operating as a MASTER
104     snapshots dir for master defaults to <fs>/snapshots
105
106     
107 Now the master PFS 'test' is created. Make a note of its 'shared-uuid' because we will need to use that to create the slave PFS for mirroring.
108 You can mount the PFS under the Hammer file system on Disk 1 by doing the following.
109
110     # mkdir /Backup1/test
111
112 Now Edit '/etc/fstab' to contain the following line.
113
114     /Backup1/pfs/test      /Backup1/test    null    rw              0       0
115
116 Now mount the PFS by doing.
117
118     # mount -a
119     # mount |grep test
120     /Backup1/pfs/@@-1:00003 on /Backup1/test (null, local)
121
122 #Creating the slave PFS on Disk 2. 
123
124 Note that we must use the 'shared-uuid' of the master PFS to enable mirroring.
125     
126     # hammer pfs-slave /Backup2/pfs/test shared-uuid=9043570e-b3d9-11de-9bef-011617202aa6
127     Creating PFS #3 succeeded!
128     /Backup2/pfs/test
129     sync-beg-tid=0x0000000000000001
130     sync-end-tid=0x0000000000000001
131     shared-uuid=9043570e-b3d9-11de-9bef-011617202aa6
132     unique-uuid=97d77f53-b3da-11de-9bef-011617202aa6
133     slave
134     label=""
135     prune-min=00:00:00
136     operating as a SLAVE
137     snapshots directory not set for slave
138
139
140 The slave PFS is not mounted but a symlink can be created in the root Hammer file system to point to it.
141
142     # ln -s /Backup2/pfs/test /Backup2/test
143     # ls -l /Backup2/test
144     lrwxr-xr-x  1 root  wheel  17 Oct  8 12:07 /Backup2/test -> /Backup2/pfs/test
145
146 (This step is optional, the PFS can be read through the original magic symlink /Backup2/pfs/test.)
147
148 #Copying contents from PFS on Disk 1 to PFS on Disk 2 to enable mirroring. 
149
150 The slave PFS will be accessible only after the first 'mirror-copy' operation.
151
152     # touch /Backup1/test/test-file
153     # ls /Backup1/test/
154     test-file
155     # sync
156
157 We do the "sync" so that the file creation operation in flushed from the kernel memory. Mirroring works only on operations flushed from the kernel memory. The slave PFS will be accessible only after the first mirroring operation.
158
159     # hammer mirror-copy /Backup1/test /Backup2/pfs/test
160     histogram range 000000013f6425fd - 000000013f644d60
161     Mirror-read: Mirror from 0000000000000002 to 000000013f644d60
162     Mirror-read /Backup1/test succeeded
163
164     # ls /Backup2/test/
165     test-file
166
167 #Enabling continuous mirroring.
168
169 The hammer mirror-stream will automatically restart if the connection is lost so you only need to start it up once at boot.You can do this with an @reboot entry in the crontab.
170
171     @reboot root hammer mirror-stream /Backup1/test /Backup2/test
172
173
174
175