Setting-up RAID1 On RHEL6/CentOS


Well, I have been waiting for this since long! While RAID has been with us since ages, it has always been amazing me ever since I got into Administration 😉 Since I own a very basic laptop with a single 250 GB Hard Disk, testing & managing RAID has never been easy 😦 But, thanks to Flipkart!! I could find dirt cheap USB storage devices (pen-drives, that’s what most people call it. But its NOT a drive as there are no rotating/moving parts & no Read-Write heads) & today I could test it out on my sweet old laptop 😉 So, below is how to set-up a RAID on your RHEL 6 (and many other similar Linux distributions). I have demonstrated a RAID 1 (mirroring) here.

Note – All the set-up has been done on a RHEL 6.2 virtual machine running on Oracle Virtual Box with 2 8GB pen-drives.

1. Insert your pen-drives. Go to Devices & enable them.

Devices tab.

Devices tab.

 2. Change Partition Type to Linux RAID & reboot your server.

Changing Partition Type

Changing Partition Type

3. Create RAID1 by issuing below command.

mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=2 /dev/sdb1 /dev/sdc1

You will run into below error.

[root@server ~]# mdadm --create --verbose /dev/md0 --level=mirror --raid-devices =2 /dev/sdb /dev/sdc
mdadm: super1.x cannot open /dev/sdb: Device or resource busy
mdadm: ddf: Cannot use /dev/sdb: Device or resource busy
mdadm: Cannot use /dev/sdb: It is busy
mdadm: cannot open /dev/sdb: Device or resource busy

To fix this error, remove dmraid package & reboot your server.

[root@server ~]# yum erase dmraid

Now retry creating your RAID. It will be successful.

Array Created

4. Check RAID details.

[root@server ~]# mdadm --detail /dev/md0

RAID Details

5. Test RAID1 Functionality.

Create a file-system. Lets call it test_raid. Format RAID device /dev/md0 with ext4 file-system & mount /dev/sd0 to /test_raid.

[root@server ~]# mkfs.ext4 /dev/md0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
488640 inodes, 1953913 blocks
97695 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2004877312
60 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks:
 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks):done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@server ~]# mkdir /test_raid
[root@server ~]# mount /dev/md0 /test_raid/
[root@server ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 6.5G 2.7G 71% /
tmpfs 246M 100K 246M 1% /dev/shm
/dev/sda1 97M 28M 65M 30% /boot
/dev/md0 7.4G 145M 6.9G 3% /test_raid

cd to /test_raid & create a few files there.

[root@server ~]# cd /test_raid/

Since RAID1 means mirroring, our data will be stored on both the pen-drives. So, lets eject out one pen-drive & check RAID details once again. You may deselect one in Devices tab. See the text below in bold. It shows that no. of active device is now reduced to 1. Compare this with the RAID details above when there were 2 devices. Try changing to test_raid directory. Your data will still be there 🙂

[root@server test_raid]# mdadm --detail /dev/md0
/dev/md0:
 Version : 1.2
 Creation Time : Sat Mar 28 02:19:57 2015
 Raid Level : raid1
 Array Size : 7815652 (7.45 GiB 8.00 GB)
 Used Dev Size : 7815652 (7.45 GiB 8.00 GB)
 Raid Devices : 2
 Total Devices : 2
 Persistence : Superblock is persistent

Update Time : Sat Mar 28 02:25:01 2015
 State : active, degraded
 Active Devices : 1
Working Devices : 1
 Failed Devices : 1
 Spare Devices : 0

Name : server.shashank.com:0 (local to host server.shashank.com)
 UUID : ed128599:3e80284f:33ce9d8a:0d5f2014
 Events : 6

Number Major Minor RaidDevice State
 0 0 0 0 removed
 1 8 33 1 active sync /dev/sdc1

0 8 17 - faulty spare
Advertisements

Changing Prompt Color In Bash Shell Based On Userid


Edit your /etc/profile file & append the below text to change prompt-color to red when logged-in as root & to green when logged in as normal user 🙂

if [ $(id -u) -eq 0 ]; #if userid is 0, then change prompt color to red.
then
export PS1="\e[0;31m[\u@\h \W]\$ \e[m"
else # change prompt to green
export PS1="\e[0;32m[\u@\h \W]\$ \e[m"
fi

Caveat – The prompt will be $ in both the cases. I tried changing that to # but that echoed a numerical digit instead 😦

Quote

Using Expect Command To Automate User Creation on RHEL 6 – Part II


Continuing with where I left off, today I finally managed to write a script that will read the names of users from a text file & add them to the server. The whole process is automated & requires absolutely no human intervention. 😉 If you are new to expect, please read this post to get clear understanding of expect command.

Below is how to add users to the server using a while loop & expect 🙂

Execute this as :

[shashank@server ~]$ sudo sh ~shashank/scripts/useradd.sh

Hers is the script.

while read user; do
{
/usr/bin/expect << EOF
spawn useradd $user
puts "$user added"
expect "?shashank \r"
spawn passwd $user
expect "?Changing password for user $user. \r"
send "Password\r"
expect "?Retype new password: \r"
send "Password\r"
expect eof
puts "Password for $user set."
EOF
}
done </home/shashank/userslist.txt

Below is the output with list of 3 test users :-

spawn useradd test5
test5 added
spawn passwd test5
Changing password for user test5.
New password:
BAD PASSWORD: it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
Password for test5 set.
spawn useradd test6
test6 added
spawn passwd test6
Changing password for user test6.
New password:
BAD PASSWORD: it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
Password for test6 set.
spawn useradd test7
test7 added
spawn passwd test7
Changing password for user test7.
New password:
BAD PASSWORD: it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
Password for test7 set.

Using Expect Command To Automate User Creation on RHEL 6


Many a time my job requires me to add a number of users on a number of servers. The task can soon become painful once the number increases 😦

Thankfully, we have an amazing utility called Expect. Below is how I used Expect to automate user creation task. 🙂 This is basically a small script with self-explanatory tokens. I am also trying to incorporate a for /while loop so as to fully automate this task. This script is ideal for multiple users with same password.

#!/usr/bin/expect -f
#The script uses 2 arguments. user_name & password. Checking this condition.
if {[llength $argv] != 2} {
puts "usage: expect ./expect.sh user_name Password"
exit 1
}
set user [lindex $argv 0] #set user variable. Using first argument as username.
set pass [lindex $argv 1] #set password variable. Using second argument as username.
spawn useradd $user
puts "$user added" #print a message
expect "?server \r"
spawn passwd $user
expect "?Changing password for user $user. \r"
send "$pass\r"
expect "?Retype new password: \r"
send "$pass\r"
expect eof
puts "Password for $user set."

Below is the output 🙂

[shashank@server scripts]$ sudo expect ./expect.sh test4 Password
spawn useradd test4
test4 added
spawn passwd test4
Changing password for user test4.
New password:
BAD PASSWORD: it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
Password for test4 set.

Setting Up A Cron Job To Run Every 5 Minutes During A Specific Duration To Check Top CPU Consuming Processes.


Landed into a trouble yesterday that said CPU usage high for a specific duration of time. Further discussion took me to a finding that usage is high daily during that specific time 😦

So, decided to diagnose the issue & hence installed a new Cron job that will monitor CPU usage & processes during that time. Below is how it reads 😉

*/5 8,10 * * * /bin/ps -eo pcpu,pid,user,args | sort -k 1 -r | head -20 >> /home/shashank/CPU_Usage.txt

It will collect the report in the text file. Below is how the contents of report read (at a different time while testing).

%CPU PID USER COMMAND
0.0 9 root [ksoftirqd/1]
0.0 9024 root /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
0.0 8 root [stopper/1]
0.0 879 root [flush-253:3]
0.0 85 root [deferwq]
0.0 84 root [usbhid_resumer]
0.0 83 root [kpsmoused]
0.0 837 root [kauditd]
0.0 81 root [kthrotld/3]

Bash Script To Check If A Device Is In A Specific Network | Cutting First Octet of IP Address


Had to configure a Print Server few week back. While the configurations was easy, its testing was way too cumbersome. Thanks to this trick that did the job 🙂

Now that the configuration was correct, my task was to configure many (62 to be precise) printers on my Print Server. A few printers were in a different network. So, the task was to first ping them & determine their location. Doing it manually would have been a pain (pinging 62 devices) 😦 So I chose to write a script to help me achieve it 😉

Below is that script.

while read line
do
ping_response=`ping -c1 $line|grep PING|cut -d"(" -f2|cut -d")" -f1 2>/dev/null`
ipAddressFirstOctet=`echo "$ping_response"|cut -d"." -f1`
re='^[0-9]+$' //Any output other than ping response. Happens when ping doesn't find that host.
if [[ $ipAddressFirstOctet =~ $re ]]
then
if [ "$ipAddressFirstOctet" -eq 10 ] //10 is first octet for our DMZ
then
echo "$line ($ping_response) is TCS Printer"
else
echo "$line ($ping_response) is CSC Printer"
fi
fi
done<~shashank/printer_list