Creating A RAID1 (Software RAID) Setup in Ubuntu Server 9.10

For many, creating a RAID setup may seem like an elusive task. Frankly, its not as I will show you.

Redundant Array of Independent Disks is a method whereby many hard drives are configured to act as one. There are two main types of setup in RAID:-

RAID0 is in favour of performance/speed while RAID1 is in favour of redundancy/reliability. Disks that are to be configured in RAID1 or RAID0 (which are supposed to be 2 in number) are supposed to be identical. There are other RAID setups that involve more hard disks but I won’t get into them here. In Brief, these are the ones i theoretically know of:-

* RAID0+1 – uses RAID0 and RAID1 at the same time with four identical hard disk drives. If one of the hard disk drive fails, the system becomes a RAID0 system.

* RAID10 – uses RAID0 and RAID1 at the same time also with four identical hard disk drives. If one of the hard disk drive fails, the system becomes a RAID1 system.

* RAID5 – this one is actually a RAID0 system that needs at least three identical hard disk drives. It stores parity information to improve the reliability of the disks. In a case whereby three drives are used, the total capacity will be double the size of each hard disk drive. e.g If I use three 750GB hard drives, my total hard disk space will be 1500GB since the rest of the space will be used to store this parity data.

Aaaaaaanayway….

In RAID0, when data is written to the hard disks, it is divided into several chunks whereby each chunk will be saved on a different drive. I won’t get into the intricacies of how RAID divides and distributes the data today. However, the main point is that if a 500KB file is stored and 250KB stored on each hard drive, it takes half the time to store each file since the hard disks are working in parallel therefore meaning MORE SPEED.

RAID1 on the other hand shows the two hard disks as one. By this i mean, like in my case, two 750GB hard drives set up in RAID1 will show as 750GB in your system. This is because, data that is sent to the 750GB RAID Volume is copied to both physical hard drives. This is why RAID1 is also known as mirroring. If one hard drive fails, all the data exists in the other hard drive..alas! REDUNDANCY and RELIABILITY in the flesh!I opted for data redundancy since i was setting up a server.

The next option that comes along the way is which method of RAID setup to use. RAID can either be setup as Hardware RAID or Software RAID. Hardware RAID can only work on machines whose motherboards have so called RAID Controllers. In this case, RAID is set up in the BIOS. The controller makes the OS think there’s only one drive and maintains the drives ‘invisibly’. I opted for Software RAID whereby no controller is needed. In this case, the OS knows about both drives and actively maintains both of them. The truth is, Linux software RAID is ideal for mirroring, and due to kernel disk caching and buffering it can actually be faster than RAID1 on lower end RAID hardware. However, for larger requirements like RAID 5, the CPU can still get bogged down with software RAID.

So… lets set up our two hard disks with Ubuntu Server 9.10 (Karmic Koala).

Follow the installation steps until you get to the Partition disks step, then:

  1. Select Manual as the partition method.
  2. Select the first hard drive, and agree to “Create a new empty partition table on this device?”.

    Repeat this step for each drive you wish to be part of the RAID array.

  3. Select the “FREE SPACE” on the first drive then select “Create a new partition”.
  4. Next, select the Size of the partition. This partition will be the swap partition, and a general rule for swap size is twice that of RAM. Enter the partition size, then choose Primary, then Beginning.
  5. Select the “Use as:” line at the top. By default this is “Ext3 journaling file system”, change that to “physical volume for RAID” then “Done setting up partition”.
  6. For the / partition once again select “Free Space” on the first drive then “Create a new partition”.
  7. Use the rest of the free space on the drive and choose Continue, then Primary.
  8. As with the swap partition, select the “Use as:” line at the top, changing it to “physical volume for RAID” then choose “Done setting up partition”.
  9. Repeat steps three through eight for the other disk and partitions.

There should now be a list of hard drives and RAID devices. The next step is to format and set the mount point for the RAID devices. Treat the RAID device as a local hard drive, format and mount accordingly.

  1. Select the RAID1 device #0 partition.
  2. Choose “Use as:”. Then select “swap area”, then “Done setting up partition”.
  3. Next, select the RAID1 device #1 partition.
  4. Choose “Use as:”. Then select “Ext3 journaling file system”.
  5. Then select the “Mount point” and choose “/ – the root file system”. Change any of the other options as appropriate, then select “Done setting up partition”.
  6. Finally, select “Finish partitioning and write changes to disk”.

If you choose to place the root partition on a RAID array, the installer will then ask if you would like to boot in a degraded state. At some point in the life of the computer a disk failure event may occur. When this happens, using Software RAID, the operating system will place the array into what is known as a degraded state. If the array has become degraded, due to the chance of data corruption, by default Ubuntu Server Edition will boot to initramfs after thirty seconds. Once the initramfs has booted there is a fifteen second prompt giving you the option to go ahead and boot the system, or attempt manual recover. Booting to the initramfs prompt may or may not be the desired behavior, especially if the machine is in a remote location. Booting to a degraded array can be configured several ways:

  • The dpkg-reconfigure utility can be used to configure the default behavior, and during the p rocess you will be queried about additional settings related to the array. Such as monitoring, email alerts, etc. To reconfigure mdadm enter the following:
    sudo dpkg-reconfigure mdadm
  • The dpkg-reconfigure mdadm process will change the /etc/initramfs-tools/conf.d/mdadm configuration file. The file has the advantage of being able to pre-configure the system’s behavior, and can also be manually edited by setting the ‘boot degraded’ option to true (BOOT_DEGRADED=true)

Finish the rest of the install and you’re good to go. For a more detailed and visual example, check out this guy’s detailed setup in Ubuntu Server 8.04. Not much has changed in the setup. CHEERS!

http://kuparinen.org/martti/comp/ubuntu/en/raid.html

Getting rid of virbr0/virbr1 interface in Ubuntu Server 9.10

After a fresh install of Ubuntu 9.04,  you might have a network interface virbr0 if you’ve selected to install a virtual machine emulator.  During the CD install, selecting vm, it will install kvm and xen.  It will create a network interface virbr0 or virbr1 to perform host networking.

Later, if you decide to install VMWare, VirtualBox or decide you really didn’t want a virtual machine server, you can remove the applications easily.  Just do a (sudo):

apt-get purge kvm

apt-get purge libxen3

Getting the current action and controller from the view in Rails

This turned out to be quite simple. You might need it for a myriad of reasons at which point the dirty alternative (at least in rails) would be to do some URL parsing. I got a clean solution from the following post:

http://www.postal-code.com/mrhappy/blog/2007/01/18/rails-knowing-the-current-action-in-a-view/

If you add the following code to your application_controller.rb,

private

before_filter :instantiate_controller_and_action_names

def instantiate_controller_and_action_names

@current_action = action_name

@current_controller = controller_name

end

You will have access to the instance variables that represent the action and the controller. Pretty neat ay?

The above solution is for me the neatest one since it promotes DRY code.

The second solution is just using the controller keyword to access the values of the action and the controller right in your view:-

controller.controller_name

controller.action_name

P.S I was working with Rails 2.3 at the time

git error: unable to create temporary sha1 filename .git/objects/de: File exists

git is a great versioning system. The best I’ve worked with to be honest compared to SVN (which is the only other one I’ve worked with lol!) I haven’t looked at Mercurial yet. Either way, it can be annoying at times due to some ambigous error messages and commit errors because of simple conflicts. Anyway, I will share a solution to one of these headaches. Have you ever gotten this when you tried to pull your code from the server?

error: unable to create temporary sha1 filename .git/objects/de: File exists

In this case, it was my bad I guess. The solution is very simple. Just chown your .git directory recursively to the respective user/group or even your whole repository if you want. For example in my case:

sudo chown -R joram .git

That will be the end of such errors. But you have many more to come 😀

Getting the current URI (Everything AFTER the domain)

This is just a mini post of a simple thing that I might either way forget in future. So if you want to get the URI of the current page and what’s relevant to you is everything after the domain, try this:-

request.request_uri

In a URI like lets say http://www.mysite.com/downloads… it will return /downloads

A practical example of how I used it in a Rails app is here. Cheers!

<% if request.request_uri.eql?(root_path) %> cssnav_visited<% end %>”>Home

Getting rid of periods using Rails’ validates_format_of validation method

I just love the validation methods in Rails! They make life so much easier. Here’s how I checked that the file being uploaded to a server does not have periods(Long story… The title was ending up being a link to the download and thus the resultant URL messed up with the routing. Encoding the URL proved trickier than expected in this particular instance. If you have any insights on the encoding give me a shout on the comments).

validates_format_of         :title , :with => /\A[^.]*\Z/, :message => “cannot have periods(.)”

Cheers!

P.S This was in Rails 2.3

Displaying multiple blog pages in same wordpress blog

So this is the scenario… I have a website that is powered by wordpress. I have two different pages/views that should both be presented as blogs. The thing is WordPress haven’t yet come up with this feature. WordPress works such that there is one main BLOG and the others are just pages. The pages only contain a title and the content whereby a user can leave a reply to the content.

There are solutions for this tiff we find ourselves in. Some talk of WordPress MU which is a special implementation of WP. Google it up and try it out if you can. I didn’t have the time to delve into that solution since it needed fresh installation of an implementation I am not familiar with.

I found a brilliant and crafty solution for what I wanted. The use of categories and pages.

All you need to do is add this code before your WordPress loop.. in my case.. in the content.php file.. in yours you might have used page.php.

$catID = 0;

if (is_page(‘stories’)) {

$catID=3;

} elseif (is_page(‘about’)) {

$catID=0;

}

elseif (is_page(‘contact’)) {

$catID=0;

}

if ($catID) {

$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;

query_posts(“cat=$catID&paged=$paged”);

} ?>

All I’m doing is associating each page with a category ID. There are elaborate ways of obtaining this ID but I used the juvenile way because I’m a self confessed WordPress rookie ;-D Once you’re logged in your WordPress dashboard, go to Manage and then in Categories. Simply put your mouse cursor on the “edit” link related to the category you want to know the ID and look on your browser’s status bar.

Yeah.. noob.. i know :-D… but heeey! I rarely work with wordpress and it works for me. Aaaaanyway! Category ID 0 means that this page should not be viewed as a blog and will just be displayed as a normal page. For the stories page(in my example above), WordPress will instead get all the posts belonging to the category with ID of 3 and display them instead of a normal page.

Hope this works for you. For me it didn’t display the edit and comments link so if you find out how to fix that let me know. Cheers!

How to get all your packages back after a fresh install of Ubuntu

I found this in a forum… Pretty neat. Didn’t use it because in my case I didn’t want all the packages/apps I had before but still… Pretty cool!

dpkg --get-selections > installed-software

And if you wanted to use the list to reinstall this software on a fresh ubuntu setup,

dpkg --set-selections < installed-software

followed by:

dselect

Matching all occurrences of a regex

This was a bit stupid of me. So I’m blogging about it to avoid being this stupid again. I was working on my own little Rack middleware and i needed to get the contents of the url after the domain name i.e get the bolded strings here: http://www.pissoff.com/yes/why/dont/ya. I had the regex working ok and it was working on Rubular though  it wasn’t showing the matches i.e it was highlighting them but not listing them down as it should do usually. Here is the regex:

/[^\/]+/

The problem is I was using the Regexp class to match it and this was only producing the first match and returning all others as nil. Maybe I was using it in the wrong way, I don’t know. What I’m certain of is the fact that the String class worked much better.

So in a nutshell, I used the String class’ scan method to get all my matches as shown below:

url = “/admin/product/show/012”
url.scan(/[^\/]+/) { |match|
puts match
}

OUTPUT

admin
product
show
012