17 July 2015

VMware ESXi command line

Following are some useful commands I'm using in daily management of VMware ESXi 5.5 servers.

Showing the ESXi Gateway alternate to "netstat -rn" in Linux: 

# esxcfg-route -l


Alternate to "arp" command in Linux:

# esxcli network ip neighbor list


List of all registered VMs:

 # vim-cmd vmsvc/getallvms


Restart the vpxa and ESXi host management agents:

# /etc/init.d/hostd restart
# /etc/init.d/vpxa restart


Restart all the mgmt services in the ESXi host:

# /sbin/services.sh restart


Restart VMware vCenter services:

Remote desktop to Windows vCenter server, in the services, restart "VMware VirtualCenter Server".
Same goes to restarting vCenter Inventory as well.


Alternate to "top" in Linux:

# esxtop 


Check ESXi version and update level:

#vmware -vl
   VMware ESXi 5.5.0 build-2068190
   VMware ESXi 5.5.0 Update 2


Listing the files that are opened and being used by VM:

# vmkvsitools lsof | grep vm_name 


Current state of a virtual machine:

# vim-cmd vmsvc/getallvms


Shutdown the VM using the VMID in previous step:

# vim-cmd vmsvc/power.getstate VMID
# vim-cmd vmsvc/power.shutdown VMID


Poweroff the VM in case it fails to shutdown:

# vim-cmd vmsvc/power.off VMID





3 July 2015

Add new Hard Disk to Linux LVM on VMware

If you decided to add or expand some space to your Linux server running on top of the VMware, here is how to do it by adding new hard disk to your VM.

VMware side:

1. In the vSphere client, right click on the desired VM and choose "Edit Settings"

2. In Hardware tab, click Add and choose "Hard Disk" and click next until new disk has been added to VM.


Within the Linux VM: 

1. Detecting new hard disk

We need to check if Linux sees new hard disk:

# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000da861

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       26109   209202176   8e  Linux LVM

Disk /dev/mapper/vg00-lv_root: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg00-lv_swap: 4160 MB, 4160749568 bytes
255 heads, 63 sectors/track, 505 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

In this case /dev/sdb has been detected at the end of the list.

We can confirm it by checking the logs:

# tail /var/log/messages 


If Linux cannot see new hard disk, we need to scan the disks:

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

and check the results in /var/log/messages.


2. After Linux can see the new hard disk, we need to partition it:

# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x8fddf252.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').



Press "p" to print current partition table, which is empty in this case:

Command (m for help): p

Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8fddf252

   Device Boot      Start         End      Blocks   Id  System



Press "n" to add new partition, "p" to make it primary and "1" as creating the first partition:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1


Choosing start and end disk cylinder for the size of the new partition, for me I accept the default of "1" for start and default of last cylinder to use the whole disk for this new partition:

First cylinder (1-26108, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-26108, default 26108):
Using default value 26108


Press "p" to see the new partition table:

Command (m for help): p

Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8fddf252

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       26108   209712478+  83  Linux


Our new partition has been created and its type is 83 under "Id" column. 

To make use of it as LVM disk, we will change its type to "8e" as Linux LVM.

Press "l" to see all partition types:

Command (m for help): l

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1


Press "t" to change the partition type and enter "8e" for LVM:

Command (m for help): t

Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)


Now we can see our new partition is "8e" LVM type:

Command (m for help): p

Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8fddf252

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       26108   209712478+  8e  Linux LVM


Now we are almost done with partition creation and good to write the changes to the disk with pressing "w":

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


After new partition has been created, we can check it:

# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000da861

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       26109   209202176   8e  Linux LVM

Disk /dev/mapper/vg00-lv_root: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg00-lv_swap: 4160 MB, 4160749568 bytes
255 heads, 63 sectors/track, 505 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8fddf252

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       26108   209712478+  8e  Linux LVM


Therefore, our new hard disk /dev/sdb has its own first partition of /dev/sdb1.


3. LVM side: Now we need to make our new partition to be a physical volume:

# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

And add new physical volume to an existing volume group or create new volume group. Here we will add it to an existing VG:

# vgextend vg00 /dev/sdb1
  Volume group "vg00" successfully extended


And current status of volume group after new pv has been added, we can see it at the end of the result:

# vgdisplay -v
    Finding all volume groups
    Finding volume group "vg00"
  --- Volume group ---
  VG Name               vg00
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  9
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                7
  Open LV               7
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               399.50 GiB
  PE Size               4.00 MiB
  Total PE              102273
  Alloc PE / Size       7136 / 27.88 GiB
  Free  PE / Size       95137 / 371.63 GiB
  VG UUID               Mgj9le-I3Z2-acFE-qwoL-IUNr-C051-rsG3X5

  --- Logical volume ---
  LV Path                /dev/vg00/lv_root
  LV Name                lv_root
  VG Name                vg00
  LV UUID                dQ68re-B5bN-AjBk-WTdb-O5rw-CpSe-0eH4Jj
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2013-07-23 10:28:39 +0200
  LV Status              available
  # open                 1
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/vg00/lv_swap
  LV Name                lv_swap
  VG Name                vg00
  LV UUID                yHKrBb-qDtj-Syj3-g7mv-uNiG-xeNa-bJ6dkq
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2013-07-23 10:28:43 +0200
  LV Status              available
  # open                 1
  LV Size                3.88 GiB
  Current LE             992
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Physical volumes ---
  PV Name               /dev/sda2
  PV UUID               xSYV0W-wOoS-xGze-vNDk-NX9w-sLbj-CLBwnn
  PV Status             allocatable
  Total PE / Free PE    51074 / 43938

  PV Name               /dev/sdb1
  PV UUID               wIC3vg-A0B3-eIGX-EJ6j-OSwO-7rhB-9jv1Ym
  PV Status             allocatable
  Total PE / Free PE    51199 / 51199


Now we are good to create new logical volume or extend an existing one. In my case, I'll create new LV in volume group "vg00" with name "lv_backup" and "51199" number of physical extents specified with "-l":

You can also specify LV size with "-L" which requires size in megabyte(M) or gigabyte(G).

# lvcreate vg00 -l 51199 -n lv_backup
  Logical volume "lv_backup" created

Current VG status after new LV:

# vgdisplay -v
    Finding all volume groups
    Finding volume group "vg00"
  --- Volume group ---
  VG Name               vg00
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  10
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                8
  Open LV               7
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               399.50 GiB
  PE Size               4.00 MiB
  Total PE              102273
  Alloc PE / Size       58335 / 227.87 GiB
  Free  PE / Size       43938 / 171.63 GiB
  VG UUID               Mgj9le-I3Z2-acFE-qwoL-IUNr-C051-rsG3X5

  --- Logical volume ---
  LV Path                /dev/vg00/lv_root
  LV Name                lv_root
  VG Name                vg00
  LV UUID                dQ68re-B5bN-AjBk-WTdb-O5rw-CpSe-0eH4Jj
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2013-07-23 10:28:39 +0200
  LV Status              available
  # open                 1
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/vg00/lv_swap
  LV Name                lv_swap
  VG Name                vg00
  LV UUID                yHKrBb-qDtj-Syj3-g7mv-uNiG-xeNa-bJ6dkq
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2013-07-23 10:28:43 +0200
  LV Status              available
  # open                 1
  LV Size                3.88 GiB
  Current LE             992
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/vg00/lv_backup
  LV Name                lv_backup
  VG Name                vg00
  LV UUID                OF34Lm-AOmq-l3yu-LxNV-XB3K-kXxy-6hE1hi
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2013-07-23 10:58:30 +0200
  LV Status              available
  # open                 0
  LV Size                200.00 GiB
  Current LE             51199
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:7

  --- Physical volumes ---
  PV Name               /dev/sda2
  PV UUID               xSYV0W-wOoS-xGze-vNDk-NX9w-sLbj-CLBwnn
  PV Status             allocatable
  Total PE / Free PE    51074 / 43938

  PV Name               /dev/sdb1
  PV UUID               wIC3vg-A0B3-eIGX-EJ6j-OSwO-7rhB-9jv1Ym
  PV Status             allocatable
  Total PE / Free PE    51199 / 0


Now it's time to create file system for our new logical volume "lv_backup". I'll create ext4 type file system:

# mkfs.ext4 /dev/vg00/lv_backup
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
13107200 inodes, 52427776 blocks
2621388 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
1600 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

And we are good to mount our new LV and use it. I'll create /backup folder and mount the LV to it:

# mkdir /backup

# mount /dev/vg00/lv_backup /backup/

# df -h
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vg00-lv_root    4.0G  357M  3.4G  10% /
tmpfs                       1.9G     0  1.9G   0% /dev/shm
/dev/sda1                   485M   39M  421M   9% /boot
/dev/mapper/vg00-lv_backup  197G  188M  187G   1% /backup

Congratulations! Our new LV is seeing by Linux and ready to use.

To make it auto mounted during boot, we will add an corresponding entry in /etc/fstab file.
Last line has been added to the file:

# vim /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Jul 23 10:29:06 2013
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg00-lv_root /                       ext4    defaults        1 1
UUID=11b5567d-6f29-49b7-9ec7-27f6b3d56f27 /boot                   ext4    defaults        1 2       1 2
/dev/mapper/vg00-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/vg00/lv_backup     /backup                 ext4    defaults        1 2

And there you have it.