Setting Up RAID and Migrating MythTV Recordings
Summary
This tutorial details the process of setting up RAID on a Mythbuntu
12.04 installation and then migrating the existing MythTV recordings
to the RAID.
This tutorial is the third in a mini series that started
with Why Consider RAID for
PVR Recordings and Solid State Drive for Mythbuntu Operating
System, and proceeded
to Migrating
Existing MythTV 0.24 on HDD to MythTV 0.25 on SSD.
The original series of articles that outline the path of creating a
custom-built PVR can be found at the following links:
- Cutting the cord on cable TV
- Selecting hardware for a MythTV PVR
- Installing Mythbuntu 12.04.2 with MythTV 0.25
- Configuring MythTV automatic wakeup and shutdown
Contents
Strategy for Migrating MythTV Recordings to RAID
At this point in this RAID mini series, Mythbuntu 12.04 is installed
and running from a new separate drive (SSD), and the original hard
drive contains the active MythTV recordings and a no-longer used
Mythbuntu 11.04. The strategy is to install a new hard drive,
configure Mythbuntu to work with Linux Software RAID, build half of
the RAID, migrate the MythTV recordings to the new RAID (half), setup
RAID monitoring, then finish up by adding the original drive to the
RAID.
Note that when building a RAID, it is important to have identically
sized storage areas, and drives with the same sector size. To place
the storage area size under my control, I recommend partitioning the
drive and then creating the RAID using a partition. Moreover I think
it is a good idea to partition all devices to ensure that partition
editors do not view the drive as unpartitioned and hence empty, which
might result in accidentally overwriting the drive.
TIP: Perform Steps When Not Recording Shows |
Now onto the setting up the RAID migrating the PVR recordings.
Install New Hard Drive (HDD) in PVR
In my computer I plugged the drives into the SATA ports in the following order:
- /dev/sda - new SSD for Mythbuntu 12.04 OS.
- /dev/sdb - original HDD containing Mythbuntu (11.04) and recordings.
- /dev/sdc - new HDD for mirrored RAID storage of recordings.
- /dev/sr0 - original DVD R/W optical drive.
Following is a picture of my PVR with the SSD (middle left) and two
HDDs (lower right) installed.
Install and Configure Packages for Linux Software RAID
- Install Linux Software RAID package.
sudo apt-get install mdadm
TIP: Use TAB or ARROWS to navigate prompts, and ENTER to accept.
- At first Package Configuration screen:
TAB to "Ok" and press ENTER. - At second Package Configuration screen:
ARROW UP to "No configuration", and TAB to "Ok" and press ENTER.
- At first Package Configuration screen:
- Fix problem
that Ubuntu
12.04 can't reliably boot with SW RAID.
- Edit
/usr/share/initramfs-tools/scripts/mdadm-functions.
sudo leafpad /usr/share/initramfs-tools/scripts/mdadm-functions
Add the "udevadm settle" line as indicated below:
{
udevadm settle
mdadm --misc --scan --detail --test >/dev/null 2>&1
return $((! $?))
}
- Rebuild the current initramfs.
sudo update-initramfs -u
- Edit
/usr/share/initramfs-tools/scripts/mdadm-functions.
- Ensure boot from degraded RAID
- Edit /etc/initramfs-tools/conf.d/mdadm.
sudo leafpad /etc/initramfs-tools/conf.d/mdadm
Change:
BOOT_DEGRADED=false
To:
BOOT_DEGRADED=true
NOTE: RAID Monitoring Suggested
In the default configuration, Mythbuntu will not boot if the RAID is in a degraded state (for example, one drive has failed). Hence when a drive in the RAID fails, we will become aware of this by Mythbuntu failing to fully boot up.
Since we change the configuration to continue with boot even in a degraded state, we need another way to know when there are problems with the RAID. In a later section we describe how to add basic RAID monitoring.
- Rebuild the current initramfs.
sudo update-initramfs -u
- Edit /etc/initramfs-tools/conf.d/mdadm.
- Install gparted graphical partitioning software.
sudo apt-get install gparted
NOTE: Mythbuntu 12.04 uses older GParted 0.11.0
This older version of GParted does not properly detect Linux Software RAID drives. As such to partition the RAID you will need to pass the RAID name. For example:
sudo gparted /dev/md0
Setup New Hard Drive as Half of RAID Level 1 Mirror
The Linux Software RAID commands are very flexible. These commands
support building a complete two drive RAID mirror directly. Further
the commands also support building one half of the RAID mirror (a RAID
in a degraded state), and then later adding the other half of the RAID
mirror. Since I needed to migrate my existing recordings to the RAID
I used the latter method. In these instructions I will also point out
how you could build the RAID mirror directly with both drives in one
command.
In this example I have the following three drives:
- /dev/sda - new SSD for Mythbuntu 12.04 OS.
- /dev/sdb - original 1 TB HDD containing recordings and
inactive Mythbuntu 11.04.
- /dev/sdc - new 1 TB HDD for mirrored RAID storage of recordings.
- Add the additional hard drive to your PVR.
Ideally the hard drives should be the same size. This is not critical though as we will use partitions to define the size of the RAID. Hence a 2 TB drive could be configured with a smaller 1 TB partition so that it could match another 1 TB drive with a 1 TB partition. - Partition the new drive (For example: /dev/sdc).
Run GParted from a terminal window using the following command:
sudo gparted
- Select the /dev/sdc device using the upper right-hand-side drop-down list.
- Use Device -> Create Partition Table to write an msdos (MBR) partition table.
- Select the unallocated space.
- Use Partition -> New to create one new
unformatted primary partition spanning the drive.
NOTE: On my 1 TB drive this is 931.51 GiB or 953,869 MiB. - Use Edit -> Apply All Operations to apply this operation.
- Close the Applying pending operations window.
- Use Partition -> Manage Flags to set
"raid" flag.
- Exit gparted.
- Create half of the RAID level 1 mirror (degraded
RAID) on the new drive partition (For example: /dev/sdc1).
sudo mdadm --create /dev/md0 --auto=p --level=1 \
--raid-devices=2 missing /dev/sdc1
NOTE: How-to set up both drives in the mirrored RAID at once (For example: sdb1 and sdc1)
To set up both drives at once, ensure that both drives contain the exact same size partitions and use a command such as:
sudo mdadm --create /dev/md0 --auto=p --level=1 \
--raid-devices=2 /dev/sdb1 /dev/sdc1
In my experience creating only half of a RAID level 1 is quick because the step to mirror the RAID from one drive to another has been postponed until later.
When adding the second 1 TB drive in the RAID level 1 mirror, it took 3 hours to mirror the RAID from one drive to another.
You can use the watch command to frequently check the progress of building the RAID:
watch -n 60 cat /proc/mdstat
Use Ctrl+C to stop watching the progress.
- Partition the (degraded) RAID.
sudo gparted /dev/md0
- Use Device -> Create Partition Table to write an msdos (MBR) partition table.
- Select the unallocated space.
- Use Partition -> New to create one new
ext4 primary partition spanning the drive with label
"data".
NOTE: On my 1 TB partitioned drive this is 953,738 MiB. - Use Edit -> Apply All Operations to apply this operation.
- Close the Applying pending operations window.
- Exit gparted.
- Update /etc/mdadm/mdadm.conf to re-assemble RAID
with name md0 on boot.
- Add the RAID configuration to the end of the
/etc/mdadm/mdadm.conf file with the following command:
sudo mdadm --detail --scan >> /etc/mdadm/mdadm.conf
This should add a line like the following to the end of mdadm.conf:
ARRAY /dev/md/0 metadata=1.2 UUID=8d8a88cc:e239f398:8c202a01:d3750e45 name=mythpvr:0
NOTE: If you run this command multiple times, then you will need to edit /etc/mdadm/mdadm.conf to remove the duplicate entries. - Rebuild the current initramfs.
sudo update-initramfs -u
- Add the RAID configuration to the end of the
/etc/mdadm/mdadm.conf file with the following command:
Copy Recordings to RAID
- Copy recordings from source partition to new
(degraded) RAID level 1 partition.
In this example I assume that your recordings and such are stored in /data/mythtv/.... If not, feel free to substitute your own mount points where appropriate.
- Mount the RAID partition /dev/md0p1 at /mnt.
sudo mount /dev/md0p1> /mnt
- Copy the mythtv data to the RAID.
sudo cp -p -r -v /data/mythtv /mnt
NOTE: Depending on the amount of recordings you have this might take a long time. - Unmount the RAID partition from /mnt.
sudo umount /mnt
- Mount the RAID partition /dev/md0p1 at /mnt.
- Change to mount new RAID partition at /data
- Unmount /data.
sudo umount /data
- Edit /etc/fstab entry to mount /data from new RAID
sudo leafpad /etc/fstab
Change existing /data mount entry from something like the following:
# /data on /dev/sda3 UUID=2f02b4a8-acfa-4a09-b809-73d1823d1d01 /data ext4 defaults 0 2
To an entry to mount the RAID partition like the following:
# /data on /dev/md0p1 /dev/md0p1 /data ext4 defaults 0 2
- Test mounting the directory with:
sudo mount -a
- Unmount /data.
Setup RAID Monitoring [Optional]
At this point we have setup a degraded RAID which means that only half
of the RAID mirror is operational. This is a great time to also setup
and test RAID monitoring.
We desire RAID monitoring so that we will be notified when one of the
RAID hard drives fails. Without RAID monitoring, a problem with RAID
hard drive failure might go unnoticed until the last half of the RAID
failed. At such a point all of the MythTV recordings would be
lost.
In the following steps we setup a simple visual indication for when
the RAID is running in a degraded state.
- Create script for mdadm to notify of RAID
problems.
- Create new script file
/usr/bin/mdadm-notify-mythtv.sh
sudo leafpad /usr/bin/mdadm-notify-mythtv.sh
and paste in the following contents:
---------- begin file /usr/bin/mdadm-notify-mythtv.sh ----------
#!/bin/bash # Purpose: To notify user of problems with Linux software RAID # Called by: /etc/mdadm/mdadm.conf # Parameters: 1 - Event, 2 - Device, 3 - Optional Component # # # Write message to MythTV Frontend / MythWelcome # mythutil --message \ --message_text="RAID Status :: $1 :: $2 :: $3" \ --timeout=5 \ --bcastaddr=127.0.0.1 # # Write message to Desktop # notify-send 'RAID Status' "$1 :: $2 :: $3" \ --expire-time=5000
---------- end file /usr/bin/mdadm-notify-mythtv.sh ----------
- Ensure script file is executable.
sudo chmod a+x /usr/bin/mdadm-notify-mythtv.sh
- Create new script file
/usr/bin/mdadm-notify-mythtv.sh
- Add notification script to mdadm configuration.
- Edit /etc/mdadm/mdadm.conf:
sudo leafpad /etc/mdadm/mdadm.conf
add the following two lines:
# notify if problems with RAID
PROGRAM /usr/bin/mdadm-notify-mythtv.sh
The resulting file should look something like the following:
---------- Begin example file mdadm.conf ----------
# mdadm.conf # # Please refer to mdadm.conf(5) for information about this file. # # by default (built-in), scan all partitions (/proc/partitions) and all # containers for MD superblocks. alternatively, specify devices to scan, using # wildcards if desired. #DEVICE partitions containers # auto-create devices with Debian standard permissions CREATE owner=root group=disk mode=0660 auto=yes # automatically tag new arrays as belonging to the local system HOMEHOST <system> # instruct the monitoring daemon where to send mail alerts MAILADDR root # definitions of existing MD arrays ARRAY /dev/md/0 metadata=1.2 UUID=8d8a88cc:e239f398:8c202a01:d3750e45 name=mythpvr:0 # notify if problems with RAID PROGRAM /usr/bin/mdadm-notify-mythtv.sh # This file was auto-generated on Thu, 11 Apr 2013 12:58:58 -0600 # by mkconf $Id$
---------- Begin example file mdadm.conf ----------
- Rebuild the current initramfs:
sudo update-initramfs -u
- Test notification with:
sudo mdadm --monitor --scan --oneshot --test 2>/dev/null
When I ran my tests, I was only able to see a visual indication of any RAID problems viewing the XFCE desktop. While the MythTV Frontend was running, or when viewing a recording, I did not see a visual indication of RAID problems until after I exited the MythTV Frontend.
- Edit /etc/mdadm/mdadm.conf:
- Create a root crontab file to automatically have
mdadm monitor the RAID at a time when you usually watch TV.
I choose 8:35pm or 20:35 in 24 hour time format.
- Start up the crontab file editor using the
leafpad editor.
sudo sh -c "export VISUAL=leafpad && crontab -e"
- Add and save the following three lines at the
end of the file:
---------- Begin three crontab lines ----------
# # Monitor mdadm RAID and notify if problems 35 20 * * * /sbin/mdadm --monitor --scan --oneshot 2>/dev/null
---------- End three crontab lines ----------
- Confirm the crontab file by listing the contents.
sudo crontab -l
- Start up the crontab file editor using the
leafpad editor.
Add Remaining Half of RAID Level 1 Mirror
At this point the PVR is fully operational on a degraded RAID. The original drive is not used because we have migrated the Mythbuntu OS to an SSD, and migrated the recordings to the new HDD RAID. The final step is to repurpose the original hard disk drive for use as the other half of the RAID level 1 mirror.
CAUTION: Original Hard Drive WILL BE OVERWRITTEN! |
- Repartition the original drive (For example: /dev/sdb).
Run GParted from a terminal window using the following command:
sudo gparted
- Select the /dev/sdb device using the upper right-hand-side drop-down list.
- Use Device -> Create Partition Table to write a new empty msdos (MBR) partition table.
- Select the unallocated space.
- Use Partition -> New to create one new
unformatted primary partition spanning the drive.
NOTE: On my 1 TB drive this is 931.51 GB or 953,869 MiB. - Use Edit -> Apply All Operations to apply this operation.
- Close the Applying pending operations window.
- Use Partition -> Manage Flags to set
"raid" flag.
- Exit gparted.
- Add this newly created partition to complete both
halves of the RAID level 1 mirror.
sudo mdadm /dev/md0 -a /dev/sdb1
NOTE: Monitoring RAID Build Progress
When adding the second 1 TB drive in the RAID level 1 mirror, it took 3 hours to mirror the RAID from one drive to another.
You can use the watch command to frequently check the progress of building the RAID:
watch -n 60 cat /proc/mdstat
Use Ctrl+C to stop watching the progress.
Note that there are methods suggested on the Internet to increase speed if necessary. Unfortunately I did not find any that worked for me.
RAID Migration Complete
Congratulations, you have now configured your MythTV PVR to store
recordings on RAID level 1 mirrored hard drives. Now you can benefit
from an easier recovery from a failed RAID drive without losing any
recordings. :-)
Since I recently experienced problems with one of my RAID hard drives
(after 995 days of operation), I plan to document how I discovered the
problem and subsequently replaced the failing RAID drive.
References
While setting up RAID for the Mythbuntu MythTV PVR recordings, I found the following references useful:
- Ubuntu 12.04 can't reliably boot with software RAID (and why).
- How can I make mdadm auto-assemble RAID after each boot?
- Convert a single drive system to RAID
Future Articles
In future articles, I plan to cover the following steps of our path: