Skip to main content.

RAID in FreeBSD and OpenBSD

2014-05-07

Live demo in BSD Now Episode 036 | Originally written by TJ for bsdnow.tv | Last updated: 2014/05/07

NOTE: the author/maintainer of the tutorial(s) is no longer with the show, so the information below may be outdated or incorrect.

Data integrity is a complex, multi-step process. Local backups, offsite backups and fault tolerance all play key roles in keeping your files safe. The easiest way to keep your data safe from hard drive failure is by using RAID. There are different levels of RAID, each with their own different goals and caveats. This tutorial will be showing RAID1, or "mirrored" drives. By installing your OS onto a RAID1 array, you can withstand a single hard drive dying. The system will even keep running normally after a drive has died, but at that point you need to look into replacing the failed drive and rebuilding the array. OpenBSD's bioctl command also has options for other RAID levels. For FreeBSD, you'd want to check the gstripe and graid commands.


FreeBSD

FreeBSD has two filesystems you can choose from: UFS and ZFS. As of 10.0, the installer supports an easy ZFS installation method that's capable of doing RAID1. This section of the tutorial is focused only on UFS, which is a more manual and involved process. Boot up the installer and go through the prompts until you get to the following screen:

We'll choose "shell" here and set up the array manually. This is assuming you have a machine capable of booting from GPT and you have two drives in the system. In my case "ada0" and "ada1" were the device names. The gpart command is the main tool we'll be using.

# gpart create -s gpt ada0
# gpart add -s 64k -t freebsd-boot -l ada0boot ada0
# gpart add -t freebsd-ufs -l ada0root ada0
# gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ada0

Repeat for the second drive.

# gpart create -s gpt ada1
# gpart add -s 64k -t freebsd-boot -l ada1boot ada1
# gpart add -t freebsd-ufs -l ada1root ada1
# gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ada1

Now we'll load the kernel module and actually create the mirror with gmirror.

# kldload geom_mirror
# gmirror label boot /dev/gpt/ada0boot /dev/gpt/ada1boot
# gmirror label root /dev/gpt/ada0root /dev/gpt/ada1root

Format the new mirror so we can actually install the OS to it. We'll mount it at /mnt so bsdinstall will recognize it after we're done.

# newfs -U /dev/mirror/root
# mount /dev/mirror/root /mnt

We need an /etc/fstab as well, so let's go ahead and create that.

# echo '/dev/mirror/root / ufs rw,noatime 1 1' > /tmp/bsdinstall_etc/fstab
# exit

The installer should start untarring the files to the new RAID1 array and do most of the work for you. Choose "yes" when asked "would you like to open a shell in the new system to make any final manual modifications?"

# echo 'geom_mirror_load="YES"' >> /boot/loader.conf
# exit

If you forget this step, your new system won't boot. Reboot into the new installation and check the status like so:

# gmirror status

       Name    Status  Components
mirror/boot  COMPLETE  ada0p1 (ACTIVE)
                       ada1p1 (ACTIVE)
mirror/root  COMPLETE  ada0p2 (ACTIVE)
                       ada1p2 (ACTIVE)

You're ready to go.


OpenBSD

Setting up a mirror in OpenBSD is quite easy. Boot up the installer and go to shell mode.

Welcome to the OpenBSD/i386 5.5 installation program.
(I)nstall, (U)pgrade, (A)utoinstall or (S)hell? s

By default, OpenBSD will only make a /dev entry for the first drive. This is assuming you have two drives: wd0 and wd1. Let's create the /dev entry for wd1 and set a disk layout.

# cd /dev
# sh MAKEDEV wd1
# dd if=/dev/zero of=/dev/wd0c bs=1m count=1
# dd if=/dev/zero of=/dev/wd1c bs=1m count=1
# fdisk -iy wd0
# fdisk -iy wd1

Next we create the partitions on the disks.

# disklabel -E wd0
Label editor (enter '?' for help at any prompt)
> a a
offset: [64]
size: [20964761]
FS type: [4.2BSD] RAID

We can do it once for wd0 and then just import that same configuration into wd1 - pretty cool!

# disklabel wd0 > bsdnowiscool
# disklabel -R wd1 bsdnowiscool
# rm bsdnowiscool

Create the mirror like so:

# bioctl -c 1 -l wd0a,wd1a softraid0
# exit

In my case, the newly-created pseudo-device was called sd0. Go through the installer as normal, but be sure to choose that device instead of the regular hard drives.

Available disks are: wd0 wd1 sd0.
Which one is the root disk? (or 'done') [wd0] sd0

Reboot and check the status of your array in the new system:

# bioctl sd0                                                                   
Volume      Status               Size Device  
softraid0 0 Online         2146656256 sd0     RAID1 
          0 Online         2146656256 0:0.0   noencl <wd0a>
          1 Online         2146656256 0:1.0   noencl <wd1a>

Or just verify through dmesg.

# grep sd0 /var/run/dmesg.boot                                                 
sd0 at scsibus3 targ 1 lun 0: <OPENBSD, SR RAID 1, 005> SCSI2 0/direct fixed
sd0: 2047MB, 512 bytes/sector, 4192688 sectors
root on sd0a (fa6620cf9195850e.a) swap on sd0b dump on sd0b

That's it.

Latest News

New announcement

2017-05-25

We understand that Michael Dexter, Brad Davis, and George Rosamond think there should be more real news....

Two Year Anniversary

2015-08-08

We're quickly approaching our two-year anniversary, which will be on episode 105. To celebrate, we've created a unique t-shirt design, available for purchase until the end of August. Shirts will be shipped out around September 1st. Most of the proceeds will support the show, and specifically allow us to buy...

New discussion segment

2015-01-17

We're thinking about adding a new segment to the show where we discuss a topic that the listeners suggest. It's meant to be informative like a tutorial, but more of a "free discussion" format. If you have any subjects you want us to explore, or even just a good name...

How did you get into BSD?

2014-11-26

We've got a fun idea for the holidays this year: just like we ask during the interviews, we want to hear how all the viewers and listeners first got into BSD. Email us your story, either written or a video version, and we'll read and play some of them for...


Episode 243: Understanding The Scheduler

2018-04-25

This episode was brought to you by Headlines OpenBSD 6.3 released Punctual as ever, OpenBSD 6.3 has been releases with the following features/changes: > Improved HW support, including: > SMP support on OpenBSD/arm64 platforms > vmm/vmd improvements: > IEEE 802.11 wireless stack improvements > Generic network stack improvements > Installer improvements > Routing daemons and other userland network improvements > Security...

Episode 242: Linux Takes The Fastpath

2018-04-18

Direct Download:MP3 AudioVideo This episode was brought to you by Headlines TrueOS STABLE 18.03 Release The TrueOS team is pleased to announce the availability of a new STABLE release of the TrueOS project (version 18.03). This is a special release due to the security issues impacting the computing world since the beginning...

Episode 241: Bowling in the LimeLight

2018-04-11

Direct Download:MP3 AudioVideo This episode was brought to you by Headlines [Other big ZFS improvements you might have missed] 9075 Improve ZFS pool import/load process and corrupted pool recovery > One of the first tasks during the pool load process is to parse a config provided from userland that describes what devices the pool is...

Episode 240: TCP Blackbox Recording

2018-04-04

Direct Download:VideoMP3 Audio This episode was brought to you by Headlines [A number of Upstream ZFS features landed in FreeBSD this week] 9188 increase size of dbuf cache to reduce indirect block decompression With compressed ARC (6950) we use up to 25% of our CPU to decompress indirect blocks, under a workload of...