Sunday, July 3, 2011

How to install Linux (Ubuntu, Debian, etc.) on two drive RAID mirror

Drives are cheap nowadays, and your data is valuable. Next time you are installing Ubuntu, consider buying a second disk, and making your fresh installation on a mirrored drive setup using Linux software RAID (MD).

This is a guide for how to install Linux/GNU on a RAID1 or RAID10 mirrored set of drives. We use a GPT disk label so it works for any size drives, even ones larger than 2TB. For this guide, I used Ubuntu LTS, but Debian, and any newer Ubuntu should work with little modification. Any other distribution, like Fedora that has a LiveCD which one can install from can be installed with little modification.

If you are looking to convert a running system which *doesn't* use the GPT disk label, consider following my guide for converting a Linux/GNU system to mirrored drive setup.


1) Download, burn, and boot into an Ubuntu LiveCD.

2) After it starts, start a terminal, and run:

sudo apt-get update
sudo apt-get install mdadm

When prompted about the Mail Transport Agent (postfix in LTS) configuration, select "No Configuration".

3) Turn off swap in case the LiveCD discovered and started using a swap partition on some drive in your system.

sudo swapoff -a

4) Delete all partitions on the target drives. Assuming they are /dev/sda and /dev/sdb:

sudo parted /dev/sda
mklabel msdos y
q

And similarly for /dev/sdb.

4) Create a RAID10 or RAID1 with one drive and second drive missing (we will add it later).
a) For RAID10:

sudo mdadm --create /dev/md0 -l10 -pn2 -c512 -n2 /dev/sda missing

b) For RAID1:

sudo mdadm --create /dev/md0 -l1 -n2 /dev/sda missing

5) Check that md0 exists and is operational:

cat /proc/mdstat

5) Create partition table and partitions on the RAID, but DO NOT MAKE A SWAP partition. This is important because the Ubuntu LiveCD will try to activate swap on the raw device (/dev/sda) even though the device is part of a RAID and will fail because the device is busy:

sudo parted /dev/md0
mklabel gpt y
mkpart "grub-bios" ext4 0 1
set 1 bios_grub on
mkpart "root" ext4 1 40000
q


I found it necessary to stop and start the array to get the partitions to show up.

mdadm --stop /dev/md0
rm -rf /dev/md0*
mdadm --assemble --scan


The 40000MB size above is just an example. Modify to suit your needs. Also, feel free to create separate /boot, /var, /usr, /tmp, etc. as you see fit.

5) Double-click the "Install Ubuntu" icon on the desktop, and go through the process until you reach the point of partitioning.

6) Select "Specify partitions manually", and click "Forward".

7) On the next screen, double-click the /dev/md0p2 partition.
a) In the "Use as" field, for a production server, select ext3. For a personal computer, you can select ext4.
b) Put a check-mark on "Format the partition".
c) Enter "/" in the "Mount point" field (just forward slash, without the quotes).
d) Click "OK", and then "Forward". You will likely get a warning that no swap has been created, but simply click "Continue".

8) On the next screen, enter your login information, and click "Forward", and on the screen after that, select "Advanced", and under "Device for boot loader installation", select /dev/md0. Installation of the boot-loader will fail, and we'll have to do it manually, but it's better than the installer trying to install it on some raw device.

9) As mentioned above, towards the end of the installation, the boot-loader will fail to install. Select the "Continue without a boot-loader" option, and finish the installation. Click "Continue testing" to stay in the LiveCD session.

10) Edit your fstab, and change ALL the entries that read UUID=XXXXXXXXX to /dev/md0pNN, where NN is a partition number. In other words, you need to make sure your system will not decide to use the raw devices which comprise /dev/md0, but the mirror itself. If your device is indicated by a UUID, then hopefully there is some indication what the device name used to be at installation time in a comment above the entry. So, for example, if the comment says the device used to be /dev/md0p2, delete ONLY the UUID=XXXXXXXXX part (make sure you leave the mount-point intact), and put /dev/md0p2 in place of the UUID=XXXXXXXXX.

11) Save your array into /etc/mdadm/mdadm.conf, and rebuild your initrd in order to make sure the array gets assembled at boot time, and under the correct name (e.g. /dev/md0 instead of something like /dev/md_d0).

sudo chroot /mnt

mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev

apt-get update
apt-get install mdadm
mkdir -p /etc/mdadm/
mdadm --examine --scan > /etc/mdadm/mdadm.conf
update-initramfs -c -k all

If you get complaints that initrd is custom, you can force update-initramfs totake over the custom initrd with these options instead:

update-initramfs -ut -k all

DO NOT EXIT CHROOT YET!!

12) Configure and install grub v2 into the MBR of the mirror. For Ubuntu 10.04, it was trivially easy to do. First we make sure that Grub v2 is installed.

sudo apt-get install grub2

Next, edit /etc/default/grub, and *uncomment* the line that reads "GRUB_DISABLE_LINUX_UUID=true".

grub-mkconfig > /boot/grub/grub.cfg
grub-install --recheck --modules="raid" /dev/sda


13) Reboot into your disk installation, but make sure you are booting off /dev/sda, on which the mirror lives now. To do so, you can usually call up a one-time Boot menu with one of the F-keys (like F8) on many systems. Otherwise, modify the necessary BIOS settings to do so.

Once you're in, you can add /dev/sdb to the array that was created in the steps above.

sudo mdadm --add /dev/md0 /dev/sdb

14) You can watch the process of resyncing the drives with:

watch cat /proc/mdstat

15) If you need swap, create a swap partition and add it to /etc/fstab. If you find that you would like to have this step described here (or linked from here), please post a comment requesting it.

Thursday, June 30, 2011

How to get a free Sipgate account with a US number (DID), and receive calls for free on it through SIP (e.g. on Android)

This guide will help you get a free US number (DID), and receive incoming calls on your Android device *for free* without using minutes on your voice plan, purely over Wifi (or data if you have a good data plan). This guide is part of my larger guide on how to make free calls to USA and Canada using Google Voice.

1) Register for a free Sipgate One account

2) Once you are signed up and your account is active, make note of your Sipgate SIP ID and SIP password.

3) Configure either your built-in SIP client (Android 2.3 and up), or download one of CSipSimple, Sipdroid, or Linphone from the market, and install it on your Android phone. General instructions on setting up Sipgate with a SIP client can be found here.

a) For Android 2.3 and up, you may choose to use the built-in SIP functionality of your device. Go to Settings->Call Settings->Accounts. Select "Receive incoming calls" (this is essential for callback to work), and then hit "Add account". Enter your Sipgate ID and password in the appropriate fields, and sipgate.com under "Server".

b) Once you install and start Sipdroid, hit Menu->Settings->SIP Account (Line 1), and enter your Sipgate ID in the "Authorization Username" field, and password in the "Password" field, and sipgate.com under "Server or Proxy". If you so choose, you can check "Use 3G" (see NOTE below about data usage). If you experience weird audio issues, you can also select Settings->Advanced Options->Use STUN Server, and enter stun.sipgate.net in "STUN Server name", or use the pre-supplied STUN server.

c) Recent versions of CSipSimple have a Sipgate wizard which can be followed to set up your Sipgate account easily. When when you first start it up:
- Select whether you want to be "Always available" or "Available on WiFi", depending on whether you have a generous mobile data plan or not. Also, if using your data plan, check "I'm allowed to use mobile" below.
- On the next screen, click "Add account", then choose the Sipgate wizard, and enter your Sipgate ID and password in the appropriate fields, and sipgate.com under "Server".
- If you experience weird networking issues, one way audio or other issues, you can try enabling STUN, Menu->Settings->Network->Enable STUN. You can elect to use callwithus STUN server (stun.sipgate.net) or the CSipSimple provided server (Thanks for your comment, Ross).
- Finally, you if you want to use your mobile data plan (see NOTE below about data usage), check "Use 3G" in the settings.

d) After installing Linphone, when it is first run, it will do an echo cancellation calibration. After that, you will be in the SIP Account screen. Enter your Sipgate ID and password in the appropriate fields, and sipgate.com under "Domain". You can check "Start at boot time" below, and enter Sipgate's STUN server (stun.sipgate.net) under "Stun server" if you have strange audio problems. Linphone will make use of your mobile data plan by default, so either make sure you have a generous enough mobile data plan (see NOTE below about data usage), or make sure you don't make calls when you are outside WIFI coverage.
(2 - 3 minutes)

NOTE: If you have a mobile data plan, you can tell Sipdroid, CSipSimple, and Linphone to connect even over your wireless provider's data plan (3G), but *beware of potentially very high data charges*, since voip can be very data intensive. In my personal tests, a high quality codec consumes about 1MB per minute.

4) You can either use this number as your real phone number, add it to Google Voice, and make GV forward to it, even use it to activate your GV account. Finally, you can use this number to MAKE FREE CALLS TO USA AND CANADA using my other guide.

How to get a free IPKall US number (DID), and receive calls for free on it through SIP (e.g. on Android)

This guide will help you get a free US number (DID), and receive incoming calls on your Android device *for free* without using minutes on your voice plan, purely over Wifi (or data if you have a good data plan). This guide is part of my larger guide on how to make free calls to USA and Canada using Google Voice.

1) Register for a free SIP service with www.callwithus.com or with www.callcentric.com. For the purpose of this guide we shall work with callwithus as the example.

2) For callwithus, you can give them your Google Voice number as your contact number if you have one, but don't have a "real number". The number is not needed initially, it is for "confirmation of details" later on according to callwithus. I guess you can edit this number later as well.

3) Once you have the confirmation email from callwithus, note your username and password from it (they will look something like this: 234567890 and 123456 respectively).

4) Configure either your built-in SIP client (Android 2.3 and up), or download one of CSipSimple, Sipdroid, or Linphone from the market, and install it on your Android phone.

a) For Android 2.3 and up, you may choose to use the built-in SIP functionality of your device. Go to Settings->Call Settings->Accounts. Select "Receive incoming calls" (this is essential for callback to work), and then hit "Add account". Enter your callwithus username and password in the appropriate fields, and sip.callwithus.com under "Server".

b) Once you install and start Sipdroid, hit Menu->Settings->SIP Account (Line 1), and enter your callwithus username in the "Authorization Username" field, and password in the "Password" field, and sip.callwithus.com under "Server or Proxy". If you so choose, you can check "Use 3G" (see NOTE below about data usage). If you experience weird audio issues, you can also select Settings->Advanced Options->Use STUN Server, and enter stun.callwithus.com in "STUN Server name", or use the pre-supplied STUN server.

c) Recent versions of CSipSimple have "Wizaards" which can be followed to set up your SIP account easily. When when you first start it up:
- Select whether you want to be "Always available" or "Available on WiFi", depending on whether you have a generous mobile data plan or not. Also, if using your data plan, check "I'm allowed to use mobile" below.
- On the next screen, click "Add account", then choose the Generic wizard for callwithus or the Callcentric wizard for callcentric, and enter your callwithus username and password in the appropriate fields, and sip.callwithus.com under "Server".
- If you experience weird networking issues, one way audio or other issues, you can try enabling STUN, Menu->Settings->Network->Enable STUN. You can elect to use callwithus STUN server (stun.callwithus.com) or the CSipSimple provided server (Thanks for your comment, Ross).
- Finally, you if you want to use your mobile data plan (see NOTE below about data usage), check "Use 3G" in the settings.

d) After installing Linphone, when it is first run, it will do an echo cancellation calibration. After that, you will be in the SIP Account screen. Enter your callwithus username and password in the appropriate fields, and sip.callwithus.com under "Domain". You can check "Start at boot time" below, and enter callwithus's STUN server (stun.callwithus.com) under "Stun server" if you have strange audio problems. Linphone will make use of your mobile data plan by default, so either make sure you have a generous enough mobile data plan (see NOTE below about data usage), or make sure you don't make calls when you are outside WIFI coverage.
(2 - 3 minutes)

NOTE: If you have a mobile data plan, you can tell Sipdroid, CSipSimple, and Linphone to connect even over your wireless provider's data plan (3G), but *beware of potentially very high data charges*, since voip can be very data intensive. In my personal tests, a high quality codec consumes about 1MB per minute.

Once your SIP client is successfully registered, we can create an IPKall account. If you haven't already done so, make sure that you go through the settings of your SIP client, and ensure that it is set to be AVAILABLE FOR INCOMING CALLS. Every client has this option in a different place, and worded slightly differently, but you must find it.

5) Browse to www.ipkall.com and click the "*Sign up*" link.

6) Fill out the sign up form, making sure that:
- You pick an an area code you're happy with.
- "SIP username is your callwithus username (e.g. 234567890).
- "Hostname or IP address" is sip.callwithus.com
- "Email Address" is valid.
- "Password" is the password to your ipkall account, not your callwithus
account.
- You fill out the two word confirmation correctly.

7) Click "Submit", and wait for a confirmation email at your email address.

8) In the confirmation email, there will be a phone number with the area code you picked. Dialing this number will ring your SIP client provided you have enabled your SIP client for incoming calls. Ensure this works, otherwise, retrace your steps and make sure everything above was done correctly.

9) If you will now proceed to use the ipkall number in Google Voice, keep in mind that these numbers are often recycled from accounts that were deleted, and they may still be present in another person's Google Voice account. If they were used to activate the other person's GV account, you will NOT be able to use this number to activate OR forward to in your own GV account. You won't even be able to "reclaim" it for your purposes to your own GV account. If this turns out to be the case, login to ipkall here:

http://phone.ipkall.com/login.asp

with your IPKall phone number and password (from the confirmation email), hit "Cancel account", and redo steps 1 - 9. I had to do this 3 times before I got a number I could "reclaim".

10) You can either use this number as your real phone number, add it to Google Voice, and make GV forward to it, even use it to activate your GV account. Finally, you can use this number to MAKE FREE CALLS TO USA AND CANADA using my other guide.

Friday, April 22, 2011

How to convert a Linux (Ubuntu, Debian, etc.) system to a two drive RAID mirror (RAID1 or RAID10) using Linux MD

Drives are cheap nowadays, and your data is valuable. Buy a second disk, and convert your system to a RAID1 or RAID10 mirrored drive setup using Linux software RAID (MD). I've done this procedure on Debian Squeeze 6.0 and on Ubuntu Lucid Lynx 10.04, but it should work with slight modifications on any modern Linux distribution like Fedora, Gentoo, Slackware, etc.

If you are in a position to make a fresh installation instead of converting, perhaps you are looking for my guide on making a fresh installation of Linux/GNU on a mirror.

Alignment issues are outside the scope of this document, so if you are planning on mixing disks with 512 and 4096 blocks, my guide won't be of help resolving any performance issues.

Also, please note that recently, I discovered that if you have a GPT disk label (rather than the "usual" msdos disk label), this procedure is somewhat more complicated. I'd have to write a separate guide on how to convert such a system. To find out what your disk label is, run parted on your primary drive, and use the "print" command, e.g. if your primary drive is /dev/sda:

sudo parted /dev/sda
print

Look for the line that reads "Partition Table:", and if it says gpt rather than msdos, do not proceed. Generally speaking GPT is used on drives of size 2TB and larger.

If your system is using GPT indeed, consider making a fresh installation with my guide on making a fresh installation of Linux/GNU on a mirror.

0) Prerequisites:
- A working Linux installation that you want to convert to use a mirrored system drive.
- Mdadm and Grub v2 installed on your hard drive installation (mdadm and grub2 packages in Debian/Ubuntu) .
- A second drive of the same capacity as your system drive. If you are in a tight spot and have to make do with drives of slightly different capacity, instead of picking the new drive to make the mirror on in step (3), pick the old drive, and skip to step (6). In step (7), make enough space at the end of the drive so that the data would fit on both drives and still have enough left over for the md superblock. Finally, in step (9), make sure you add the "--size" parameter to mdadm to tell it not to use the entire device. Make careful calculations so that your choice of "--size" would work for both drives. Also, keep in mind that in this case, you are working on your main drive and not on a backup.
- If you have two drives of different capacity, see my example at the end of this guide for a rough idea on how to proceed.

1) Boot into a LiveCD that preferably matches your hard drive installation, like Ubuntu 10.04.

2) Turn off swap in case the LiveCD discovered and started using a swap partition on your system drive.

sudo swapoff -a


3) Let's say your system drive is /dev/sda and your new drive is /dev/sdb, and the drive capacities are the same (see step (0)).

4) Make a "backup" copy of your system drive:

sudo dd if=/dev/sda of=/dev/sdb bs=1024k

5) If you are bored, you can monitor the progress of dd by running this command, which will make dd issue progress information:

sudo killall -SIGUSR1 dd


6) When done copying, you need to make a bit of space at the end of the drive for the md superblock, so run gparted on the drive what you wish to start the mirror with. For instance, let's say you've decided to start the mirror with the new drive first, and then add your original drive to the mirror:

sudo gparted /dev/sdb


7) Shrink the last partition on your drive (the right-most one) by anything more than 128 kilobytes. Let's say 8MB to be absolutely safe. If you are using an msdos label, don't forget that if the last partition is a logical partition (i.e. lives within an extended partition), you need to shrink the extended partition that houses it as well. First shrink the logical partition, THEN, the extended partition. To shrink partitions, right click on them and select Resize. To right-click on an extended partition is tricky, as you have to click on a very narrow strip of pixels at the edge of the box that represents your drive. Click apply, and wait for all operations to complete.

8) Make sure md is installed in your LiveCD, and load the md module:

sudo apt-get update
sudo apt-get install mdadm
sudo modprobe md_mod
sudo modprobe raid10

9) Create the array. I decided to make a RAID10 array instead of a RAID1 array, which gives me faster sequential reads, but amounts to the same thing in terms of data replication. RAID10 with two drives and "n2"
layout provides you with two identical drives with normal layout. See "man mdadm" for information on what each option below means.

sudo mdadm --create
--metadata=1.0 /dev/md0 -l10 -pn2 -c512 -n2 /dev/sdb missing
You should see output that reads something like "mdadm: array /dev/md0 started."

If you want a RAID1 mirror, then the line above is simpler:

sudo mdadm --create --metadata=1.0 /dev/md0 -l1 -n2 /dev/sdb missing


10) Depending on what LiveCD you are using, you may or may not already have the partitions on /dev/md0 detected. If you do, you should see devices like /dev/md0p1, etc. If you don't, you can try some of these
ways to make them appear:

sudo mdadm --assemble --scan
sudo udevadm trigger
sudo mdadm-startall


11) Once you see the partitions, mount your root partition (mine was /dev/md0p5):

sudo mount /dev/md0p5 /mnt

Also, if you have a separate /boot, /usr, and /var, mount those too:

sudo mount /dev/md0p1 /mnt/boot
sudo mount /dev/md0p7 /mnt/usr/
sudo mount /dev/md0p8 /mnt/var/

12) Edit your fstab, and change the entries that read either UUID=XXXXXXXXX or /dev/sdaN to /dev/md0pN. In other words, you need to make sure your system will not decide to use the raw devices underlying /dev/md0, but the mirror itself. If your device is indicated by a UUID, then hopefully there is some indication what the device name used to be at installation time in a comment. For example, where the device used to be /dev/sda1, it should now read /dev/md0p1

13) Save your array into /etc/mdadm/mdadm.conf, and rebuild your initrd in order to make sure the array gets assembled at boot time, and under the correct name (e.g. /dev/md0 instead of /dev/md_d0).

sudo chroot /mnt

mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev

apt-get update
apt-get install mdadm
mkdir -p /etc/mdadm/
mdadm --examine --scan > /etc/mdadm/mdadm.conf
update-initramfs -c -k all


If you get complaints that initrd is custom, you can force update-initramfs totake over the custom initrd with these options instead:

update-initramfs -ut -k all


DO NOT EXIT CHROOT YET!!

14) Configure and install grub v2 into the MBR of the mirror. For Ubuntu 10.04, it was trivially easy to do. First we make sure that Grub v2 is installed.

sudo apt-get install grub2 # Just in case the prerequisite was not satisfied.


Next, edit /etc/default/grub, and *uncomment* the line that reads "GRUB_DISABLE_LINUX_UUID=true".

grub-mkconfig > /boot/grub/grub.cfg
grub-install --recheck --modules="raid" /dev/md0


15) Reboot into your disk installation, but make sure you are booting off /dev/sdb, on which the mirror lives now. To do so, you can usually call up a one-time Boot menu with one of the F-keys (like F8) on many
systems. Otherwise, modify the necessary BIOS settings to do so.

Once you're in, you can add /dev/sda to the array that was created in steps 1-16 above.

sudo mdadm --add /dev/md0 /dev/sda

17) You can watch the process of resyncing the drives with:

watch cat /proc/mdstat


================================
Example of two 80GB drives of slightly different capacity, and how I dealt with them:
================================
[ 2.272122] sd 0:0:0:0: [sda] 156301488 512-byte logical blocks: (80.0 GB/74.5 GiB)
[ 2.441572] sd 2:0:0:0: [sdb] 156250000 512-byte logical blocks: (80.0 GB/74.5 GiB)

Using gparted, I shrunk the last partition by 32MB, and using fdisk, determined it ends at block 156232124. Then I used --size=78116352 with mdadm to make the mirror fit on either drive.

The kibibyte value I gave to --size was arrived at thus: 78116352=((156232124+580)*512/1024)

The value 156232124+580=156232704 was arrived as the smallest amount of 512byte blocks that gave me a round value in kibibytes (bytes/1024).