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

Hi, Mr. Dexter...

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 210: Your questions, part I

2017-09-06

Direct Download:HD VideoMP3 AudioTorrent This episode was brought to you by Headlines A Reimplementation Of Netbsd Using a Microkernel Minix author Andy Tanenbaum writes in Part 1 of a-reimplementation-of-netbsd-using-a-microkernel Based on the MINIX 3 microkernel, we have constructed a system that to the user looks a great deal like NetBSD. It uses pkgsrc,...

Episode 209: Signals: gotta catch ‘em all

2017-08-30

Direct Download:HD VideoMP3 AudioTorrent This episode was brought to you by Headlines Trip Report: FreeBSD in China at COPU and LinuxCon This trip report is from Deb Goodkin, the Executive Director of the FreeBSD Foundation. She travelled to China in May 2017 to promote FreeBSD, meet with companies, and participate in discussions around Open...

Episode 208: Faces of Open Source

2017-08-23

Direct Download:HD VideoMP3 AudioTorrent This episode was brought to you by Headlines LLVM, Clang and compiler-rt support enhancements In the last month I started with upstream of the code for sanitizers: the common layer and ubsan. I worked also on the elimination of unexpected failures in LLVM and Clang. I've managed to...

Episode 207: Bridge over the river Cam

2017-08-16

Direct Download:HD VideoMP3 AudioTorrent This episode was brought to you by Headlines BSDCam recap The 2017 Cambridge DevSummit took place from 2-4 August 2017. The event took place over three days including a formal dinner at St John's College, and was attended by 55 registered developers and guests. Prior to the start of...