Wednesday, March 26, 2008

Find the inode size of the filesystem

# tune2fs -l /dev/sda1 | grep Inode

Restore a backup of a MySQL Database Server

# read the dump file back into the server
mysql db-name <>For example, to restore database called sales:

# First create the database sales:
$ mysql -u root -p

mysql> CREATE DATABASE sales;
mysql> quit;

# now restore database:
$ mysql -u root -p sales < /path/to/sales-backup-file.sql

Force DHCP client (dhclient) to renew ip address

$ sudo dhclient -r # release current ip
$ sudo dhclient # acquire new ip


How to format and connect to an iSCSI Volume

Install open-iscsi package for high performance, transport independent iSCSI implementation. This package is also known as the Linux Open-iSCSI Initiator.

Kernel version 2.6.16+

Install Open-iSCSI Initiator

$ sudo apt-get install open-iscsi

Open-iSCSI default configuration

Default configuration file: /etc/iscsi/iscsid.conf or ~/.iscsid.conf.

# vi /etc/iscsi/iscsid.conf
node.startup = automatic
node.session.auth.username = MY-ISCSI-USER
node.session.auth.password = MY-ISCSI-PASSWORD
discovery.sendtargets.auth.username = MY-ISCSI-USER
discovery.sendtargets.auth.password = MY-ISCSI-PASSWORD
node.session.timeo.replacement_timeout = 120
node.conn[0].timeo.login_timeout = 15
node.conn[0].timeo.logout_timeout = 15
node.conn[0].timeo.noop_out_interval = 10
node.conn[0].timeo.noop_out_timeout = 15
node.session.iscsi.InitialR2T = No
node.session.iscsi.ImmediateData = Yes
node.session.iscsi.FirstBurstLength = 262144
node.session.iscsi.MaxBurstLength = 16776192
node.conn[0].iscsi.MaxRecvDataSegmentLength = 65536

# /etc/init.d/open-iscsi restart


Now run a discovery against the iscsi target host:
# iscsiadm -m discovery -t sendtargets -p ISCSI-SERVER-IP-ADDRESS

For example:
# iscsiadm -m discovery -t sendtargets -p 192.168.1.60

# /etc/init.d/open-iscsi restart

Format iSCSI Volume

Now an additional drive should appear on the system, such as /dev/sdc. To find out device name:

# tail -f /var/log/messages

Create a partition:
# fdisk /dev/sdc

Format partition:
# mkfs.ext3 /dev/sdc1

Mount file system:
# mkdir /iscsi
# mount /dev/sdc1 /iscsi

Reboot after kernel panic

By default, the kernel will not reboot after a panic, to change this.

# vi /etc/sysctl.conf

kernel.panic = 10 # reboot after 10 seconds

Shell completion dig-in

Fine tunning Shell Completing stuff with ESC key

Bash allows you to fine tune file completion using ESC key combinations. People get amazed when I use ESC combination in front of them. For example, to inserts all possible completions into your command use ESC+*. Let us see how to backup all /etc/*.conf files, type the command:
tar -zcvf /dev/rt0 /etc/*.conf {hit ESC followed by *}
As soon as you hit Esc+*, shell replaces the /etc/*.conf part with names of all matching wild card patterns
tar -zcvf /dev/rt0 /etc/aatv.conf /etc/adduser.conf /etc/apg.conf /etc/brltty.conf /etc/ca-certificates.conf /etc/cvs-cron.conf /etc/cvs-pserver.conf /etc/debconf.conf ....

To displays all possible completions of command or filenames or username type ESC+?, to display all username start with the word le, type
cat ~le {hit ESC followed by ?}

complete command

There is also in built command called complete. It is used to specify how arguments are to be completed for a command. For example, when you type passwd (or any other user admin command such as su / usermod etc) and hit tab key, bash will show you a list of all available users i.e. all user admin related commands will see only user names:
complete -u su usermod userdel passwd chage write chfn groups slay w
Now type passwd and hit tab key to see all username:

# passwd {hit tab key}
Output:

avahi          bin            dhcp           gdm            haldaemon      klog           mail           news           root           sys            uucp
avahi-autoipd cupsys dnsmasq gnats hplip list man nobody sshd syslog vivek
backup daemon games guest irc lp messagebus proxy sync telnetd www-data


To make life even easier, source the file in the startup script:
$ source /etc/bash_completion

How to check if a shell script is being run by root

BASH stores a user's ID in $UID variable. Your effective user ID is stored in $EUID variable.

#Old way: just add a simple check at the start of the script:
#!/bin/bash
# Init
FILE="/tmp/out.$$"
GREP="/bin/grep"
#....
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
#A new way by using EUID
#!/bin/bash
# Init
FILE="/tmp/out.$$"
GREP="/bin/grep"
#....
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# ...
#only root can mount /dev/sdb1
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "You must be a root user" 2>&1
exit 1
else
mount /dev/sdb1 /mnt/disk2
fi