Search This Blog

Extend a Linux LVM Volume on a VM - Part 2

This is the part 2 of extending a Linux LVM volume. See part 1 when the entire disk as a PV.

Extend a LVM when the partition as a PV

  1. Increase the VM’s hard disk size in vSphere Client

    • If there is a VM snapshot on the disk, its size cannot be changed. Remove all the snapshots first
    • After increasing the disk size, take a snapshot as the backup
  2. Rescan the SCSI bus to verify the OS see the new space on the disk
    • ls /sys/class/scsi_host/
    • echo “- - -“ > /sys/class/scsci_host/<host_name>/scan
    • tail -f /var/log/message
    • or
    • ls /sys/class/scsi_disk/
    • each ‘1’ > /sys/class/scsi_disk/<0\:0\:0\:0>/device/rescan
    • tail -f /var/log/message
    • fdisk -l
  3. Prepare the disk partition
    • fdisk -l
    • fdisk </dev/sdb>
    • p - print the partition table, note the next available partition number
    • n - add a new parition
    • p - primary partition
    • <X> - partition number, enter the next available partition number from the previous p - print the partition table command 
    • default - the begining of the free cylinder
    • default - the last of the free cylinder
    • t - change a partition’s system id
    • <X> - partition number, enter the next available partition number from the previous p - print the partition table command
    • 8e - Linux LVM
    • w - write table to disk and exit
    • fdisk -l to verify the new partition
  4. Update partition table changes to kernel
    • reboot
    • or partprobe </dev/sdb>
    • Update (04/18/2016): In RHEL 6, partprobe will only trigger the OS to update the partitions on a disk that none of its partitions are in use (e.g. mounted). If any partition on a disk is in use, partprobe will not trigger the OS to update partition in the system because it it considered unsafe in some situations. So a reboot is required. see “How to use a new partition in RHEL6 without reboot?”
  5. Initializ the disk partition
    • pvcreate </dev/sdb3>
  6. Extend the VG
    • use vgdispaly to determine the volume group name
    • vgextend <volume_group_name> </dev/sdb3>
    • vgs
  7. Extend the LV
    • lvextend -l +100%FREE</dev/volume_group_name>/<logical_volume_name>
    • or lvextend -L+<size> /dev/<volume_group_name>/<logical_volume_name>
    • lvs
  8. Resize the file system
    • resize2fs /dev/<volume_group_name>/<logical_volume_name>
    • df -h
  9. Remove the VM snapshot once confirming the data intact

Extend a Linux LVM Volume on a VM - Part 1

As I mentioned in the recent Linux LVM post, there are two ways to prepare the physical volume (PV)

  • the entire disk as a PV (not recommended)
  • or creating a partition on the disk and the partition as a PV.

The step to extend a LVM volume are different on these two configuration.

Extend a LVM when the entire disk as a PV

  1. Increase the VM’s hard disk size in vSphere Client
    • If there is a VM snapshot on the disk, its size cannot be changed. Remove all the snapshots first
    • After increasing the disk size, take a snapshot as the backup
  2. Rescan the SCSI bus to verify the OS see the new space on the disk
    • ls /sys/class/scsi_host/
    • echo “- - -“ > /sys/class/scsci_host/<host_name>/scan
    • tail -f /var/log/message
    • or
    • ls /sys/class/scsi_disk/
    • each ‘1’ > /sys/class/scsi_disk/<0\:0\:0\:0>/device/rescan
    • tail -f /var/log/message
    • fdisk -l
  3. Resize the PV
    • pvs
    • pvresize </dev/sdb>
    • image
  4. Verify the VG automatically sees the new space
    • vgs
    • image
    • compare the vg_app VFree size in this screen with the one in step #3
  5. Extend the LV
    • lvextend -l +100%FREE </dev/volume_group_name>/<logical_volume_name>
    • or lvextend -L+<size> /dev/<volume_group_name>/<logical_volume_name>
    • lvs
    • image
  6. Resize the file system
    • resize2fs /dev/<volume_group_name>/<logical_volume_name>
    • df -h
    • image
  7. Remove the VM snapshot once confirming the data intact

Linux Logical Volume Management (LVM) and Setup

LVM Layout

LVM Logical Volume Components

(source: RedHat Logical Volume Manager Administration)

LVM Components (from bottom to top)

  • Hard Disks
  • Partitions
    • LVM will work fine with the entire disk (without creating a partition) as a PV. But this is not recommended.
    • Other OS or disk utility (e.g. fdisk) will not recognize the LVM metadata and display the disk as free, so the disk is likely being overwritten by mistake.
    • The best pratice is to create a partition on the hard disk, then initialize the partition as a PV.
    • It is generally recommended that creating a single partition that covers the whole disk. (see RedHat Logical Volumen Manager Administration)
    • Using an entire disk a PV or using a partition as a PV will have a different procedure when growing the hard disk size in the VM (see “Expanding LVM Storage”)
  • Physical Volumnes
  • Volume Group
  • Logical Volumes
  • File Systems

LVM Setup

  1. Add a new hard disk
  2. Rescan the SCSI bus
    • ls /sys/class/scsi_host/
    • echo “- - -“ > /sys/class/scsci_host/<host_name>/scan
    • tail -f /var/log/message 
    • or
    • ls /sys/class/scsi_disk/
    • each ‘1’ > /sys/class/scsi_disk/<0\:0\:0\:0>/device/rescan
    • tail -f /var/log/message
  3. Prepare the disk partition
    • fdisk -l
    • fdisk </dev/sdb>
    • n - add a new parition
    • p - primary partition
    • 1 - partition number
    • default - first cylinder
    • default - last cylinder
    • t - change a partition’s system id
    • 1 - partition number
    • 8e - Linux LVM
    • w - write table to disk and exit
    • fdisk -l to verify the new partition
  4. Update partition table changes to kernel
    • reboot
    • or partprobe </dev/sdb>
    • Update (04/18/2016): In RHEL 6, partprobe will only trigger the OS to update the partitions on a disk that none of its partitions are in use (e.g. mounted). If any partition on a disk is in use, partprobe will not trigger the OS to update partition in the system because it it considered unsafe in some situations. So a reboot is required. see “How to use a new partition in RHEL6 without reboot?”
  5. Initialize disks or disk partitions
    • pvcreate </dev/sdb> - skip step #3, use the entire disk as a PV, not recommended
    • pvcreate </dev/sdb1> - use the partition created in step #3 as a PV, best practice
    • pvdisplay
    • pvs
  6. Create a volume group
    • vgcreate <volume_group_name> </dev/sdb1>
    • vgdisplay
    • vgs
  7. Create a logical volume
    • lvcreate --name <logical_volume_name> --size <size> <volume_group_name>
    • or lvcreate -n <logical_volume_name> -L <size> <volume_group_name>
    • lvdisplay
    • lvs
  8. Create the file system on the logical volume
    • mkfs.ext4 /dev/<volume_group_name>/<logical_volume_name>
  9. Mount the new volume
    • mkdir </mount_point>
    • mount /dev/<volume_group_name>/<logical_volume_name> </mount_point>
  10. Add the new mount point in /etc/fstab
    • vi /etc/fstab
    • /dev/<volume_group_name>/<logical_volume_name> </mount_point> ext4 defaults 0 0

Robocopy Command Examples

In my previous post, I summarize some key information about Robocopy. In this post, I will share some Robocopy command examples that I have used in the file migration projects. Even these examples have been successfully used on the production environment, please verify yourself before use. That is why I add the /L option on all the examples. Save the following to a batch file and pick the command by remove the rem at the beginning of the line.

@echo off

set LOG_FILE="C:\Temp\ROBOCOPY_LOG.TXT"
set SOURCE="Source Path"
set DEST="Destination Path"

rem Remove rem on the command line you want to run
rem Only one Robocopy command line is used at a time
rem Remove /L to apply the change after confirm the outcome in the log file

rem *************************************************
rem /PURGE option
rem Mirror the source to the destination - the newer file in the destination will be overwritten, the extra file in the destination will be purged
rem robocopy %SOURCE% %DEST% /E /COPYALL /PURGE /MT /R:1 /W:1 /V /NP /TEE /LOG+:%LOG_FILE% /L

rem /ZB option
rem Use /ZB option when file cannot be copied
rem robocopy %SOURCE% %DEST% /E /ZB /COPYALL /PURGE /MT /R:1 /W:1 /V /NP /TEE /LOG+:%LOG_FILE% /L

rem *************************************************
rem default option - changed, newer, and older file is copied; extra file is not impacted
rem Copy the source to the destination - the newer file in the destination will be overwritten, but keep the extra file in the destination
rem robocopy %SOURCE% %DEST% /E /COPYALL /MT /R:1 /W:1 /V /NP /TEE /LOG+:%LOG_FILE% /L

rem *************************************************
rem /XO option
rem Copy the source to the destination - keep the newer file and extra file in the destination
rem robocopy %SOURCE% %DEST% /E /COPYALL /MT /R:1 /W:1 /V /NP /TEE /LOG+:%LOG_FILE% /XO /L

rem *************************************************
rem /FFT option
rem Copy the source to the destination - when the third-party storage system is used
rem robocopy %SOURCE% %DEST% /E /COPYALL /MT /R:1 /W:1 /V /NP /TEE /LOG+:%LOG_FILE% /FFT /L

Robocopy Usage Summary

Robocopy is a feature-rich and powerful file migration tool in Windows. The built-in help system (robocopy /?) provides the basic usage of each option, but it doesn’t thoroughly explain why and how to use these options.

Recently I discovered a best Robocopy guide (robocopy.doc) in Windows Server 2003 Resource Kit Tools. To access this document without installing, use 7-Zip to extract the rktools.exe file, then extract the rktools.msi file. Even this document is for Version XP010, it is still applicable to the version built-in Windows 10.

I summarize some key information from this Robocopy document along with my usage experience below. It is highly recommended to read the full document if you have time.

What does Newer, Orlder or Extra mean?

File Class

In Source

In Destination

Source/Dest File Time

Source/Dest File Size

Source/Dest Attribute

Lonely

Yes

No

n/a

n/a

n/a

Tweaked

Yes

Yes

Equal

Equal

Different

Same

Yes

Yes

Equal

Equal

Equal

Changed

Yes

Yes

Equal

Different

n/a

Newer

Yes

Yes

Source > Dest

n/a

n/a

Older

Yes

Yes

Source < Dest

n/a

n/a

Extra

No

Yes

n/a

n/a

n/a

Mismatched

Yes (file)

Yes (Directory)

n/a

n/a

n/a

  • By default, Changed, Newer, and Older files are candidates for copying (subject to further filtering, as described later)
  • Same files are not copied
  • Extra and Mismatched files and directories are only reported in the output log
  • Tweaked files are neither identified nor copied - they are usually identified as Same files by default. Only when /IT is used, then Tweaked files will be copied

Use /L option to test Robocopy command

After building a Robocopy command with the options I want, I append /L option at the end of the command.

/L option lists files without copying, deleting, or applying a time stamp to any files.

Running the Robocopy command with the /L option, I can analyze the output to confirm the outcome before the real execution.

Should I use /Z, /B, or /ZB option?

  • /B – copy all files in Backup Mode. Enable you to copy some files as a Backup Operator that you would not be able to copy as a normal user. Backup mode copies are not restartable.
  • /ZB – try to copy files in Restartable Mode (for greater resiliency), and automatically switch to Backup Mode if the Restartable copy fails with an “Access Denied” error.
  • /Z, /B, or /ZB restart from close to the point of failure rather than the beginning of the files. This can decrease Robocopy performance and throughput significantly, as these options involve extra overhead. These options are only recommended when experience indicates you really need them.

My comment: not to use them unless you are not able to copy a file or the network connectivity is unstable.

Robocopy reports the file timestamp is different, even they look the same in Windows Explorer.

This usually happens when copying files between a Windows NTFS volume and a third-party storage system.

FAT file system stores its timestamp for files (modified date, etc) with 2-second granularity

NTFS file system stores its timestamp for files in a 100-nanosecond granularity

File-time granularity is 100 nanoseconds on NTFS, and two seconds on FAT. So copying a file from NTFS to FAT causes file times to be rounded to a value that file FAT file system can manage. When both the source and destination volumes are NTFS, file times are compared exactly. When either the source or destination volume is not an NTFS volume, Robocopy considers file times to be identical if they are within two seconds of each other.

Some third-party operating systems declare that their volumes are NTFS, but only implement FAT-style file times with a 2-second granularity. When copying to such a destination from a true NTFS volume, file time rounding may occur, along with unnecessary copying of file data in subsequence jobs. In this situation, use /FFT (FAT File Time) to tell Robocopy to consider file times to be identical if they are within 2 seconds of each other, even though both source and destination appear to be NTFS. This eliminates unnecessary copying in this scenario.

/FFT is useful for copying to third-party storage systems (when copying from Windows-to-Windows NTFS storage, this option is not needed) that declare a volume to be NTFS but only implement file time with a 2-second granularity.

/FFT forces Robocopy to use FAT file-time granularity so that the utility uses a 2-second standard for comparing files. As long as files are time-stamped within a 2-second interval, Robocopy considers them identical and doesn’t copy them. (Source: WindowsITPro Robocopy FAQ)

Two-way file change synchronization

/XO excludes “Older” files.

To synchronize two directory trees so that they can be updated simultaneously in separate areas (ensure that the latest files are present in both directory trees). First copy with /XO in one direction, and then in the other.

Exclude File or Directory by name

  • /XF file [file] excludes Files matching given names/paths/wildcards
  • /XD dirs [dirs] excludes Directories matching given names/paths
    • My comment: to exclude multiple directories, simply list them with spaces in between. If the path contains spaces, wrap it in double-quotes (“ ”). The most important thing is DO NOT EVER include a trailing “\” in the excluded paths.
  • /XF and /XD exclude files and directories before all other processing

Refresh security information without copying file data

  • /IS /COPY:SOU refreshes all security information for all selected files, without copying any file data

Directory fragementation for large directories

/CREATE – Robocopy creates only the directory tree and zero-length files as placeholders for the actual files. No file data is copied.

By default (without /CREATE), for the first time copy, the directory entry for each file is created first, and then the file data is copied. If the directory tree contains large directories, however, this sequence can cause fragmentation of the directory structure.

Minimize directory fragmentation for large directories

Run Robocopy with /CREATE (along with whatever the options supposed to be) first, then run the same command again without /CREATE to copy the file data to the destination.

When using this technique, the zero-length files created by the initial /CREATE command are tagged as “Changed” files on the second run.

My comment: The Robocopy version XP010 document states the files are tagged as “Older” files on the second run. When I tested on Windows 7 Pro, it shows “Changed” instead. The “Changed” matches the description in the File Classes for Changed - Source & Destination file times are equal, but file sizes are different.

Install hotfix for Windows 7 and Server 2008 R2 build-in Robocopy

Install the hotfix in KB2639043 to fix the Discretionary Access Control Lists (DACLs) for files issue.

Fix “Failed to install the hcmon driver” Error on Windows 10 When Installing VMware Remote Console

  • Launch Windows PowerShell as Administrator
  • Change directory to the folder where the VMware-VMRC-xxx.msi is located
  • Execute .\VMware-VMRC-xxx.msi

VM Hard Disk’s VMDK Files

Each hard disk of a VM consists of two .vmdk files:

  • one is a text file (descriptor file) containing descriptive data about the virtual hard disk; the name of the file is myvm.vmdk
  • the second is the actual content of the disk; the name of the file is myvm-flat.vmdk

Normally when browsing the datastore in the vSphere Web or C# Client, only the first .vmdk file is showed, and the size of the file is showed the total of both .vmdk files. I used to mislead by this and thought only one .vmdk for each VM hard disk.

VMDK.1
only one .vmdk file is showed, and note its size and type

But when SSH to the ESXi host and list the content in the VM folder, both .vmdk files are showed.

VMDK.2

If you manually move the first .vmdk file to another folder using the vSphere Client (when the VM is powered off), the –flat.vmdk file will show in the datastore when browsing in the vSphere Client.

VMDK.3
moving the .vmdk file to a different folder

VMDK.4
after moving, the .vmdk file is at the new location, but its size and type are changed

VMDK.5
after moving, the –flat.vmdk file is showed at the original location. note its size and type

Use WinSCP to Transfer Files in vCSA 6.7

This is a quick update on my previous post “ Use WinSCP to Transfer Files in vCSA 6.5 ”. When I try the same SFTP server setting in vCSA 6.7...