Tuesday, December 6, 2011

Store svn password

To store your cert and password, all you have to do is:

1. Go to .subversion in your home folder

$ cd ~/.subversion
2. Run below command. This command will add store-auth-creds = yes and store-passwords = yes to your bottom of config file
$ echo -e "store-auth-creds = yes\nstore-passwords = yes" >> config
3. Run any svn command, and choose p for permanent accept certificate, and yes for storing your password.

Now you won't be asked for svn password anymore.

Friday, November 25, 2011

Add file type swap to linux

To add swap to linux machine:

1. Create a swap file. Size depends on your preference. Let's say we want to create a swap file with 8GB size (1024 x 1024 x 8 = 8388608).

# dd if=/dev/zero of=/swapfile bs=1024 count=8388608
where if is source, of is output file for dd to write to which is /swapfile in this case, bs is read/write byte size at a time and count is number of blocks.

2. Once created, make it a swap file
# mkswap /swapfile
3. Activate your swap file
# swapon /swapfile
4. Check your newly created swap space using free or top
# free -m
# top
5. To make it appear even after reboot, put it into fstab
# echo "swapfile swap swap defaults 0 0" >> /etc/fstab
It will look like this:
# cat /etc/fstab 
/swapfile1 swap swap defaults 0 0

Tuesday, November 1, 2011

ssh forward tunnel

To make this happen, the command is like below:

$ ssh -L 10022:target.local:22 middle.local
-L is for forward tunnel, 10022 is the port at localhost that we want to use, target.local is our target, 22 is the target's port that we want to forward and middle.local is our middleman server.

What the above command do is forwarding port 22 of target.local to port 10022 of localhost by using middle.local as a middleman. Once done, you can access port 22 of target.local just by SSHing into port 10022 in your localhost like below:
$ ssh localhost -p10022
and voila, you will be directed to target.local instead. This technique is useful when you have firewall blocking some ports, and you have server behind the firewall than can access those ports witth openssh installed.

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 (./).

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'
2. awk
$ echo "one two three" | awk '$1=$1' RS= OFS="\n"
3. tr
$ echo "one two three" | tr -s ' ' '\n'
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 port 8000 ...
4. Now open a browser, and type to access the server, where is your machine's ip address

Monday, August 15, 2011

Download apache directory listings recursively

To apache directory listings recursively, use wget like below:

$ wget -r -np -nH -R index.html http://filestodownload/

where -r is for recursive retrieving, -np is for no-parent option where wget won't get the parent directory when retrieving recursively, -nH equals to no host diretories where generation of host-prefixed directories will be disabled and -R is to omit index.html.

Friday, August 5, 2011

Extract rpm without installing

Run below command:

$ rpm2cpio packagename.rpm | cpio -idmv 
where rpm2cpio will change rpm file to cpio, -i is for extract, -d is to create leading directories where needed, -m is to preserve modification time and -v is for verbose mode.

Thursday, July 28, 2011

Creating a lot of same sized files

Let's say you need to create 10 files with 100kB size. All you need to do is create a 1MB files and split it into 10 files.

$ dd if=/dev/zero of=myfile bs=1024 count=1024
$ ls -lh
-rw-rw-r--. 1 owner owner 1.0M Jul 28 15:33 myfile
where if is input file, of is output file, bs is byte size and count is the multipler of bs

and then
$ split -b 100k myfile x
$ ls -lh
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xaa
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xab
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xac
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xad
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xae
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xaf
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xag
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xah
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xai
-rw-rw-r--. 1 owner owner 100K Jul 28 15:35 xaj
-rw-rw-r--. 1 owner owner 24K Jul 28 15:35 xak
where -b is byte size, myfile is your file input name and x is your prefix.

Tuesday, June 14, 2011

Convert time to unix timestamp and vice versa

To convert current time to unix timestamp

$ date +%s
where %s is seconds since 1970-01-01 00:00:00 UTC

To convert a unix timestamp to a readable time/date
$ date -d @1308021863
Mon Jun 13 22:24:23 CDT 2011
where -d is for displaying time described by the string after @

Wednesday, May 25, 2011

Install Al-Quran extension in openoffice

This was done in Fedora 13 with openoffice 3.2.0. Please follow below steps to install it:

1. Download the installation files

$ wget http://qioo.googlecode.com/files/QiOO-0.4b.zip

2. Extract it to somewhere
$ unzip QiOO-0.4b.zip
$ ls QiOO-0.4b
CHANGELOG QiOO-0.4b.oxt README.txt ScheherazadeRegOT.ttf

3. Copy ScheherazadeRegOT.ttf to /usr/share/fonts
$ sudo mkdir /usr/share/fonts/ScheherazadeRegOT; sudo cp -apv QiOO-0.4b/ScheherazadeRegOT.ttf /usr/share/fonts/ScheherazadeRegOT

4. Install the extension by opening openoffice writer and go to Tools > Extension Manager > Add, and search for the QiOO-0.4b.oxt plugin.

5. Activate CTL by going to Tools > Option > Language Settings > Languages > Enable For CTL(Complex Text Layout)

6. If all went well, you can see AlQuran menu in your openoffice writer's menubar like below.

That's all.

Friday, May 20, 2011

Cloning virtual machine on vmware esx using vmware-cmd

Below are the steps:

1. List down all the virtual machines that you have

# /usr/bin/vmware-cmd -l

2. Shutdown the machine that you are going to clone. Let say the machine is server-1
# /usr/bin/vmware-cmd /vmfs/volumes/47971de9-34657d96-8ac4-001d09068c0c/server-1/server-1.vmx stop

3. Use hard stop if you cannot stop it using above command
# /usr/bin/vmware-cmd /vmfs/volumes/47971de9-34657d96-8ac4-001d09068c0c/server-1/server-1.vmx stop hard

4. Make sure the vm is shutdown by viewing the state
# /usr/bin/vmware-cmd /vmfs/volumes/47971de9-34657d96-8ac4-001d09068c0c/server-1/server-1.vmx getstate

5. Copy the whole directory
# cp /vmfs/volumes/47971de9-34657d96-8ac4-001d09068c0c/server-1/ /vmfs/volumes/47971de9-34657d96-8ac4-001d09068c0c/newserver/

6. Once done, rename all the files to new name
# rename server-1 newserver /vmfs/volumes/47971de9-34657d96-8ac4-001d09068c0c/newserver/*

7. Change all occurrence of "server-1" to "newserver" in .vmx, .vmdk and .vmxf
# cd /vmfs/volumes/47971de9-34657d96-8ac4-001d09068c0c/newserver/
# sed -i 's/server-1/newserver/g' {newserver.vmdk,newserver.vmx,newserver.vmxf}

8. Edit newserver.vmx file and remove below lines
ethernet0.generatedAddress = "....."
ethernet0.generatedAddressOffset = "....."
uuid.location = "....."
uuid.bios = "....."

9. Add below line to newserver.vmx
uuid.action = "keep"

10. Register your newly cloned virtual machine. Please use the full path to register as below
# vmware-cmd -s register /vmfs/volumes/47971de9-34657d96-8ac4-001d09068c0c/newserver/newserver.vmx

11. Start your new vm after registration.
# /usr/bin/vmware-cmd /vmfs/volumes/47971de9-34657d96-8ac4-001d09068c0c/newserver/newserver.vmx start

Friday, February 4, 2011

vim comment lines

In linux, while editing configuration files, we always used # sign for comments. How do we do it to multiple lines in vi easily?


1. Edit the files using vi
$ vi files.conf

2. set line number in vi (for us to easily determine what is the line number that we want to edit)
:se nu

3. replace start of lines with # (for example, put # sign in front of line 2 to line 5). The meaning of below command is for line 2 - 5, subtitute (s) start of line (^) with # sign, and press enter

4. It is done. Save your file (w) and quit (q)