Sunday, January 22, 2012

WOL Wakeonlan Guide: Turn On Servers Remotely Without Physical Access


Wakeonlan (wol) enables you to switch ON remote servers without physically accessing it. Wakeonlan sends magic packets to wake-on-LAN enabled ethernet adapters and motherboards to switch on remote computers.

By mistake, when you shutdown a system instead of rebooting, you can use Wakeonlan to power on the server remotely. Also, If you have a server that don’t need to be up and running 24×7, you can turn off and turn on the server remotely anytime you want.

This article gives a brief overview of Wake-On-LAN and instructions to set up Wakeonlan feature.

Overview of Wake-On-LAN

  • You can use Wakeonlan when a machine is connected to LAN, and you know the MAC address of that machine.
  • Your NIC should support wakeonlan feature, and it should be enabled before the
    shut down. In most cases, by default wakeonlan is enabled on the NIC.
  • You need to send the magic packet from another machine which is connected to the same network ( LAN ). You need root access to send magic packet. wakeonlan package should be installed on the machine.
  • When the system crashes because of power failure, for the first time you cannot switch on your machine using this facility. But after the first first boot you can use wakeonlan to turn it on, if the server gets shutdown for some reason.
  • WakeonLan is also referred as wol.

Check whether wol is supported on the NIC

Execute the following ethtool command in the server which you want to switch ON from a remote place.
# ethtool eth0
Settings for eth0:
        Supported ports: [ TP MII ]
        Supported link modes:   10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
        Supports auto-negotiation: Yes
        Advertised link modes:  10baseT/Half 10baseT/Full
                                100baseT/Half 100baseT/Full
        Advertised auto-negotiation: Yes
        Speed: 100Mb/s
        Duplex: Full
        Port: MII
        PHYAD: 1
        Transceiver: internal
        Auto-negotiation: on
        Supports Wake-on: pumbg  [ Note: check whether flag g is present ]
        Wake-on: g [ Note: g mean enabled. d means disabled ]
        Current message level: 0x00000001 (1)
        Link detected: yes
If  Supports Wake-on is g, then the support for wol feature is enabled on the NIC card.

Enabling wol option on the Ethernet Card

By default the Wake-on will be set to g in most of the machines. If not, use ethtool to set the g flag to the wol option of the NIC card as shown below.
# ethtool -s eth0 wol g
Note: You should execute ethtool as root, else you may get following error message.
$ /sbin/ethtool eth0
Settings for eth0:
Cannot get device settings: Operation not permitted
Cannot get wake-on-lan settings: Operation not permitted
        Current message level: 0x000000ff (255)
Cannot get link status: Operation not permitted

Install wakeonlan package on a different machine

Install the wakeonlan package in the machine from where you need to send the magic packet to switch on your server.
# apt-get install wakeonlan

Note down the MAC address of the remote server

Note down the MAC address of the server that you wish to switch on remotely.
# ifconfig
eth0     Link encap:Ethernet  HWaddr 00:16:k5:64:A9:68  [ Mac address ]
          inet addr:192.168.6.56  Bcast:192.168.6.255  Mask:255.255.255.0
          inet6 addr: fe80::216:17ff:fe6b:289/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3179855 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2170162 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3832534893 (3.5 GB)  TX bytes:390304845 (372.2 MB)
          Interrupt:17

Finally, Switch ON the machine remotely without physical access

When the server is not up, execute the following command from another machine which is connected to the same LAN. Once the magic packet is sent, the remote system will start to boot.
# wakeonlan 00:16:k5:64:A9:68

Overview of RAMFS and TMPFS on Linux


Using ramfs or tmpfs you can allocate part of the physical memory to be used as a partition. You can mount this partition and start writing and reading files like a hard disk partition. Since you’ll be reading and writing to the RAM, it will be faster.

When a vital process becomes drastically slow because of disk writes, you can choose either ramfs or tmpfs file systems for writing files to the RAM.


Both tmpfs and ramfs mount will give you the power of fast reading and writing files from and to the primary memory. When you test this on a small file, you may not see a huge difference. You’ll notice the difference only when you write large amount of data to a file with some other processing overhead such as network.

1. How to mount Tmpfs

# mkdir -p /mnt/tmp

# mount -t tmpfs -o size=20m tmpfs /mnt/tmp
The last line in the following df -k shows the above mounted /mnt/tmp tmpfs file system.
# df -k
Filesystem      1K-blocks  Used     Available Use%  Mounted on
/dev/sda2       32705400   5002488  26041576  17%   /
/dev/sda1       194442     18567    165836    11%   /boot
tmpfs           517320     0        517320    0%    /dev/shm
tmpfs           20480      0        20480     0%    /mnt/tmp

2. How to mount Ramfs

# mkdir -p /mnt/ram

# mount -t ramfs -o size=20m ramfs /mnt/ram
The last line in the following mount command shows the above mounted /mnt/ram ramfs file system.
# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
tmpfs on /mnt/tmp type tmpfs (rw,size=20m)
ramfs on /mnt/ram type ramfs (rw,size=20m)
You can mount ramfs and tmpfs during boot time by adding an entry to the /etc/fstab.

3. Ramfs vs Tmpfs

Primarily both ramfs and tmpfs does the same thing with few minor differences.
  • Ramfs will grow dynamically.  So, you need control the process that writes the data to make sure ramfs doesn’t go above the available RAM size in the system. Let us say you have 2GB of RAM on your system and created a 1 GB ramfs and mounted as /tmp/ram. When the total size of the /tmp/ram crosses 1GB, you can still write data to it.  System will not stop you from writing data more than 1GB. However, when it goes above total RAM size of 2GB, the system may hang, as there is no place in the RAM to keep the data.
  • Tmpfs will not grow dynamically. It would not allow you to write more than the size you’ve specified while mounting the tmpfs. So, you don’t need to worry about controlling the process that writes the data to make sure tmpfs doesn’t go above the specified limit. It may give errors similar to “No space left on device”.
  • Tmpfs uses swap.
  • Ramfs does not use swap.

4. Disadvantages of Ramfs and Tmpfs

Since both ramfs and tmpfs is writing to the system RAM, it would get deleted once the system gets rebooted, or crashed. So, you should write a process to pick up the data from ramfs/tmpfs to disk in periodic intervals. You can also write a process to write down the data from ramfs/tmpfs to disk while the system is shutting down. But, this will not help you in the time of system crash.
Table: Comparison of ramfs and tmpfs
ExperimentationTmpfsRamfs
Fill maximum space and continue writingWill display errorWill continue writing
Fixed SizeYesNo
Uses SwapYesNo
Volatile StorageYesYes

If you want your process to write faster, opting for tmpfs is a better choice with precautions about the system crash.

6 Awesome Linux cd command Hacks – Productivity Tips

Hack #1: Use CDPATH to define the base directory for cd command
If you are frequently doing cd to subdirectories of a specific parent directory, you can set the CDPATH to the parent directory and perform cd to the subdirectories without giving the parent directory path as explained below.
[ramesh@dev-db ~]# pwd
/home/ramesh

[ramesh@dev-db ~]# cd mail
-bash: cd: mail: No such file or directory
[Note: This is looking for mail directory under current directory]

[ramesh@dev-db ~]# export CDPATH=/etc
[ramesh@dev-db ~]# cd mail
[Note: This is looking for mail under /etc and not under current directory]

[ramesh@dev-db /etc/mail]# pwd
/etc/mail
To make this change permanent, add export CDPATH=/etc to your ~/.bash_profile
This hack can be very helpful under the following situations:
  • Oracle DBAs frequently working under $ORACLE_HOME, can set the CDPATH variable to the oracle home
  • Unix sysadmins frequently working under /etc, can set the CDPATH variable to /etc
  • Developers frequently working under project directory /home/projects, can set the CDPATH variable to /home/projects
  • End-users frequently accessing the subdirectories under their home directory, can set the CDPATH variable to ~ (home directory)

Hack #2: Use cd alias to navigate up the directory effectively

When you are navigating up a very long directory structure, you may be using cd ..\..\ with multiple ..\’s depending on how many directories you want to go up as shown below.
# mkdir -p /tmp/very/long/directory/structure/that/is/too/deep

# cd /tmp/very/long/directory/structure/that/is/too/deep
# pwd
/tmp/very/long/directory/structure/that/is/too/deep

# cd ../../../../
# pwd
/tmp/very/long/directory/structure
Instead of executing cd ../../../.. to navigate four levels up, use one of the following alias methods:

Navigate up the directory using ..n : In the example below, ..4 is used to go up 4 directory level, ..3 to go up 3 directory level, ..2 to go up 2 directory level. Add the following alias to the .bash_profile and re-login.
alias ..="cd .."
alias ..2="cd ../.."
alias ..3="cd ../../.."
alias ..4="cd ../../../.."
alias ..5="cd ../../../../.."

# cd /tmp/very/long/directory/structure/that/is/too/deep
#..4
[Note: use ..4 to go up 4 directory level]
# pwd
/tmp/very/long/directory/structure/
Navigate up the directory using only dots: In the example below, ….. (five dots) is used to go up 4 directory level.  Typing 5 dots to go up 4 directory structure is really easy to remember, as when you type the first two dots, you are thinking “going up one directory”, after that every additional dot, is to go one level up. So, use …. (four dots) to go up 3 directory level and .. (two dots) to go up 1 directory level. Add the following alias to the .bash_profile and re-login for the ….. (five dots) to work properly.
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ......="cd ../../../../.."

# cd /tmp/very/long/directory/structure/that/is/too/deep
# .....
[Note: use ..... (five dots) to go up 4 directory level]
# pwd
/tmp/very/long/directory/structure/
Navigate up the directory using cd followed by consecutive dots: In the example below, cd….. (cd followed by five dots) is used to go up 4 directory level. Making it 5 dots to go up 4 directory structure is really easy to remember, as when you type the first two dots, you are thinking “going up one directory”, after that every additional dot, is to go one level up. So, use cd…. (cd followed by four dots) to go up 3 directory level and cd… (cd followed by three dots) to go up 2 directory level. Add the following alias to the .bash_profile and re-login for the above cd….. (five dots) to work properly.
alias cd..="cd .."
alias cd...="cd ../.."
alias cd....="cd ../../.."
alias cd.....="cd ../../../.."
alias cd......="cd ../../../../.."

# cd /tmp/very/long/directory/structure/that/is/too/deep
# cd.....
[Note: use cd..... to go up 4 directory level]
# pwd
/tmp/very/long/directory/structure

Hack #3: Perform mkdir and cd using a single command

Sometimes when you create a new directory, you may cd to the new directory immediately to perform some work as shown below.
# mkdir -p /tmp/subdir1/subdir2/subdir3
# cd /tmp/subdir1/subdir2/subdir3
# pwd
/tmp/subdir1/subdir2/subdir3
Wouldn’t it be nice to combine both mkdir and cd in a single command? Add the following to the .bash_profile and re-login.
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
Now, perform both mkdir and cd at the same time using a single command as shown below:
# mkdircd /tmp/subdir1/subdir2/subdir3
[Note: This creates the directory and cd to it automatically]
# pwd
/tmp/subdir1/subdir2/subdir3

Hack #4: Use “cd -” to toggle between the last two directories

You can toggle between the last two current directories using cd – as shown below.
# cd /tmp/very/long/directory/structure/that/is/too/deep
# cd /tmp/subdir1/subdir2/subdir3

# cd -
# pwd
/tmp/very/long/directory/structure/that/is/too/deep

# cd -
# pwd
/tmp/subdir1/subdir2/subdir3

# cd -
# pwd
/tmp/very/long/directory/structure/that/is/too/deep
Note: You can also substitute an argument from other commands in the history to the cd command using example#12 and #13 mentioned in the command line history examples article.

Hack #5: Use dirs, pushd and popd to manipulate directory stack

You can use directory stack to push directories into it and later pop directory from the stack. Following three commands are used in this example.
  • dirs: Display the directory stack
  • pushd: Push directory into the stack
  • popd: Pop directory from the stack and cd to it
Dirs will always print the current directory followed by the content of the stack. Even when the directory stack is empty, dirs command will still print only the current directory as shown below.
# popd
-bash: popd: directory stack empty

# dirs
~

# pwd
/home/ramesh
How to use pushd and popd? Let us first create some temporary directories and push them to the directory stack as shown below.
# mkdir /tmp/dir1
# mkdir /tmp/dir2
# mkdir /tmp/dir3
# mkdir /tmp/dir4

# cd /tmp/dir1
# pushd .

# cd /tmp/dir2
# pushd .

# cd /tmp/dir3
# pushd .

# cd /tmp/dir4
# pushd .

# dirs
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
[Note: The first directory (/tmp/dir4) of the dir command output is always
          the current directory and not the content from the stack.]
At this stage, the directory stack contains the following directories:
/tmp/dir4
/tmp/dir3
/tmp/dir2
/tmp/dir1
The last directory that was pushed to the stack will be at the top. When you perform popd, it will cd to the top directory entry in the stack and remove it from the stack. As shown above, the last directory that was pushed into the stack is /tmp/dir4. So, when we do a popd, it will cd to the /tmp/dir4 and remove it from the directory stack as shown below.
# popd
# pwd
/tmp/dir4

[Note: After the above popd, directory Stack Contains:
/tmp/dir3
/tmp/dir2
/tmp/dir1]

# popd
# pwd
/tmp/dir3

[Note: After the above popd, directory Stack Contains:

/tmp/dir2
/tmp/dir1]

# popd
# pwd
/tmp/dir2

[Note: After the above popd, directory Stack Contains: /tmp/dir1]

# popd
# pwd
/tmp/dir1

[Note: After the above popd, directory Stack is empty!]

# popd
-bash: popd: directory stack empty

Hack #6: Use “shopt -s cdspell” to automatically correct mistyped directory names on cd

Use shopt -s cdspell to correct the typos in the cd command automatically as shown below. If you are not good at typing and make lot of mistakes, this will be very helpful.
# cd /etc/mall
-bash: cd: /etc/mall: No such file or directory

# shopt -s cdspell
# cd /etc/mall
# pwd
/etc/mail
[Note: By mistake, when I typed mall instead of mail,
          cd corrected it automatically]

Execution sequence for .bash_profile, .bashrc, .bash_login, .profile and .bash_logout


This article will explain the sequence in which the following files are executed:
  • /etc/profile
  • ~/.bash_profile
  • ~/.bashrc
  • ~/.bash_login
  • ~/.profile
  • ~/.bash_logout

Execution sequence for interactive login shell

Following pseudo code explains the sequence of execution of these files.
execute /etc/profile
IF ~/.bash_profile exists THEN
    execute ~/.bash_profile
ELSE
    IF ~/.bash_login exist THEN
        execute ~/.bash_login
    ELSE
        IF ~/.profile exist THEN
            execute ~/.profile
        END IF
    END IF
END IF
When you logout of the interactive shell, following is the sequence of execution:
IF ~/.bash_logout exists THEN
    execute ~/.bash_logout
END IF
Please note that /etc/bashrc is executed by ~/.bashrc as shown below:
# cat ~/.bashrc
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

Execution sequence for interactive non-login shell

While launching a non-login interactive shell, following is the sequence of execution:
IF ~/.bashrc exists THEN
    execute ~/.bashrc
END IF
Note: When a non-interactive shell starts up, it looks for ENV environment variable, and execute the file-name value mentioned in the ENV variable.

Test the sequence of execution

One of the ways to test the sequence of execution is by adding different PS1 values to these files and re-login to the shell and see which PS1 value got picked up by the Linux prompt. Also, earlier we discussed about how to use PS1 to make your Linux prompt both functional and stylish.

1. /etc/profile gets executed. Add following PS1 line to /etc/profile and re-login to make sure the Linux prompt changes to the PS1 value set inside the /etc/profile.
# grep PS1 /etc/profile
PS1="/etc/profile> "

[Note: re-login to see the prompt change as shown below]
Last login: Sat Sep 27 16:43:57 2008 from 192.168.1.2
/etc/profile>
Please make sure ~/.bash_profile doesn’t have any PS1 for the above to work properly.
2. ~/.bash_profile gets executed: Add following PS1 to ~/.bash_profile, ~/.bash_login, ~/.profile and ~/.bashrc. Re-login to make sure the Linux prompt changes to the PS1 value set inside the  ~/.bash_profile as shown below.
/etc/profile> grep PS1 ~/.bash_profile
export PS1="~/.bash_profile> "

/etc/profile> grep PS1 ~/.bash_login
export PS1="~/.bash_login> "

/etc/profile> grep PS1 ~/.profile
export PS1="~/.profile> "

/etc/profile> grep PS1 ~/.bashrc
export PS1="~/.bashrc> "

[Note: Upon re-login, it executed /etc/profile first and ~/.bash_profile next.
So, it took the PS1 from ~/.bash_profile as shown below.
It also did not execute ~/.bash_login, as ~/.bash_profile exists]
Last login: Sat Sep 27 16:48:11 2008 from 192.168.1.2
~/.bash_profile>
3. ~/.bash_login gets executed. Rename the .bash_profile to something else. Re-login to make sure the Linux prompt changes to the PS1 value set inside the  ~/.bash_login as shown below.
~/.bash_profile> mv .bash_profile bash_profile_not_used

[Note: Upon re-login, it executed /etc/profile first.
Since it cannot find ~/.bash_profile, it executed ~/.bash_login]
Last login: Sat Sep 27 16:50:55 2008 from 192.168.1.2
~/bash_login>
4. ~/.profile gets executed. Rename the .bash_login to something else. Re-login to make sure the Linux prompt changes to the PS1 value set inside the  ~/.profile as shown below.
~/.bash_login> mv .bash_login bash_login_not_used

[Note: Upon re-login, it executed /etc/profile first.
Since it cannot find ~/.bash_profile and ~/.bash_login, it executed ~/.profile]
Last login: Sat Sep 27 16:56:36 2008 from 192.168.1.2
~/.profile>
5. ~/.bashrc gets executed for non-login shell testing. Executing bash” at the command prompt will give another non-login shell, which will invoke .bashrc as shown below.
~/.profile> bash

[Note: This displays PS1 from .bashrc as shown below.]
~/.bashrc> exit
exit

[Note: After exiting from non-login shell, we are back to login shell]
~/.profile>

10 Tips to Use Your Hardware and Software Vendor Support Effectively


Companies purchase support for most of their enterprise hardwares (servers, switches, routers, firewalls etc.,) and softwares (databases, OS, applications, frameworks etc.,). They spend lot of cash on support mainly for two reasons: 1) To get help from vendors to fix critical production issues 2) To keep up-to-date with the latest version of the software and security patches released by the vendors. In this article, I’ve given 10 practical tips for DBAs, sysadmins and developers to use their hardware and software support effectively.

1. Use the Knowledge Base
Most vendors have dedicated support website including a separate knowledge base section with lot of white papers, best practice documents, troubleshooting tips and tricks. Use the knowledge base section of support website to learn and expand your knowledge. Most of the time, the best possible solution to solve a specific problem can be found from the knowledge base or forum of your vendor support website. For example, when you have an issue setting up Automatic Storage Management during Oracle 11g installation, Oracle’s support website metalink, will give you appropriate solution than searching Google.

2. Use support website to create ticket

HTTP Support websiteInstead of calling the support over phone, use their website to create a ticket. It is not easy to explain complex technical issue in detail to the support person over phone. Even when you take time to explain the issue in detail over phone, they may still miss lot of details or write the issue description little differently. This will cause unnecessary delay, as you’ve to explain the problem again to the support engineer who will be assigned to the ticket. If you create the ticket yourself from their website, you can upload all the supporting materials and copy/paste the error message. After you create a ticket from their website, call the support to follow-up and make sure an engineer is getting assigned to it immediately. If they don’t have a support website, ask them whether you can create a ticket by sending an email.

3. Explain the issue in detail

Explain the detailsProvide as much as information possible in the ticket description. Don’t assume that the support engineer will understand the issue just by looking at the error message you’ve provided. Providing as much as information upfront in the ticket will help you avoid lot of wasted time going back and forth explaining the issues in detail to the support. Provide a clear step-by-step instructions on how to reproduce the issue.

4. Do some research and debugging before submitting the ticket

Before creating a ticket, perform some basic debugging to eliminate some of the common issues. Attach related log files and debugging output to the ticket. If you’ve worked with your vendor before, you’ll have a good idea of all the basic log files and testing they may ask you to perform. Don’t wait for them to ask the same thing again. Go-ahead and do those basic testing yourself and attach all the log files to the ticket.

5. Don’t waste time with first level of support

Dealing with first level of support is waste of time for complex issues. If you’ve done #2, #3 and #4 mentioned above properly, call the support and demand them to escalate it to the second level of support. If they don’t respond properly, escalate the issue through vendor’s account manager assigned to your company.

6. Use support for your research project

Don’t just call support only for production issues. Call them even for your research project. For example, if you are performing a prototype of a new software that was released by your vendor, call the support to get their help when you get stuck. When you are testing their new bleeding edge software, that was released recently, most of the vendors will even assign a dedicated resource to help you resolve the issue, as they want to fix all the issues in their new software as soon as posible.

7. Setup your support profile

Anytime you create a ticket, you may have to repeatedly enter some basic information related to your account and environment. Most of the support site has the ability to setup a profile with all the basic information, which you can use when you are creating a ticket. This will speed up the ticket creation process.

8. Setup support access for admins

Make sure all your DBAs, sysadmins and senior developers have access to the support website. If you are the only person who has access to support website, identify another backup resource for you and make sure they know how to access the support website to create a ticket, when you are not available. Also, create a separate support-access document with vendors support telephone number,  your account number, support website URL and put it in a shared area where all admins can access it.

9. Subscribe to security alert

Security AlertIt is very important for DBAs, sysadmins, and senior developers to subscribe to the security alerts from the support website. If there are any critical security updates that affects your hardware and software, it should be immediately tested on test environment and moved to production.  I have seen admins who receive the security alerts, but don’t read those emails consistently. It is very important to act on security alerts from your vendors immediately.

10. Get official documentation and diagnostics tools

Read DocumentationUse support to get official documentation for your hardware and software. Call your vendor support and ask for diagnostics tools and best practice documents for maintaining your hardware and software. Most of us hate to read documentation. But experienced developers and admins understand that reading official documentation of hardware and software will give them in-depth understanding about the product.

Do you use support from your hardware and software vendors? If you have any tips, please leave a comment.