Pages

Wednesday, May 24, 2017

Migrate OS from one HDD to another

Migrate standard RHEL installation from one hard disk to another

Posted on 
You have one standard installation of RHEL, you need migrate the installation from one hard disk to another, this is required due to technology improvement.
The server is productive and has running critical services, so is important minimize the migration window, this procedure requires only one reboot, if you want to apply all changes immediately but you can program the restart after.
For x86_64
Scenario:
vda -> Old Disk
vdb -> New Disk
centos -> root volume group
Partitioning:
# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  983M   17G   6% /
devtmpfs                 487M     0  487M   0% /dev
tmpfs                    497M     0  497M   0% /dev/shm
tmpfs                    497M  6.7M  490M   2% /run
tmpfs                    497M     0  497M   0% /sys/fs/cgroup
/dev/vda1                497M  164M  333M  33% /boot
tmpfs                    100M     0  100M   0% /run/user/0
tmpfs                    100M     0  100M   0% /run/user/1000
# fdisk -l
   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    41943039    20458496   8e  Linux LVM
Steps:
Clean yum cache:
# yum clean all
Clone partitioning scheme:
# sfdisk -d /dev/vda | sfdisk --force /dev/vdb
Move Logical Volume to new disk:
# pvcreate /dev/vdb2
# vgextend centos /dev/vdb2
# pvmove /dev/vda2
# vgreduce centos /dev/vda2
# pvremove /dev/vda2
Clone /boot:
# umount /boot/
# dd if=/dev/vda1 of=/dev/vdb1 bs=512 conv=noerror,sync
# mount /boot
Copy boot sector:
# dd if=/dev/vda of=/dev/vdb bs=1 count=512
Install GRUB in new disk:
# grub2-install /dev/vdb
Sync changes:
# sync
Reboot your physical or virtual machine, please make sure that your new disk is the default boot device or remove old disk but don't delete data, can be useful in a rollback situation.
For POWER
Scenario:
sda -> Old Disk
sdb -> New Disk
ca -> root volume group
Partitioning:
# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/ca-root   28G  1.1G   27G   4% /
devtmpfs             449M     0  449M   0% /dev
tmpfs                495M     0  495M   0% /dev/shm
tmpfs                495M   12M  484M   3% /run
tmpfs                495M     0  495M   0% /sys/fs/cgroup
/dev/sda2            497M  143M  354M  29% /boot
tmpfs                 99M     0   99M   0% /run/user/0
# fdisk -l
  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048       10239        4096   41  PPC PReP Boot
/dev/sda2           10240     1034239      512000   83  Linux
/dev/sda3         1034240    62914559    30940160   8e  Linux LVM
Steps:
Clean yum cache:
# yum clean all
Clone partitioning scheme:
# sfdisk -d /dev/sda | sfdisk --force /dev/sdb
Move Logical Volume to new disk:
# pvcreate /dev/sdb3
# vgextend centos /dev/sdb3
# pvmove /dev/sda3
# vgreduce centos /dev/sda3
# pvremove /dev/sda3
Clone PPC PReP Boot partition:
dd if=/dev/sda1 of=/dev/sdb1 bs=512 conv=noerror,sync
Clone /boot:
# umount /boot/
# dd if=/dev/sda2 of=/dev/sdb2 bs=512 conv=noerror,sync
# mount /boot
Copy boot sector:
# dd if=/dev/sda of=/dev/sdb bs=1 count=512
Install GRUB in new disk:
# grub2-install /dev/sdb
If you receive this message: grub2-install: error: the chosen partition is not a PReP partition. maybe you can try with:
# grub2-install /dev/sdb1
Sync changes:
# sync
Reboot your physical or virtual machine, please make sure that your new disk is the default boot device or remove old disk but don't delete data, can be useful in a rollback situation.

About LUN's and Multipath

What is a LUN


In computer storage, a Logical Unit Number, or LUN, is a number used to identify a logical unit, which is a device addressed by SAN. LUN vs. SCSI Device ID: The LUN is not the only way to identify a logical unit. There is also the SCSI Device ID, which identifies a logical unit uniquely in the world

How to scan LUN in the server

For FC Host

# echo "1" > /sys/class/fc_host/host/issue_lip

Above command performs a Loop Initialization Protocol (LIP) and then scans the interconnect and causes the SCSI layer to be updated to reflect the devices currently on the bus. A LIP is, essentially, a bus reset,  and will cause device addition and removal. This procedure is necessary to configure a new SCSI target on a Fibre Channel interconnect. Bear in mind that issue_lip is an asynchronous operation.

The command may complete before the entire scan has completed. You must monitor /var/log/messages to determine when it is done. The lpfc and qla2xxx drivers support issue_lip

For SCSI Host

# echo "- - -" > /sys/class/scsi_host/host0/scan

It means that you are echoing a wildcard value of "channel target and lun" (CTL), and the operating system will rescan the device path.


The Universally Unique Identifier can be used to identify a device independent form its mount point or device name. This is more and more important as many devices today support hot-plugging or are external anyway. Therefore it makes sometimes sense to access a device (for example in fstab) not by device name but by the UUID.

There are several ways to get the UUID. The first one uses the /dev/ directory. While you are on is you might want to check other by-* directories, I never knew of them.


The reason for using links is because kernel device names (e.g. /dev/sda, /dev/sdb) may change depending on where or when the disk is plugged in, whereas the links in /dev/disk/by-* will always point to the same drive no matter what, and therefore are safer to use.

How to find the Number of HBA's in the server

# lspci | grep -i HBA

04:00.0 Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)


05:00.0 Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)

Find out how many host bus adapter configured in the Linux box. You can use “systool -fc_host -v” to verify available FC in the system.


3.If the system virtual memory is too low ,then do not proceed further.If you have enough free virtual memory,then you can proceed with below command to scan new LUNS.

pwd

/sys/class/fc_host/host1

-r--r--r-- 1 root root 4096 Apr  7 03:06 fabric_name
--w------- 1 root root 4096 Apr  7 03:06 issue_lip
-r--r--r-- 1 root root 4096 Apr  7 03:06 node_name
-r--r--r-- 1 root root 4096 Apr  7 03:06 port_id
-r--r--r-- 1 root root 4096 Apr  5 08:40 port_name
-r--r--r-- 1 root root 4096 Apr  5 08:40 port_state
-r--r--r-- 1 root root 4096 Apr  7 03:06 port_type
-r--r--r-- 1 root root 4096 Apr  7 03:06 speed
drwxr-xr-x 2 root root    0 Apr  4 18:22 statistics
lrwxrwxrwx 1 root root    0 Apr  4 18:24 subsystem -> ../../../class/fc_host
-r--r--r-- 1 root root 4096 Apr  7 03:06 supported_classes
-r--r--r-- 1 root root 4096 Apr  7 03:06 supported_speeds
-r--r--r-- 1 root root 4096 Apr  7 03:06 symbolic_name
-rw-r--r-- 1 root root 4096 Apr  7 03:06 system_hostname
-rw-r--r-- 1 root root 4096 Apr  7 03:06 tgtid_bind_type
--w------- 1 root root 4096 Apr  4 18:23 uevent

2.Find out how many SCSI controller configured.

[root@mylinz1 ~]# ls /sys/class/scsi_host/host
host0 host1 host2
In this case,you need to scan host0,host1 & host2.

How to check the HBA driver version


Method 1

[test@linux:NODB /proc/scsi] # lspci | grep -i HBA

04:00.0 Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)

[test@linux:NODB /proc/scsi] # lspci -v -s 04:00.0
04:00.0 Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)
        Subsystem: QLogic Corp. Device 015c
        Flags: bus master, fast devsel, latency 0, IRQ 98
        I/O ports at ec00 [size=256]
        Memory at df2fc000 (64-bit, non-prefetchable) [size=16K]
        Expansion ROM at df200000 [disabled] [size=256K]
        Capabilities: [44] Power Management version 3
        Capabilities: [4c] Express Endpoint, MSI 00
        Capabilities: [88] MSI: Enable- Count=1/32 Maskable- 64bit+
        Capabilities: [98] Vital Product Data
        Capabilities: [a0] MSI-X: Enable+ Count=2 Masked-
        Capabilities: [100] Advanced Error Reporting
        Capabilities: [138] Power Budgeting <?>
        Kernel driver in use: qla2xxx

        Kernel modules: qla2xxx


# modinfo qla2xxx | grep version
version:        8.03.07.15.05.09-k

Method 2

[test@linux:NODB /proc/scsi] 
systool -c scsi_host -v | grep 'driver_version'

Method 3   : 

#dmesg | grep -i hba

How to check the HBA driver version


Method 1: 


# systool -c scsi_host -v | grep -w fw_version

Method 2:

# dmesg | grep -i fw

How to check the wwn number 


# cat /sys/class/scsi_host/host1/device/fc_host:host1/port_name
# cat /sys/class/scsi_host/host*/device/fc_host/host*/node_name

[XXXX@XXXX ~]# modinfo qla2xxx | grep version
version:        8.03.01.04.05.05-k
srcversion:     6CD3A3C1F687EACB007F2CD

l2105 ~]# cat /sys/class/fc_host/host*/node_name
0x5001438028cbd4c5
0x5001438028cbd4c7
0x5001438028cbd2c5
0x5001438028cbd2c7

[root@l2105 ~]# lspci | grep -i hba

04:00.0 Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)
04:00.1 Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)
27:00.0 Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)
27:00.1 Fibre Channel: QLogic Corp. ISP2532-based 8Gb Fibre Channel to PCI Express HBA (rev 02)

@test ~]# modinfo qla2xxx | grep version
version:        8.07.00.26.06.8-k
srcversion:     50E0219D762F696CEE5A2D3
vermagic:       2.6.32-642.13.1.el6.x86_64 SMP mod_unload modversions

[root@l2105 ~]# lsmod
Module                  Size  Used by
iptable_filter          2793  0
ip_tables              17895  1 iptable_filter
sunrpc                267620  1
bonding               132885  0
ipv6                  336282  0
power_meter             9009  0
acpi_ipmi               3745  1 power_meter
ipmi_si                46440  2 acpi_ipmi
ipmi_msghandler        40044  2 acpi_ipmi,ipmi_si
iTCO_wdt                7278  0
iTCO_vendor_support     3056  1 iTCO_wdt
joydev                 10480  0
hpilo                   7821  8
hpwdt                   7062  0
sg                     29318  0
tg3                   163674  0
bnx2x                 727465  0
ptp                     9614  2 tg3,bnx2x
pps_core               10690  1 ptp
libcrc32c               1246  1 bnx2x
mdio                    4769  1 bnx2x
serio_raw               4626  0
lpc_ich                13571  0
mfd_core                1927  1 lpc_ich
shpchp                 29130  0
ext4                  379559  10
jbd2                   93252  1 ext4
mbcache                 8193  1 ext4
dm_round_robin          2525  4
sd_mod                 37158  8
crc_t10dif              1209  1 sd_mod

qla2xxx               502213  16

How to check LUN ID's in a server

Method 1:

# multipath -ll - Will give the LUN ID's

Method 2: Native Command

# scsi_id -g -s /block/sd<>

-g  - Treat the device as white listed. The -g option must be specified on the command line or in the scsi_id configuration file for scsi_id to generate any output
-s - Generate an id for the sysfs-device.  The sysfs mount point must not be included. For 
example, use /block/sd, not /sys/block/sd.

For each and every LUN there will be a drive letter managed by sd as the drives are managed by sd driver. which can found in /dev/disk/by-id/ - Folder will also have the scsi_id. Else we can get the drive letters by executing fdisk -l and grep for sd then give the sd device name in scsi id command to find the LUN ID. For Eg: if we get output as /dev/sdqp1 then with scsi_id execute scsi_id -g -s /block/sdqp

In the above folder we can four files with same id, that may be because of multipath. Even under /sys/block/sd four devices can have same id may be because of multipathing.





Friday, May 5, 2017

What is chattr command used for

chattr - Change attribute, changes the file attributes on a Linux file system


This is very useful to set attributes in system files like passwd and shadow files wherein user’s info are contains.

Syntax

# chattr [operator] [flags] [filename]

Options

a - file set with ‘a‘ attribute, can only be open in append mode for writing.
A - If a file is accessed with ‘A‘ attribute set, its atime record is not updated.
S - If a file is modified with ‘S‘ attribute set, the changes are updates synchronously on the disk.
i - file set with ‘i‘ attribute, cannot be modified (immutable). Means no renaming, no symbolic link creation, no execution, no writable, only superuser can unset the attribute.
j - file set with ‘j‘ attribute is set, all of its information updated to the ext3 journal before being updated to the file itself.
t - A file is set with ‘t‘ attribute, no tail-merging.
d - file set with ‘d‘, will no more candidate for backup when the dump process is run.
u - file set with ‘u‘ attribute is deleted, its data are saved. This enables the user to ask for its undeletion.

The letters 'aAcCdDeijsStTu' select the new attributes for  the  files: append only (a), no atime updates (A), compressed (c), no copy on write (C), no dump (d), synchronous directory updates (D), extent format (e), immutable  (i),  data journalling (j), secure deletion (s), synchronous updates (S), no tail-merging (t), top of directory hierarchy  (T),  and undeletable (u).

Operator

+ : Adds the attribute to the existing attribute of the files.
– : Removes the attribute to the existing attribute of the files.
= : Keep the existing attributes that the files have.

Note: The immutable bit +i can only be set by superuser (i.e root) user or a user with sudo privileges can able to set.

After setting immutable bit, let’s verify the attribute with command ‘lsattr‘.
# lsattr

The  operator  ‘+’  causes  the  selected attributes to be added to the existing attribs of the files; ‘-’ causes them to  be  removed;  and ‘=’ causes them to be the only attributes that the files have.

Thursday, May 4, 2017

LVM Management

What is LVM

If a system is partitioned with the ext3 file system, the hard drive is divided into partitions of defined sizes. If a partition becomes full, it is not easy to expand the size of the partition. Even if the partition is moved to another hard drive, the original hard drive space has to be reallocated as a different partition or not used

LVM Resize Actions

When shrinking, you should perform actions in this order
i) Resize the file system
ii) Resize the logical volume

When growing, you should perform actions in this order
i) Resize the logical volume

ii) Resize the file system

With the Help of LVM commands

a) To create LVM

To create a LVM volume we need to create Physical volume and volume group first. 


# pvcreate /dev/sda1 /dev/sdb1 /dev/sdc1
# vgcreate new_vol_group /dev/sda1 /dev/sdb1 /dev/sdc1
# lvcreate -L 2 G -n new_logical_volume new_vol_group
# mkfs.gfs2 -p lock_nolock -j 1 /dev/new_vol_group/new_logical_volume
# mount /dev/new_vol_group/new_logical_volume /mnt

b) To increase LVM size

# lvextend -L +100%FREE /dev/ubuntu-box-1-vg/root

# lvextend -L 100%FREE /dev/VolGroup00/lv_root

Note: When resizing a logical volume with the following command, it is try to resize it to the number of free extents, rather than the current size plus the number of free extents

Use the + symbol in front of X%FREE to indicate the space should be added to the current size.

# lvextend -L +100%FREE /dev/volgroup/logvo

lvresize -rL 30G /dev/vg_proxy/lv_root
lvresize -rL +5G /dev/vg_proxy/lv_root


which mitigates the need to resize in a particular order and drastically reduces the potential for erro

c.1) How to reduce LVM size

To Reduce, the LVM we need to first unmount the volume
# umount

Then execute resize2fs command with the final size what we want eg: from 100GB to 80GB then
# resize2fs filesystem <final size>

# After executing resize2fs command reduce the volume size, using lVM then mount it.
lvreduce -L 80G

Note :  Partition types : lvm - 8e, swap - 82, linux - 83

lvresize --resizefs --size SIZE /dev/vg/vg_data

d) How to remove a physical volume from a LVM

If there are enough free extents on the other physical volumes in the volume group, you can execute the pvmove command on the device you want to remove with no other options and the extents will be distributed to the other devices.

To check the usage of physical volumes
# pvs -o+pv_used

If free space is there, then use pvmove command to move the data from the volume
# pvmove /dev/sdb1

Then remove the physical volume from the volume group by using vgreduce command.
# vgreduce myvg /dev/sdb1

How to move data to a new HDD

There may be a case where 

First create physical volume with the new disk 
# pvcreate /dev/sdd1

Extend the Volume group with the new disk
# vgextend myvg /dev/sdd1

Now, move the data from OLD disk to NEW disk using pvmove command
# pvmove <Old Disk> <New Disk>
# pvmove /dev/sdb1 /dev/sdd1

Once if the data is copied, remove the old disk from volume group by using vgreduce command
# vgreduce myvg /dev/sdb1

How to LVM mirroring

Link http://www.tecmint.com/lvm-storage-migration/ Steps works fine except for one option.

We need some space for mirror logs while mirroring so --mirrorlog core/--alloc anywhere option can be used.

--mirrorlog core  à The log can be kept in memory so that no extra physical volume is needed to mirror the device
--alloc anywhere à the size of the mirror has to be smaller than the size of total physical volumes because the mirror log requires that free space.

@XXXXX~]# lvconvert -m 1 --mirrorlog core /dev/vgtest/lvtest /dev/sde
  vgtest/lvtest: Converted: 0.6%
  vgtest/lvtest: Converted: 8.7%
  vgtest/lvtest: Converted: 26.4%
  vgtest/lvtest: Converted: 63.9%
  vgtest/lvtest: Converted: 100.0%

@XXXXX~]# lvconvert -m 0 /dev/vgtest/lvtest /dev/sdd
  Logical volume lvtest converted.

@XXXXX~]# lvs -o +devices
  LV       VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert Devices
  lvvar    vgsystem -wi-ao----   2.48g                                                     /dev/sda2(384)
  lvvar    vgsystem -wi-ao----   2.48g                                                     /dev/sda2(896)
  lvtest   vgtest   -wi-ao----   5.00g                                                     /dev/sde(0)

@XXXXXtest]# df -h .
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vgtest-lvtest
                      4.8G  1.8G  2.8G  39% /test

Mirroring took hardly 3 mins for 2G of data.

2) Using dmsetup command

dmsetup is low level LVM command. Using dmsetup we can manage LVM's

UUID duplicate error


Figured it out. I was using kpartx for backups a few months ago and forgot to close the devices with kpartx -d. I discover using partprobe that the lvm volumes wich belong to virtual machines are "exported" to to the server,and fill /dev/mapper with duplicates lvms,so i used kpartx -d to all devices in /dev/mapper and situation return ok. lvs,pvs,vgs didn't give any error mess

uuidgen
tune2fs -U <output of uuidgen> /dev/sdb1
Or if you're confident uuidgen is going to work:


tune2fs -U `uuidgen` /dev/sdb1