How to manage LVM in Linux

How to manage LVM in Linux

Creating Dynamic Disc

We are going to create three discs on a USB drive, each disc will have 512 MB. First, verify if we have connected the USB drive as /dev/sdc/:

# fdisk -l

If we have a USB drive connected as /dev/sdc:

# fdisk /dev/sdc

Command d can remove existing logical volumes on the USB drive.

Enter commands n, p, 1-3, +512M to create 3 logical volumes. For example, we create the first volume /dev/sdc1:

Command (m for help): p
Command (m for help): n
Partition number (1-3): 1
First sector (34-41943006, default 41940992): 34
Last sector, +sectors or +size{K,M,G,T,P}: +512M
Command (m for help): p

Command w saves new partition table and changes can not be undone.

Reload partition table:

# partprobe

Format created discs:

# pvcreate /dev/sdc1
# pvcreate /dev/sdc2
# pvcreate /dev/sdc3

Verify:

# pvdisplay

Create volume group DATA:

# vgcreate DATA /dev/sdc1 /dev/sdc2 /dev/sdc3

To be sure verify group capacity:

# vgdisplay

Create physical volume from 100% of physical volume capacity.

# lvcreate -l +100%FREE -n new_volume DATA

Format the new volume:

# mkfs -t ext3 /dev/DATA/new_volume

Create a directory for mount:

# mkdir /mnt/dynamic

Mount the new volume:

# mount /dev/DATA/new_volume /mnt/dynamic

Mount verification:

# df -hT

Mount verification using a graphical interface:

# system-config-lvm

Dynamic Resize Dynamic Disk

Create primary logical partition /dev/sdc4, that use the rest of the USB drive:

# fdisk /dev/sdc
Command (m for help):n
Command (m for help):4
Command (m for help):p
<enter>
<enter>

Command w saves a new partition table and it can not be undone.

Verify partition table:

# partprobe

Add new disk to physical LVL partition:

# pvcreate /dev/sdc4

Verify:

# pvdisplay

Extend volume group:

# vgextend DATA /dev/sdc4

Verify extended volume group:

# vgdisplay

Extend logical volume with 1 GB:

# lvextend -L+1G /dev/DATA/new_volume

Resize size of disk:

# resize2fs /dev/DATA/new_volume

Remount:

# mount -o remount /dev/DATA/new_volume

Verify:

# df -hT

Removing Dynamic Disc

Unmount logical volume:

# umount /dev/DATA/new_volume

Remove logical volume:

# lvremove /dev/DATA/new_volume

Remove volume group:

# vgremove /dev/DATA

Remove physical volumes:

# pvremove /dev/sdc1
# pvremove /dev/sdc2
# pvremove /dev/sdc3
# pvremove /dev/sdc4

Verification using a graphical interface:

# system-config-lvm

How to Make Software RAID in Linux

How to Make Software RAID in Linux

Add a new partition on the first physical disc:

# fdisk /dev/sda

Using the same option add a new partition on the second physical disc:

# fdisk /dev/sdb

Create a new RAID volume md0 type of RAID1 (raid level1):

# mdadm --create /dev/md0 --level=1 --raiddevices=2 /dev/sda7 /dev/sdb2

Check and verify addition:

# watch cat /proc/mdstat

Format the new RAID volume to ext3 filesystem:

# mkfs -t ext3 /dev/md0

Verify created RAID:

# mdadm --query /dev/md0

For more details enter:

# mdadm --query --detail /dev/md0

Create a directory for mounting RAID volume:

# mkdir /mirror

Mount RAID volume md0:

# mount /dev/md0 /mirror

Verify:

# df -hT

Let’s try to create directory /etc and file numbers.txt:

# cp –a /etc /mirror
# seq 10000000 > /mirror/cisla.txt

Verify:

# ls –ltr /mirror
# tail /mirror/subor.txt

You can anytime monitor volume [UU]:

# cat /proc/mdstat

We need to configure automatic mounting volume md0 after the system starts:

# cat /etc/mtab | grep md0
# joe /etc/fstab

Append next lines at the end of file /etc/fstab:

/dev/md0 /mirror ext3 defaults 0 0

Append next lines at the end of file /etc/mdadm.conf. If any error occured, the system will send an email about it.

DEVICE /dev/sd[a-z]*
ARRAY /dev/md0 level=raid1 devices=/dev/sda7,/dev/sdb2
MAILADDR root@localhost

Turn the monitoring service on:

# service mdmonitor start

Try to make disc down:

# yes “We love linux.“ > /mirror/linux.txt

If you pull out the disc during the yes command you should get an email with the error message:

# mutt

We can see one disc is not available [_U]:

# cat /proc/mdstat

Removing partitions that physically don’t exist:

# mdadm /dev/md0 –r detached

Removing partitions that physically exist:

# mdadm --manage /dev/md0 --remove /dev/sdb2

Connect physical disc and monitor how it is being synchronized:

# mdadm --manage /dev/md0 --add /dev/sdb2
# watch cat /proc/mdstat

Test RAID functionality. We should see “active raid1”:

# cat /proc/mdstat

verify RAID:

# mdadm --detail /dev/md0

Disable RAID:

# umount /dev/md0
# mdadm --manage --stop /dev/md0

Test RAID functionality again, we should not see “active raid1”:

# cat /proc/mdstat

RAID: removing

Unmount mounted partition:

# umount /mirror

Stop RAID (to start it again use –assemble –scan):

# mdadm --stop /dev/md0

Remove RAID:

# mdadm --remove /dev/md0

RAID partitions are readable, we can mount it:

# mount /dev/sdb2 /mnt

We should see details about RAID:

# ls –ltrh /mnt

Unmount RAID partition:

# umount /dev/sdb2

Best Linux Text Editors

Best Linux Text Editors

You can choose between several text editors in Linux. Each editor has advantages and advantages.

1. Vi/Vim
Vi is a powerful and the most popular command-line-based editor. Commonly used for writing code and editing configuration files. First of all, the advantage is availability. Vi is always installed on any distribution. The second advantage is the consumption of system resources. One of the cons is non-intuitive, but short commands.

Vi has 3 modes: command, input, and last line mode. Command mode is the default.

2. Nano
Nano is WYSIWYG (what you see is what you get) editor and is installed by default in Ubuntu and many other Linux distributions. Action/commands are done in a CTRL and Key manner, for example, CTRL + X save a file. Features: Autoconf support, case-sensitive search function, auto-indent ability, regular expression search and replace.

3. Gedit
Gedit is the default text editor for the GNOME desktop environment. Gedit’s aim is simple and easy to use for beginner Linux users. Useful features are syntax highlighting, clipboard support, brackets matching, search and replace with support of regular expressions

4. GNU Emacs
Emacs is the extensible self-documenting editor. It provides an interpreter for Emacs Lisp. Main function: text editing including a project planner, mail and newsreader, debugger interface, calendar.

5. Leaf Pad
GTK+ based editor is popular among new Linux users because it is easy to use. It supports the codeset option, auto codeset detection, and Drag & Drop function. It does not provide syntax coloring.

Linux How to Execute Script

Linux How to Execute Script

There are many ways how to execute a script, the first one is to execute the script by specifying the interpreter:

bash script.sh
sh script.sh

For debugging use option “-x” to see what is being done.

bash -x script.sh

As an interpreter, you can use sh, ksh, csh, bash etc.

If you want to execute the script without specifying an interpreter, you need to set execute (+x) permission:

chmod +x script.sh

Then you can execute the script following way:

./script.sh

Another way how to run the script:

source script.sh

The script doesn’t need to execute permission in this example. If the script makes any changes to the environment, it will be visible after running the script, because commands in the script are executed in the current shell.

Linux How to Extend Swap Space

Linux How to Extend Swap Space

Display swap usage summary

# swapon -s

Show how many space swap needs in megabytes

# free -m

Create new swap disk.

# fdisk /dev/sdb

Format new disk to swap format

# mkswap /dev/sdb1

Add new part of swap

# swapon /dev/sdb1

Verify if there is more space in swap space

# free -m

Append next line to file /etc/fstab.

/dev/sdb2 swap swap defaults 0 0

If you need old swap use

# swapoff /dev/sda6

Linux How to Change Hostname

Linux How to Change Hostname

Hostname identifies a computer on the network and it is visible in the terminal. We set up hostname during the installation operating system, but sometimes we want to change it.

Use command “hostname” to change hostname:

hostname new_hostname

Will set the hostname of the system to new_hostname. Open a new terminal to verify change hostname.

If you want to change the hostname permanent use the command “sysctl kernel.hostname” to change it:

sysctl kernel.hostname=NewHostname

Output: kernel.hostname = Newhostname

Command hostname without any parameter prints the current hostname of the system.

Linux How to Generate Random Password

Linux How to Generate Random Password

Generating password is relatively simple and can be done in different ways. You can install tools to generate passwords, a few examples:

  • mkpasswd (Debian/Ubuntu)
  • makepasswd (Debian/Ubuntu)
  • pwgen (CentOS/Fedora/RHEL)

When you don’t have installed these tools, here are useful commands:

date +%s | sha256sum | base64 | head -c 32 ; echo

We used time in seconds as a input to hash function sha-256 and print first 32 chars. You can replate sha256sum with other hash function (md5sum).

strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 32 | tr -d '\n'; echo

/dev/urandom is the built-in feature which generate random chars.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32; echo

Another way using /dev/urandom, even simpler.

You can also create the script:

generatePasswd () {
local l=$1
[ "$l" == "" ] && l=16
tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}

generatePasswd "$1"

Bash How to Return from Function

Bash How to Return from Function

Use the statement “return” to return from the function. You can also specify the return value. Example:

return 1234

We returned function status 1234 from function. Usually, we use 0 value for success and 1 for failure. It is similar to command exit which we use to terminate the script.

If you don’t use the return statement in the whole function, the status of the last executed command will be returned.

To verify returned status from the last called function use “$?”.

There is a difference between commands return and exit. The exit will cause the script to end at the line where it is called. Return will cause the current function to go out of scope and continue execution command after the function.

Bash How to Read from Keyboard

Bash How to Read from Keyboard

To read input from the keyboard and assign input value to a variable use the read command.

Read syntax:

read options var1 var2

To write the value of first entered word use:

read var1 var2
echo $var1

If you don’t give any argument to the read command, input will assign to the environment variable REPLY.

The “s” option does not echo input while reading from a keyboard.

read -s -p "Enter password:" $password

The -p “TEXT” option displays TEXT to the user without a newline.

The -e option means that command readline is used to obtain the line.
Option -u FD reads inputs from file descriptor FD (0,1,2).
Option -t TIME causes that read returns a failure if the input is not read within TIME seconds.

Linux: How to Connect External Hard Drive

Linux: How to Connect External Hard Drive

Diagnostic Commands

Display from log directory what was recently connected

# tail -20 /var/log/messages

Display list of the partition tables

# fdisk -l

Display device parameters

# hdparm /dev/sda

More detailed information

# hwbrowser

# more /etc/sysconfig/hwconf

Connection Process

Run fdisk

# fdisk /dev/sda

Get list of existing partitions

Command (m for help): p

Delete partition which exist on the drive

Command (m for help): d

Create new partition

Command (m for help): n

Verification before save

Command (m for help): p

Save new table to drive

Command (m for help): w

Update partition table in system

# partprobe

Alternative to partprobe is hdparm -z

# hdparm -z /dev/sda

Format created partition to ext3 file system

# mkfs.ext3 /dev/sda1

Verification if the partition is not mounted

# df -hT

Mount new-created partition to directory /mnt/usb. If a directory does not exist, first created it.

# mkdir /mnt/usb

# mount /dev/sda1 /mnt/usb

# df -hT

Display disk partitions

# parted /dev/sda unit GB print free

Bash Vs KSH

Bash Vs KSH

Linux and Unix have various shells. Two kinds of these numerous shells are KSH and BASH.

KSH (The Korn Shell) was developed many years before the BASH. Ksh has associative arrays and handles loop syntax better than bash. Also ksh’s command print is better than bash’s echo command. In other way, ksh does not support history completion, process substitution and rebindable command-line editing.

Bash has more added extension than ksh. Bash has tab completion and easier method to set a prompt in order to display current directory.

Compared to ksh, bash is newer and more popular.

Example of difference ksh and bash in condition test. First bash:

if [ $i -eq 3 ]

and condition test in ksh:

if (($i==3))

Bash can handle exit codes from pipes in a cleaner way. Bash and KSH are both Bourne=compatible shells, they share common functions and features and can be interchangeable to use.