Monday, June 27, 2011

Setting up DNS Service Discovery

DNS Service Discovery (DNS-SD) is a component of Zeroconf networking, which allows servers and clients on an IP network to exchange their location and access details around the LAN without requiring any central configuration.

Most Linux distributions supply the Avahi library for Zeroconf support, but not nearly as many users take advantage of it.

Let's look at an easy-to-set-up use for DNS-SD: providing automatic bookmarks to services. All it takes is an Apache module and a Firefox extension.

Background

The essence of DNS-SD is that Zeroconf-supporting applications or hardware devices broadcast a DNS SRV record (of the kind typically used in static DNS to point to a host and port number combo) advertising themselves, and everyone else on the network hears it and takes note.

They make the broadcast over multicast-DNS (mDNS), which is a protocol derived from normal DNS, but using special, local-only "multicast" addresses and the reserved .local pseudo-domain.

The system is akin to Universal Plug-and-Play (UPnP), except that it handles more types of services, and builds more directly on top of DNS.

The mDNS/DNS-SD pair's major backer is Apple, and UPnP's is Microsoft, so as you might guess, neither is likely to give up and start supporting the other.

There is hope for a unifying IETF protocol in the future, but at the moment mDNS/DNS-SD is well-supported enough by the open source Avahi library that Linux users can start working with it today.

In the Apple world, printers and chat clients commonly use mDNS to advertise their availability. But there is a long list of application types that can advertise over the system, including VoIP clients and servers (such as Asterisk), closed-circuit video devices, even collaborative editors (such as Gobby).

Essentially, any service that can be described in a SRV record can be advertised; it just needs to provide a service name, a transport protocol (TCP or UDP), and the port and hostname of the server where it can be reached. The mDNS .local domain allows participating devices to assign themselves reachable hostnames.

With the server properly configured, the DNS-SD stack on any client machines will catch and catalog the local services automatically, for use by applications on the system.

On a Linux box, Avahi hears and notes the mDNS messages, and an interested client (say, a chat app) asks Avahi if there are any XMPP servers nearby to talk to. The connection is made, and voilà, you start chatting.

Discovering Some Services

It's easy to imagine how DNS-SD could take the pain out of some typically hard-to-configure applications like VoIP, but if you are new to DNS-SD there are simpler places to start, such as with good old-fashioned HTTP web servers.

If you're like me, your main Linux box is running a variety of web interfaces for local services: phpMyAdmin, CUPS administration, Webmin or another config tool (in my case, I also have an X10 home automation front-end and the MythWeb MythTV interface running).

You may also have work-related services running, such as a Bugzilla instance or network administration workspace, or even a straightforward Intranet site.

The unifying principle is that these are all web services you might like to access from more than one machine on the LAN.

You can manually enter the bookmarks on every machine, or use a synchronization tool like Firefox Sync or XMarks, but these strategies make you choose between repetitive work and potential security risks -- not to mention they require updating all of the client machines whenever there is a change. That is precisely the problem Zeroconf networking was designed to solve.

Developer Andrew Tunnell-Jones has written a small but highly useful extension that adds DNS-SD support to Firefox. The code is hosted at Github, but you can install the extension itself, "DNSSD for Firefox," through the addons.mozilla.org site.

It requires Firefox 4.0 or later (no word yet on the just-released Firefox 5; it doesn't appear that anything relevant has changed in Firefox itself, but the add-ons system is notoriously pedantic about version numbers), and a working Zeroconf implementation.

For Linux, Avahi works just fine, and Mac OS X users will already have Apple's Bonjour installed. Windows users can install the Apple-provided Bonjour-for-Windows package, which Tunnell-Jones links to from the extension page.

After you restart Firefox, the extension adds a menu labeled DNSSD to the Navigation toolbar (between the forward/back buttons and the URL bar) and to the Bookmarks menu.

Click on it, and you will see a list of all of the local HTTP servers detected by your Avahi or Bonjour service: no configuration necessary. If you want to double-check the extension's list, you can run avahi-browse --all from the command line.

If you are running GNOME, you should also see a desktop notification pop up whenever the extension notices a new service (although for most services, this will just be at start-up time). That option is configurable in the preferences, which you can get to through the Add-ons Manager.

It works, and it is automatic, but there are a few quirks to be aware of. First, you don't (yet) have the option to choose where the DNSSD menu is displayed.

Placing it in the navigation toolbar makes sense because that is the one toolbar almost guaranteed to be present, but putting it in the Bookmarks toolbar would seem to make more sense to me -- it seems like a natural complement to Firefox's automatic "Most Visited" and "Recently Added" bookmark folders.

Second, although you can access the DNSSD menu through the Bookmarks menu, you cannot move it around in your bookmarks to a more convenient location.

I asked Tunnell-Jones about both of these options, however, and it sounds like they are possibilities for future releases.

You can probably think of a handful of local web services you would like to automatically advertise around your office or home network, but the odds are that most of them do not advertise over mDNS out-of-the-box. In my case, the only running server that did provide a web interface over DNS-SD was the MT-DAAP audio server. To get your other services to announce themselves, you'll need mod_dnssd.

Location, Location, Location

Mod_dnssd is an Apache module that adds simple mDNS/DNS-SD support to your Apache-hosted sites, with a minimum of configuration fuss. The latest release is 0.6, which supports Apache 2.2, although there are older releases for those still running Apache 2.0 for some reason.


The author, Lennart Poettering, is best know as the maintainer of PulseAudio (which, yes, uses mDNS/DNS-SD to locate other networked PulseAudio sources on the LAN). The docs on the site are a nice introduction, but Poettering has written a more extensive how-to on his blog. To get it working, you'll need to install the module (packages are available on the site, but most distributions offer it as well), and make sure that Apache loads it at startup (check your distro's documentation for details, or edit your /etc/apache2/apache2.conf if installing from source).


To use the module, you must first activate it by placing the DNSSDEnable on directive in the Global Environment section of apache2.conf. With that configuration alone, Apache will advertise all of the VirtualHosts over mDNS/DNS-SD -- however, clients will have trouble connecting to them if you do not label your VirtualHosts with fully-qualified domain names.

For a little more fine-grained control, you can add a DNSSDServiceName "Whatever You Want To Advertise It As" directive to each VirtualHost or Location block.

The ServiceName you assign will be the user-visible label seen in the DNSSD menu offered by the Firefox extension, so you can give easy-to-remember, LAN-wide labels to your bug tracker, Apt-CacherNG control panel, or any other site.

But remember to include the server's name if you are running multiple web servers on the LAN, lest your users get confused.

By default, mod_dnssd advertises Apache resources as HTTP services (i.e., using the _http._tcp SRV record). That makes sense for most web services, but you can also alter it to properly advertise other applications, such as WebDAV or RSS feeds.

Simply add the DNSSDServiceTypes directive to your Apache configuration, followed by a space-separated list of the service types you wish to advertise -- either for the server as a whole, the VirtualHost, or the Location, depending on where you put the directive.

That's What I Call Service

Obviously, the Apache plus Firefox combination only scratches the surface of what DNS-SD as a whole is capable of, but as an increasing number of services use HTTP, it is at least a useful place to start. You can save yourself some trouble by DNS-SD-enabling your Intranet sites and letting your users find them automatically.

Of course, you do still need to take precautions to protect your services. The DNS-SD services will only be visible to LAN clients, but if you do not want that to include WiFi visitors, you should partition them off into a different subnet altogether -- and it goes without saying that your admin panels ought to be password-protected.

But there is another subtle condition imposed by this scheme: it requires changing Firefox, the client application.

So although it's easy to imagine DNS-SD-advertised bookmarks being useful in a public environment (say, an Internet cafe), you still cannot expect visitors wandering in off the street to have the right extension installed.

In my mind, DNS-SD, like Microformats, is a technology that Firefox really ought to support off-the-shelf.

There is no reason not to, unless you make the security argument -- but honestly, a service at risk is not any better-protected just because it is un-advertised.

Maybe Tunnell-Jones's extension will have a hand in raising awareness of the convenience offered by DNS-SD.

At the very least, you can leverage that convenience yourself, and that's a pretty good start.

Thursday, June 23, 2011

Monitoring User/Application Activity with psacct

One of the big advantages of using psacct on your server is that it provides excellent logging for activities of applications and users.  When you are running scripts one of the important aspects of that script is how much resources it may be using and are there any resource limitations that may exist with the application.  In addition, there may be times when you run a script as a user.  In other words, you create a user with specific rights, maybe even using visudo.  You will likely use this to reduce the security risks of a user who must issue a command with root privileges.
Install Process Accounting
yum install psacct
Start Process Accounting
/etc/init.d/psacct start
Starting process accounting:                               [  OK  ]
Connect Time
The connect time in hours is based on logins and logouts.  The ac command provides a total.
ac
total      256.64
Accounting By Day
The system’s default login accounting file is /var/log/wtmp.
ac -d
Nov  1  total        8.78
Nov  3  total        6.15
Nov  6  total       13.68
Nov  7  total       15.50
Nov  8  total       10.54
Nov  9  total       10.56
Nov 10  total        8.41
Nov 13  total        8.66

—cut—
Today   total       19.33
Time Totals for Users
ac -p
root                                 5.83
mike                               251.29
total      257.13

Commands of Users
You can search out the commands of users with the lastcomm command which prints out the previously executed commands.
Explanation of Output:
Process  Flag    Username    Terminal   Time
ping     S       mike        pts/5      0.00 secs Thu Nov 30 13:39
Flags:
S -     executed as super-user
F -     executed after  but not following exec
D -     terminated with core file
X -     terminated with signal SIGTERM
lastcomm mike
bash                  X     mike     __         0.04 secs Thu Nov 30 12:49
bash                  X     mike     __         0.04 secs Thu Nov 30 12:57
ping                  S     mike     pts/5      0.00 secs Thu Nov 30 13:39
nmap                        mike     pts/4      4.01 secs Thu Nov 30 13:38
ping                  S     mike     pts/5      0.00 secs Thu Nov 30 13:37
ping                  S     mike     pts/5      0.00 secs Thu Nov 30 13:34
ping                  S     mike     pts/5      0.00 secs Thu Nov 30 13:34
bash                  X     mike     __         0.03 secs Thu Nov 30 13:28
bash                   F    mike     pts/6      0.00 secs Thu Nov 30 13:28
consoletype             mike     pts/6      0.00 secs Thu Nov 30 13:28
bash                   F        mike     pts/6      0.00 secs Thu Nov 30 13:28
id                                  mike     pts/6      0.00 secs Thu Nov 30 13:28
grep                            mike     pts/6      0.00 secs Thu Nov 30 13:28
bash               F        mike     pts/6      0.00 secs Thu Nov 30 13:28
grep                        mike     pts/6      0.00 secs Thu Nov 30 13:28
bash               F        mike     pts/6      0.00 secs Thu Nov 30 13:28
egrep                       mike     pts/6      0.00 secs Thu Nov 30 13:28
bash               F        mike     pts/6      0.00 secs Thu Nov 30 13:28
dircolors                   mike     pts/6      0.00 secs Thu Nov 30 13:28
bash               F        mike     pts/6      0.00 secs Thu Nov 30 13:28

Search Logs for Commands
Using the lastcomm command you will be able to view each use of an individual command.
lastcomm grep
grep                    mike     pts/6      0.00 secs Thu Nov 30 13:28
grep                    mike     pts/6      0.00 secs Thu Nov 30 13:28
grep                    mike     pts/5      0.00 secs Thu Nov 30 12:57
grep                    mike     pts/5      0.00 secs Thu Nov 30 12:57

lastcomm grep
grep                    mike     pts/7      0.00 secs Wed Mar 14 06:07
grep                    mike     pts/7      0.00 secs Wed Mar 14 06:07
grep                    root     pts/7      0.00 secs Wed Mar 14 05:39
grep                    root     pts/7      0.00 secs Wed Mar 14 05:39
grep                    root     pts/2      0.00 secs Wed Mar 14 05:16

Print Summary
The sa command will print a summary of commands that were executed.  It will also condense the information into a summary file called savacct which contains the number of times that the command was executed.  The useracct file keeps a summary of the commands by user.
Output Fields
cpu   -  sum of system and user time in cpu minutes
re    -  actual time in minutes
k     -  cpu-time averaged core usage, in 1k units
k*sec -  cpu storage integral (kilo-core seconds)
u     -  user cpu time in cpu minutes
s     -  system time in cpu minutes
/usr/sbin/sa
Print User Information
Use the -u option to provide information on individual users.
/usr/sbin/sa -u
/usr/sbin/sa -u
root       0.00 cpu      598k mem accton
root       0.00 cpu     1081k mem initlog
root       0.00 cpu      920k mem initlog
root       0.00 cpu     1172k mem touch
root       0.00 cpu     1402k mem psacct
mike       0.01 cpu     7282k mem kdeinit          *
mike       0.00 cpu     6232k mem gnome-panel      *
mike       0.02 cpu     4848k mem gnome-terminal

Display Number of Processes
An increase in these fields indicates a problem.  This prints the number of processes and the number of CPU minutes.  If these numbers continue to increase it is time to look into what is happening.
/usr/sbin/sa -m
195         220.31re           0.09cp     2220k
mike                                  65          198.37re           0.08cp     2135k
root                                  88          21.86re           0.00cp     1084k
postgres                              40          0.09re           0.00cp     4879k
smmsp                                 2           0.00re           0.00cp     1827k

Display All Names
This option will show each of the programs on your server so you may evaluate, real time, memory usage and which programs are running.

sa -a
221      83.36re       0.01cp     1414k
1       0.01re       0.00cp     1471k   rpmq
7       0.33re       0.00cp     2465k   sendmail*
1      40.78re       0.00cp     1844k   sshd
37       0.00re       0.00cp      964k   bash*
32       0.00re       0.00cp      604k   tmpwatch
27       0.00re       0.00cp     4984k   postmaster*
26       0.00re       0.00cp     1116k   df
15       0.00re       0.00cp      959k   id
11       0.00re       0.00cp      709k   egrep
8       0.00re       0.00cp      636k   sa
7       0.00re       0.00cp      817k   grep
6       0.00re       0.00cp      562k   ac
5       0.01re       0.00cp      789k   awk
3       0.41re       0.00cp     1219k   crond*
3       0.40re       0.00cp      674k   run-parts
3       0.00re       0.00cp      774k   dircolors
3       0.00re       0.00cp      673k   consoletype
2      40.98re       0.00cp     1344k   bash
2       0.14re       0.00cp     1628k   sshd*
2       0.00re       0.00cp      914k   logrotate

Sort by Percentage
This will provide the highest percentage users first.

sa -c
232  100.00%      83.36re  100.00%       0.01cp  100.00%     1482k
14    6.03%      41.09re   49.29%       0.01cp   58.82%      957k   ***other*
7    3.02%       0.33re    0.39%       0.00cp   41.18%     2465k   sendmail*
37   15.95%       0.00re    0.00%       0.00cp    0.00%      964k   bash*
32   13.79%       0.00re    0.00%       0.00cp    0.00%     4984k   postmaster*
32   13.79%       0.00re    0.00%       0.00cp    0.00%      604k   tmpwatch
31   13.36%       0.00re    0.00%       0.00cp    0.00%     1122k   df
15    6.47%       0.00re    0.00%       0.00cp    0.00%      959k   id
11    4.74%       0.00re    0.00%       0.00cp    0.00%      709k   egrep

Monday, June 13, 2011

Top ten free Windows tools admins have never heard of


I’ve always been a bit of a tool collector.  And while IT tools are obviously more logical than physical in nature, they accomplish the same goals as a plumber’s wrench.  Without them, doing the job is difficult -- if not impossible.  Yet, too often our employers don’t recognizethe value in IT tools.  Rare is the IT pro with a budget for tools, forcing us to find free alternatives.
These ten IT tools are some of the best I have used.  They’ve saved my career more than once, so by sharing them, hopefully they will help yours. 
With a tip of the hat to our industry’s tool smiths who write and release with no expectation of return, I present the Top Ten Tools You’ve Never Heard Of.

1. Keepass

The average IT pro is responsible for close to a gazillion passwords and keeping them straight is our burden to bear.  That’s why Keepass exists -- an open source password manager that safely locks passwords beneath a master password. It can even copy and paste them into dialog boxes, which is a big help when time is short and passwords are long.

2. Notepad++

Scripting is (or should be) a friend to every IT pro, but scripting editors tend to be on the expensive side.  If an administrator’s scripting needs exceed Notepad’s capabilities check out Notepad++. This free source code editor is preloaded with over 50 popular languages, so you’ll never worry again if that single quote is actually a curly single quote.

3. TCPView

Have you ever used the Windows netstat command and immediately wished for something else?  Look no further than TCPView, an original SysInternals tool that can be downloaded from Microsoft’s website.  TCPView presents a graphical visualization of all the computer’s network connections and is indispensible for troubleshooting the most challenging network issues.

4. WinDirStat

My IT career’s most challenging difficulties have had little to do with technology.  It’s those darn users who are responsible for my biggest scrapes, such as when they store too much data and eat up expensive hard drive space.  You can’t just tell them to delete data -- you need to give them a how-much-you’re-wasting visualization. WinDirStat is that graphical answer.  It highlights which files and file types are the biggest offenders and will quickly cause users to find and delete their excess of storage.

5. OldCmp

Do you hate those aged computer accounts lingering around Active Directory and dream of a report on those not in use?  Back in 2004, the first version of OldCmp delivered on that dream and is still one of the most useful IT tools.  This JoeWare tool can also remove old accounts and help kick out AD computer accounts after someone kicks out a computer.

6. WSName

Changing a computer's name is easy:  Move it to a workgroup, rename it and add it back into the domain.  What’s hard is the time wasted watching that machine reboot multiple times and repeating those steps for dozens or even hundreds of desktops.  So, why not automate computer renames with WSName?  With a little extra scripting admins can even rename remotely.

7. MSRemoteNG

Being an IT pro means connecting to lots of computers on any particular day.  But different computers require different connection steps.  With MRemoteNG, admins can collect every computer under a single interface with little effort.  This open source, tabbed, multi-protocol, remote connections manager also supports all sorts of protocol acronyms, including RDP, VNC, ICA, SSH, TELNET, HTTP, rlogin and raw sockets connections.

8. Memtest86

In Windows 7 or Windows Server 2008, click Start | Administrative Tools | Windows Memory Diagnostic and the system will run a hardware-level test on its RAM at the next boot.  That test determines if bad RAM is a source of bad behavior.  But what if you’re not using these operating systems?  Once burned to a bootable CD, Memtest86 will boot run an extensive series of tests against physical RAM and report whether new hardware is needed.

9. Specops Gpupdate

Specops Gpupdate is the only tool on this list that requires registration, but it’s worth the information disclosure.  This tool remotely executes a restart or shutdown, wake on LAN or Group Policy update from directly within the Active Directory Users and Computers (ADUC) console.  It can also perform an automatic WSUS client update, speeding up patch management and monthly update duties.

10. Safepasswd.com

This last tool is as much fun as it is useful.  Safepasswd.com is a simple website that generates random passwords with options for password length and type, such as Easy to Remember versus all manner of not easy. The website is also useful for user and service passwords, as well as users who “can’t think of a new password”.  After seeing what Safepasswd.com considers easy, they’ll think twice about whining when their account needs to be renewed.

Sunday, June 12, 2011

How To Set Up Software RAID1 On A Running System (Incl. GRUB2 Configuration) (Debian Squeeze)


This guide explains how to set up software RAID1 on an already running Debian Squeeze system. The GRUB2 bootloader will be configured in such a way that the system will still be able to boot if one of the hard drives fails (no matter which one).
I do not issue any guarantee that this will work for you!

1 Preliminary Note

In this tutorial I'm using a Debian Squeeze system with two hard drives, /dev/sda and /dev/sdb which are identical in size. /dev/sdb is currently unused, and /dev/sda has the following partitions:
  • /dev/sda1: /boot partition, ext4;
  • /dev/sda2: swap;
  • /dev/sda3: / partition, ext4
In the end I want to have the following situation:
  • /dev/md0 (made up of /dev/sda1 and /dev/sdb1): /boot partition, ext4;
  • /dev/md1 (made up of /dev/sda2 and /dev/sdb2): swap;
  • /dev/md2 (made up of /dev/sda3 and /dev/sdb3): / partition, ext4
This is the current situation:
df -h
root@server1:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             4.0G  712M  3.1G  19% /
tmpfs                 249M     0  249M   0% /lib/init/rw
udev                  244M  100K  244M   1% /dev
tmpfs                 249M     0  249M   0% /dev/shm
/dev/sda1             472M   25M  423M   6% /boot
root@server1:~#
fdisk -l
root@server1:~# fdisk -l

Disk /dev/sda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b0ecb

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          63      498688   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              63         125      499712   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             125         653     4242432   83  Linux
Partition 3 does not end on cylinder boundary.

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table
root@server1:~#

2 Installing mdadm

The most important tool for setting up RAID is mdadm. Let's install it like this:
apt-get install initramfs-tools mdadm
MD arrays needed for the root file system: <-- all
Afterwards, we load a few kernel modules (to avoid a reboot):
modprobe linear
modprobe multipath
modprobe raid0
modprobe raid1
modprobe raid5
modprobe raid6
modprobe raid10
Now run
cat /proc/mdstat
The output should look as follows:
root@server1:~# cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
unused devices:
root@server1:~#

3 Preparing /dev/sdb

To create a RAID1 array on our already running system, we must prepare the /dev/sdb hard drive for RAID1, then copy the contents of our /dev/sda hard drive to it, and finally add /dev/sda to the RAID1 array.
First, we copy the partition table from /dev/sda to /dev/sdb so that both disks have exactly the same layout:
sfdisk -d /dev/sda | sfdisk --force /dev/sdb
The output should be as follows:
root@server1:~# sfdisk -d /dev/sda | sfdisk --force /dev/sdb
Checking that no-one is using this disk right now ...
OK

Disk /dev/sdb: 652 cylinders, 255 heads, 63 sectors/track

sfdisk: ERROR: sector 0 does not have an msdos signature
 /dev/sdb: unrecognized partition table type
Old situation:
No partitions found
New situation:
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sdb1   *      2048    999423     997376  83  Linux
/dev/sdb2        999424   1998847     999424  82  Linux swap / Solaris
/dev/sdb3       1998848  10483711    8484864  83  Linux
/dev/sdb4             0         -          0   0  Empty
Warning: partition 1 does not end at a cylinder boundary
Successfully wrote the new partition table

Re-reading the partition table ...

If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)
root@server1:~#
The command
fdisk -l
should now show that both HDDs have the same layout:
root@server1:~# fdisk -l

Disk /dev/sda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b0ecb

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          63      498688   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              63         125      499712   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             125         653     4242432   83  Linux
Partition 3 does not end on cylinder boundary.

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1          63      498688   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sdb2              63         125      499712   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sdb3             125         653     4242432   83  Linux
Partition 3 does not end on cylinder boundary.
root@server1:~#
Next we must change the partition type of our three partitions on /dev/sdb to Linux raid autodetect:
fdisk /dev/sdb
root@server1:~# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help):
 <-- m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help):
 <-- t
Partition number (1-4): <-- 1
Hex code (type L to list codes): <-- L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1
Hex code (type L to list codes):
 <-- fd
Changed system type of partition 1 to fd (Linux raid autodetect)

Command (m for help):
 <-- t
Partition number (1-4): <-- 2
Hex code (type L to list codes): <-- fd
Changed system type of partition 2 to fd (Linux raid autodetect)

Command (m for help):
 <-- t
Partition number (1-4): <-- 3
Hex code (type L to list codes): <-- fd
Changed system type of partition 3 to fd (Linux raid autodetect)

Command (m for help):
 <-- w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
root@server1:~#

To make sure that there are no remains from previous RAID installations on /dev/sdb, we run the following commands:
mdadm --zero-superblock /dev/sdb1
mdadm --zero-superblock /dev/sdb2
mdadm --zero-superblock /dev/sdb3
If there are no remains from previous RAID installations, each of the above commands will throw an error like this one (which is nothing to worry about):
root@server1:~# mdadm --zero-superblock /dev/sdb1
mdadm: Unrecognised md component device - /dev/sdb1
root@server1:~#
Otherwise the commands will not display anything at all.


4 Creating Our RAID Arrays

 
Now let's create our RAID arrays /dev/md0, /dev/md1, and /dev/md2. /dev/sdb1 will be added to /dev/md0, /dev/sdb2 to /dev/md1, and /dev/sdb3 to /dev/md2. /dev/sda1, /dev/sda2, and /dev/sda3 can't be added right now (because the system is currently running on them), therefore we use the placeholder missing in the following three commands:
mdadm --create /dev/md0 --level=1 --raid-disks=2 missing /dev/sdb1
mdadm --create /dev/md1 --level=1 --raid-disks=2 missing /dev/sdb2
mdadm --create /dev/md2 --level=1 --raid-disks=2 missing /dev/sdb3
You might see the following message for each command - just press y to continue:
root@server1:~# mdadm --create /dev/md2 --level=1 --raid-disks=2 missing /dev/sdb3
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
Continue creating array?
 <-- y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md2 started.
root@server1:~#

The command
cat /proc/mdstat
should now show that you have three degraded RAID arrays ([_U] or [U_] means that an array is degraded while [UU] means that the array is ok):
root@server1:~# cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
md2 : active raid1 sdb3[1]
      4241396 blocks super 1.2 [2/1] [_U]

md1 : active raid1 sdb2[1]
      499700 blocks super 1.2 [2/1] [_U]

md0 : active raid1 sdb1[1]
      498676 blocks super 1.2 [2/1] [_U]

unused devices: 
root@server1:~#
Next we create filesystems on our RAID arrays (ext4 on /dev/md0 and /dev/md2 and swap on /dev/md1):
mkfs.ext4 /dev/md0
mkswap /dev/md1
mkfs.ext4 /dev/md2
Next we must adjust /etc/mdadm/mdadm.conf (which doesn't contain any information about our new RAID arrays yet) to the new situation:
cp /etc/mdadm/mdadm.conf /etc/mdadm/mdadm.conf_orig
mdadm --examine --scan >> /etc/mdadm/mdadm.conf
Display the contents of the file:
cat /etc/mdadm/mdadm.conf
At the bottom of the file you should now see details about our three (degraded) RAID arrays:
# mdadm.conf
#
# Please refer to mdadm.conf(5) for information about this file.
#

# by default, scan all partitions (/proc/partitions) for MD superblocks.
# alternatively, specify devices to scan, using wildcards if desired.
DEVICE partitions

# 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 

# instruct the monitoring daemon where to send mail alerts
MAILADDR root

# definitions of existing MD arrays

# This file was auto-generated on Tue, 24 May 2011 14:09:09 +0200
# by mkconf 3.1.4-1+8efb9d1
ARRAY /dev/md/0 metadata=1.2 UUID=b40c3165:17089af7:5d5ee79b:8783491b name=server1.example.com:0
ARRAY /dev/md/1 metadata=1.2 UUID=62e4a606:878092a0:212209c5:c91b8fef name=server1.example.com:1
ARRAY /dev/md/2 metadata=1.2 UUID=94e51099:d8475c57:4ff1c60f:9488a09a name=server1.example.com:2

5 Adjusting The System To RAID1

Now let's mount /dev/md0 and /dev/md2 (we don't need to mount the swap array /dev/md1):
mkdir /mnt/md0
mkdir /mnt/md2
mount /dev/md0 /mnt/md0
mount /dev/md2 /mnt/md2
You should now find both arrays in the output of
mount
root@server1:~# mount
/dev/sda3 on / type ext4 (rw,errors=remount-ro)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
/dev/sda1 on /boot type ext4 (rw)
/dev/md0 on /mnt/md0 type ext4 (rw)
/dev/md2 on /mnt/md2 type ext4 (rw)
root@server1:~#
Next we modify /etc/fstab. Comment out the current /, /boot, and swap partitions and add new lines for them where you replace the UUIDs with /dev/md0 (for the /boot partition), /dev/md1 (for the swap partition) and /dev/md2 (for the / partition) so that the file looks as follows:
vi /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#                
proc            /proc           proc    defaults        0       0
# / was on /dev/sda3 during installation
#UUID=e4e38871-0115-477d-94f9-34b079d26248 /               ext4    errors=remount-ro 0       1
/dev/md2 /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
#UUID=7e2fb013-073e-4312-a669-f34b35069bfb /boot           ext4    defaults        0       2
/dev/md0 /boot           ext4    defaults        0       2
# swap was on /dev/sda2 during installation
#UUID=1a5951f8-d0ab-4e0e-b42a-871f81b6fd82 none            swap    sw              0       0
/dev/md1 none            swap    sw              0       0
/dev/scd0       /media/cdrom0   udf,iso9660 user,noauto     0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto  0       0
Next replace /dev/sda1 with /dev/md0 and /dev/sda3 with /dev/md2 in /etc/mtab:
vi /etc/mtab
/dev/md2 / ext4 rw,errors=remount-ro 0 0
tmpfs /lib/init/rw tmpfs rw,nosuid,mode=0755 0 0
proc /proc proc rw,noexec,nosuid,nodev 0 0
sysfs /sys sysfs rw,noexec,nosuid,nodev 0 0
udev /dev tmpfs rw,mode=0755 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
devpts /dev/pts devpts rw,noexec,nosuid,gid=5,mode=620 0 0
/dev/md0 /boot ext4 rw 0 0
/dev/md0 /mnt/md0 ext4 rw 0 0
/dev/md2 /mnt/md2 ext4 rw 0 0
Now up to the GRUB2 boot loader. Create the file /etc/grub.d/09_swraid1_setup as follows:
cp /etc/grub.d/40_custom /etc/grub.d/09_swraid1_setup
vi /etc/grub.d/09_swraid1_setup
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-amd64' --class debian --class gnu-linux --class gnu --class os {
        insmod raid
        insmod mdraid
        insmod part_msdos
        insmod ext2
        set root='(md/0)'
        echo    'Loading Linux 2.6.32-5-amd64 ...'
        linux   /vmlinuz-2.6.32-5-amd64 root=/dev/md2 ro  quiet
        echo    'Loading initial ramdisk ...'
        initrd  /initrd.img-2.6.32-5-amd64
}
Make sure you use the correct kernel version in the menuentry stanza (in the linux and initrd lines). You can find it out by running
uname -r
or by taking a look at the current menuentry stanzas in the ### BEGIN /etc/grub.d/10_linux ### section in /boot/grub/grub.cfg. Also make sure that you use root=/dev/md2 in the linux line.
The important part in our new menuentry stanza is the line set root='(md/0)' - it makes sure that we boot from our RAID1 array /dev/md0 (which will hold the /boot partition) instead of /dev/sda or /dev/sdb which is important if one of our hard drives fails - the system will still be able to boot.
Because we don't use UUIDs anymore for our block devices, open /etc/default/grub...
vi /etc/default/grub
... and uncomment the line GRUB_DISABLE_LINUX_UUID=true:
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.

GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet"
GRUB_CMDLINE_LINUX=""

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_LINUX_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"
Run
update-grub
to write our new kernel stanza from /etc/grub.d/09_swraid1_setup to /boot/grub/grub.cfg.
Next we adjust our ramdisk to the new situation:
update-initramfs -u
Now we copy the contents of /dev/sda1 and /dev/sda3 to /dev/md0 and /dev/md2 (which are mounted on /mnt/md0 and /mnt/md2):
cp -dpRx / /mnt/md2
cd /boot
cp -dpRx . /mnt/md0

6 Preparing GRUB2 (Part 1)

Afterwards we must make sure that the GRUB2 bootloader is installed on both hard drives, /dev/sda and /dev/sdb:
grub-install /dev/sda
grub-install /dev/sdb
Now we reboot the system and hope that it boots ok from our RAID arrays:
reboot

7 Preparing /dev/sda

 
If all goes well, you should now find /dev/md0 and /dev/md2 in the output of
df -h
root@server1:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/md2              4.0G  714M  3.1G  19% /
tmpfs                 249M     0  249M   0% /lib/init/rw
udev                  244M  132K  244M   1% /dev
tmpfs                 249M     0  249M   0% /dev/shm
/dev/md0              472M   25M  423M   6% /boot
root@server1:~#
The output of
cat /proc/mdstat
should be as follows:
root@server1:~# cat /proc/mdstat
Personalities : [raid1]
md2 : active raid1 sdb3[1]
      4241396 blocks super 1.2 [2/1] [_U]

md1 : active (auto-read-only) raid1 sdb2[1]
      499700 blocks super 1.2 [2/1] [_U]

md0 : active raid1 sdb1[1]
      498676 blocks super 1.2 [2/1] [_U]

unused devices: 
root@server1:~#
Now we must change the partition types of our three partitions on /dev/sda to Linux raid autodetect as well:
fdisk /dev/sda
root@server1:~# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help):
 <-- t
Partition number (1-4): <-- 1
Hex code (type L to list codes): <-- fd
Changed system type of partition 1 to fd (Linux raid autodetect)

Command (m for help):
 <-- t
Partition number (1-4): <-- 2
Hex code (type L to list codes): <-- fd
Changed system type of partition 2 to fd (Linux raid autodetect)

Command (m for help):
 <-- t
Partition number (1-4): <-- 3
Hex code (type L to list codes): <-- fd
Changed system type of partition 3 to fd (Linux raid autodetect)

Command (m for help):
 <-- w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
root@server1:~#

Now we can add /dev/sda1, /dev/sda2, and /dev/sda3 to the respective RAID arrays:
mdadm --add /dev/md0 /dev/sda1
mdadm --add /dev/md1 /dev/sda2
mdadm --add /dev/md2 /dev/sda3
Now take a look at
cat /proc/mdstat
... and you should see that the RAID arrays are being synchronized:
root@server1:~# cat /proc/mdstat
Personalities : [raid1]
md2 : active raid1 sda3[2] sdb3[1]
      4241396 blocks super 1.2 [2/1] [_U]
      [==========>..........]  recovery = 54.6% (2319808/4241396) finish=0.7min speed=45058K/sec

md1 : active raid1 sda2[2] sdb2[1]
      499700 blocks super 1.2 [2/2] [UU]

md0 : active raid1 sda1[2] sdb1[1]
      498676 blocks super 1.2 [2/2] [UU]

unused devices: 
root@server1:~#
(You can run
watch cat /proc/mdstat
to get an ongoing output of the process. To leave watch, press CTRL+C.)
Wait until the synchronization has finished (the output should then look like this:
root@server1:~# cat /proc/mdstat
Personalities : [raid1]
md2 : active raid1 sda3[2] sdb3[1]
      4241396 blocks super 1.2 [2/2] [UU]

md1 : active raid1 sda2[2] sdb2[1]
      499700 blocks super 1.2 [2/2] [UU]

md0 : active raid1 sda1[2] sdb1[1]
      498676 blocks super 1.2 [2/2] [UU]

unused devices: 
root@server1:~#
).
Then adjust /etc/mdadm/mdadm.conf to the new situation:
cp /etc/mdadm/mdadm.conf_orig /etc/mdadm/mdadm.conf
mdadm --examine --scan >> /etc/mdadm/mdadm.conf
/etc/mdadm/mdadm.conf should now look something like this:
cat /etc/mdadm/mdadm.conf
# mdadm.conf
#
# Please refer to mdadm.conf(5) for information about this file.
#

# by default, scan all partitions (/proc/partitions) for MD superblocks.
# alternatively, specify devices to scan, using wildcards if desired.
DEVICE partitions

# 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 

# instruct the monitoring daemon where to send mail alerts
MAILADDR root

# definitions of existing MD arrays

# This file was auto-generated on Tue, 24 May 2011 14:09:09 +0200
# by mkconf 3.1.4-1+8efb9d1
ARRAY /dev/md/0 metadata=1.2 UUID=b40c3165:17089af7:5d5ee79b:8783491b name=server1.example.com:0
ARRAY /dev/md/1 metadata=1.2 UUID=62e4a606:878092a0:212209c5:c91b8fef name=server1.example.com:1
ARRAY /dev/md/2 metadata=1.2 UUID=94e51099:d8475c57:4ff1c60f:9488a09a name=server1.example.com:2

8 Preparing GRUB2 (Part 2)

Now we delete /etc/grub.d/09_swraid1_setup...
rm -f /etc/grub.d/09_swraid1_setup
... and update our GRUB2 bootloader configuration:
update-grub
update-initramfs -u
Now if you take a look at /boot/grub/grub.cfg, you should find that the menuentry stanzas in the ### BEGIN /etc/grub.d/10_linux ### section look pretty much the same as what we had in /etc/grub.d/09_swraid1_setup (they should now also be set to boot from /dev/md0 instead of (hd0) or (hd1)), that's why we don't need /etc/grub.d/09_swraid1_setup anymore.
Afterwards we must make sure that the GRUB2 bootloader is installed on both hard drives, /dev/sda and /dev/sdb:
grub-install /dev/sda
grub-install /dev/sdb
Reboot the system:
reboot
It should boot without problems.
That's it - you've successfully set up software RAID1 on your running Debian Squeeze system!


9 Testing

 
Now let's simulate a hard drive failure. It doesn't matter if you select /dev/sda or /dev/sdb here. In this example I assume that /dev/sdb has failed.
To simulate the hard drive failure, you can either shut down the system and remove /dev/sdb from the system, or you (soft-)remove it like this:
mdadm --manage /dev/md0 --fail /dev/sdb1
mdadm --manage /dev/md1 --fail /dev/sdb2
mdadm --manage /dev/md2 --fail /dev/sdb3
mdadm --manage /dev/md0 --remove /dev/sdb1
mdadm --manage /dev/md1 --remove /dev/sdb2
mdadm --manage /dev/md2 --remove /dev/sdb3
Shut down the system:
shutdown -h now
Then put in a new /dev/sdb drive (if you simulate a failure of /dev/sda, you should now put /dev/sdb in /dev/sda's place and connect the new HDD as /dev/sdb!) and boot the system. It should still start without problems.
Now run
cat /proc/mdstat
and you should see that we have a degraded array:
root@server1:~# cat /proc/mdstat
Personalities : [raid1]
md2 : active raid1 sda3[2]
      4241396 blocks super 1.2 [2/1] [U_]

md1 : active (auto-read-only) raid1 sda2[2]
      499700 blocks super 1.2 [2/1] [U_]

md0 : active raid1 sda1[2]
      498676 blocks super 1.2 [2/1] [U_]

unused devices: 
root@server1:~#
The output of
fdisk -l
should look as follows:
root@server1:~# fdisk -l

Disk /dev/sda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e0f78

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          63      498688   fd  Linux raid autodetect
Partition 1 does not end on cylinder boundary.
/dev/sda2              63         125      499712   fd  Linux raid autodetect
Partition 2 does not end on cylinder boundary.
/dev/sda3             125         653     4242432   fd  Linux raid autodetect
Partition 3 does not end on cylinder boundary.

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/md0: 510 MB, 510644224 bytes
2 heads, 4 sectors/track, 124669 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/md0 doesn't contain a valid partition table

Disk /dev/md1: 511 MB, 511692800 bytes
2 heads, 4 sectors/track, 124925 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/md1 doesn't contain a valid partition table

Disk /dev/md2: 4343 MB, 4343189504 bytes
2 heads, 4 sectors/track, 1060349 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/md2 doesn't contain a valid partition table
root@server1:~#
Now we copy the partition table of /dev/sda to /dev/sdb:
sfdisk -d /dev/sda | sfdisk --force /dev/sdb
root@server1:~# sfdisk -d /dev/sda | sfdisk --force /dev/sdb
Checking that no-one is using this disk right now ...
OK

Disk /dev/sdb: 652 cylinders, 255 heads, 63 sectors/track

sfdisk: ERROR: sector 0 does not have an msdos signature
 /dev/sdb: unrecognized partition table type
Old situation:
No partitions found
New situation:
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sdb1   *      2048    999423     997376  fd  Linux raid autodetect
/dev/sdb2        999424   1998847     999424  fd  Linux raid autodetect
/dev/sdb3       1998848  10483711    8484864  fd  Linux raid autodetect
/dev/sdb4             0         -          0   0  Empty
Warning: partition 1 does not end at a cylinder boundary
Successfully wrote the new partition table

Re-reading the partition table ...

If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)
root@server1:~#
Afterwards we remove any remains of a previous RAID array from /dev/sdb...
mdadm --zero-superblock /dev/sdb1
mdadm --zero-superblock /dev/sdb2
mdadm --zero-superblock /dev/sdb3
... and add /dev/sdb to the RAID array:
mdadm -a /dev/md0 /dev/sdb1
mdadm -a /dev/md1 /dev/sdb2
mdadm -a /dev/md2 /dev/sdb3
Now take a look at
cat /proc/mdstat
root@server1:~# cat /proc/mdstat
Personalities : [raid1]
md2 : active raid1 sdb3[3] sda3[2]
      4241396 blocks super 1.2 [2/1] [U_]
      [======>..............]  recovery = 32.2% (1367168/4241396) finish=1.0min speed=44102K/sec

md1 : active raid1 sdb2[3] sda2[2]
      499700 blocks super 1.2 [2/2] [UU]

md0 : active raid1 sdb1[3] sda1[2]
      498676 blocks super 1.2 [2/2] [UU]

unused devices: 
root@server1:~#
Wait until the synchronization has finished:
root@server1:~# cat /proc/mdstat
Personalities : [raid1]
md2 : active raid1 sdb3[3] sda3[2]
      4241396 blocks super 1.2 [2/2] [UU]

md1 : active raid1 sdb2[3] sda2[2]
      499700 blocks super 1.2 [2/2] [UU]

md0 : active raid1 sdb1[3] sda1[2]
      498676 blocks super 1.2 [2/2] [UU]

unused devices: 
root@server1:~#
Then install the bootloader on both HDDs:
grub-install /dev/sda
grub-install /dev/sdb
That's it. You've just replaced a failed hard drive in your RAID1 array.

10 Links