Tuesday, October 25, 2011

Small script to test perl module

To test any perl module, just put below lines in a file, make it executable (chmod +x) and run it (./).

#!/usr/bin/perl
use DBD-Pg;
print "No errors!\n"
where DBD-Pg is example of perl module. This script will return "No errors!" if the module is available. If the module is not installed, an error like "Can't locate ...." will appear instead.

Thursday, October 6, 2011

Extending LVM logical volume

To extend the lv (logical volume), the command is lvextend. Please check beforehand that you have free disk space to be extended to.

Check which volume group the partition belongs to:

# df -lh
Filesystem Size Used Avail Use% Mounted on
...
/dev/mapper/vg01-db 6.0T 6.0T 20K 100% /var/lib/hwbackup/db
...

# lvdisplay vg01
--- Logical volume ---
LV Name /dev/vg01/db
VG Name vg01
LV UUID 2LvWW6-5TIG-ovrl-GUJt-1y3N-M6ME-3LgtUY
LV Write Access read/write
LV Status available
# open 1
LV Size 5.99 TB
Current LE 1569792
Segments 5
Allocation inherit
Read ahead sectors auto
- currently set to 4096
Block device 253:2

So, our lv belongs to vg01

Now let's check how much space we have for vg01:

# vgdisplay vg01
...
Alloc PE / Size 4059648 / 15.49 TB
Free PE / Size 232788 / 909.33 GB
VG UUID iaFnIb-hKjs-q3fQ-yuTy-tKKl-uqSp-EXGyHF
...

So we know that it got 909.33 GB unused space.

Lets do the extension, let's say we want to extend by 300GB. Run a test run first:

# lvextend -t -v -L +300G /dev/mapper/vg01-db
Test mode: Metadata will NOT be updated.
Finding volume group vg01
Using stripesize of last segment 512.00 KB
Test mode: Skipping archiving of volume group.
Extending logical volume db to 6.28 TB
Found volume group "vg01"
Found volume group "vg01"
Found volume group "vg01"
Test mode: Skipping volume group backup.
Logical volume db successfully resized
Test mode: Wiping internal cache
Wiping internal VG cache
[root@hitw-gc-backup-1 ~]# lvextend -v -L +300G /dev/mapper/vg01-db
Finding volume group vg01
Using stripesize of last segment 512.00 KB
Archiving volume group "vg01" metadata (seqno 19).
Extending logical volume db to 6.28 TB
Found volume group "vg01"
Found volume group "vg01"
Loading vg01-db table (253:2)
Suspending vg01-db (253:2) with device flush
Found volume group "vg01"
Resuming vg01-db (253:2)
Creating volume group backup "/etc/lvm/backup/vg01" (seqno 20).
Logical volume db successfully resized
where -t is for test mode, -v for verbose and -L is for the size that you want to extend

If we are OK with the test, proceed to the real extension by removing -t:
# lvextend -v -L +300G /dev/mapper/vg01-db
Finding volume group vg01
Using stripesize of last segment 512.00 KB
Archiving volume group "vg01" metadata (seqno 19).
Extending logical volume db to 6.28 TB
Found volume group "vg01"
Found volume group "vg01"
Loading vg01-db table (253:2)
Suspending vg01-db (253:2) with device flush
Found volume group "vg01"
Resuming vg01-db (253:2)
Creating volume group backup "/etc/lvm/backup/vg01" (seqno 20).
Logical volume db successfully resized

Check your new lv size, it is now being extended by 300GB:
# lvdisplay /dev/mapper/vg01-db
...
LV Size 6.28 TB
...

To make your filesystem aware of the extension, run resize2fs (for ext3 filesystem). Please unmount the partition beofre you run resize2fs
# umount /dev/mapper/vg01-db
# resize2fs /dev/mapper/vg01-db

# mount /dev/mapper/vg01-db /var/lib/hwbackup/db
For xfs, you can resize it on the fly without unmounting it:
# xfs_growfs /var/lib/hwbackup/db
Now you can enjoy your newly extended lv:
# df -lh
Filesystem Size Used Avail Use% Mounted on
...
/dev/mapper/vg01-db 6.3T 5.7T 608G 91% /var/lib/hwbackup/db
...

Thursday, September 29, 2011

Replacing space with newline

There are a few ways to achieve that:

1. sed

$ echo "one two three" | sed 's/ /\n/g'
one
two
three
2. awk
$ echo "one two three" | awk '$1=$1' RS= OFS="\n"
one
two
three
3. tr
$ echo "one two three" | tr -s ' ' '\n'
one
two
three
3 ways to do it, have fun

Tuesday, September 27, 2011

Multiple sed expression in one line

To do multiple sed expression in one line, you can use -e a few times according to your need. For example:

To subtitute 'little' with 'big' and 'lamb' with 'cow' in "Mary had a little lamb" we can use:

$ echo 'Mary had a little lamb' | sed -e 's/little/big/' -e 's/lamb/cow/'
Mary had a big cow
where -e is for expression flag, 's/little/big/' is the first expression and 's/lamb/cow/' is the second expression

That's all.

Friday, September 23, 2011

Simple http server using python

This tip is really useful when you need a really quick web server running without too much hassle. Python comes with http server built-in that you can activate in a matter of seconds if you have python already installed. If you haven't install it, you might need an additional few minutes to install it. Below are the steps (this is assuming you are using redhat/fedora linux):

1. open a terminal and install python

# yum install python
2. Once done, browse to any directory that you want to be your document root directory, in this example I'll use /home/myuser
# cd /home/myuser
3. Run the below command to start the simple http server, where 8000 is the port number the server will use
# python -m SimpleHTTPServer 8000
Serving HTTP on 0.0.0.0 port 8000 ...
4. Now open a browser, and type http://192.168.1.2:8000 to access the server, where 192.168.1.2 is your machine's ip address