Translate

Archives

Adding and Removing Disks From VMware RHEL7 Guests Without Rebooting

Consider the following scenario. You are studying for your RHCSA or RHCE using one or more RHEL or CentOS VMs running in a VMware Workstation on a Microsoft Windows OS. You need to practice adding one or more disks (it will come up in the exam!), formatting and partitioning the disks many times so that you can do it accurately, quickly and without hesitation in the actual exam where time is of the essence.

In this scenario, most people simply shutdown and power off the guest VM, go into the VM settings, add a new disk, power on the VM where upon the new disk is automatically detected and made available to the OS during the boot process when the hardware is scanned and enumerated. Similarly, when removing a disk from the OS. However, this wastes valuable (usually scarce) study time and can easily be avoided as this post will show you.

Adding a new disk to your VM

Suppose you currently have the following configuration – which is the default OOTB (Out Of The Box) single disk configuration for CentOS 7.2:

and you want to add a new disk to the current single disk configuration without shutting down your CentOS 7 guest.

Two utilities you should be very familiar with when examining the current disk configuration as seen from the OS perspective are:

  • lsblk – list block devices
  • lsscsi – list SCSI devices (hosts) and their attributes

We will use both utilities extensively in the remainder of this post.

The next image shows one of the VMware Workstation Add Hardware Wizard panes. Note that the default disk type is SCSI – not SATA or IDE (PATA). The disk is also persistent. Note also that the VM is running. Leave the disk type as SCSI, do not select either IDE (which does require a reboot) or SATA.

OK, you have created your new SCSI disk in VMware Workstation. Does this new disk automatically show up in your running VM?

No, it does not. As shown above, neither lsscsi or lsblk detect the newly added disk. We have to use sysfs to tell the kernel to rescan a bus (AKA channel in SCSI terminology) in order for the kernel to discover the new disk.

What is sysfs you may ask. Fortunately, it is not something that you need to know much about for the RHCSA or RHCE exams. From Wikipedia:

sysfs is a pseudo file system provided by the Linux kernel that exports information about various kernel subsystems, hardware devices, and associated device drivers from the kernel’s device model to user space through virtual files.[1] In addition to providing information about various devices and kernel subsystems, exported virtual files are also used for their configuring.

Consider the following commands:

# lsscsi
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR10 1.00  /dev/sr0 
[2:0:0:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sda 

# ls -1 /sys/class/scsi_host
host0
host1
host2

# grep mpt /sys/class/scsi_host/host?/proc_name
/sys/class/scsi_host/host2/proc_name:mptspi

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

# lsscsi
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR10 1.00  /dev/sr0 
[2:0:0:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sda 
[2:0:1:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sdb 


The first command lists all the SCSi devices the OS is aware of. The 4 numbers in the square brackets can be represented as [ <H> <C> <T> <L> } where <H> is the host adapter (0 being the first), <C> the channel (bus) on the host adapter (0 being the first), <T> is the target ID, and <L> is the Logical Unit Number (0 being the first.) If you are unfamiliar with SCSI (Small Computer System Interface) concepts, I suggest you read this Wikipedia article – especially the part about device identification.

The second command lists out all the host adapters. In this case, it lists host0, host1 and host2. This is useful information but it is not sufficient information information to determine if a specific host adapter is connected to a specific hard drive.

The third command is used to detect which specific host adapter is handling our new VMware Workstation virtual disk. How does this command work? It works because the default disk controller for a VMware Workstation guest virtual disk is an LSI Logic disk controller. The name of the device driver for a LSI Logic SCSI disk controller in Linux is mptspi. Technically, the full name of the driver is LSI Logic Fusion MPT SPI Host. In the case of another SCSI controller type the name of the driver will differ. For example, If for some reason you used the VMware para-virtual controller, you would need to grep for pvscsi.

Now that we know which host adapter our disk is attached to, we issue the fourth command which causes the kernel to rescan all controllers on that particular host adapter, i.e. host2, What is meant by echo “- – -“? Here, the three values “- – -” represent channel(bus), SCSI target ID, and LUN. The dashes act as wildcards meaning “rescan everything.”

Finally, the last command confirms the disk was found by re-runnning lsscsi.

To explicitly add a single disk, there is a preferred method and a depreciated method per Red Hat documentation. The preferred method uses the echo “<C> <T> <L>” > /sys/class/scsi_host/host<H>/scan syntax as shown in the following example:

# lsscsii
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR10 1.00  /dev/sr0 
[2:0:0:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sda 

# echo "0 1 0" > /sys/class/scsi_host/host2/scan

# lsscsi
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR10 1.00  /dev/sr0 
[2:0:0:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sda 
[2:0:1:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sdb 

The depreciated method uses the echo “scsi add-single-device <H><C> <T> <L>” > /proc/scsi/scsi syntax as shown in the following example:

# lsscsii
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR10 1.00  /dev/sr0 
[2:0:0:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sda 

# echo "scsi add-single-device 2 0 1 0" > /proc/scsi/scsi

# lsscsi
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR10 1.00  /dev/sr0 
[2:0:0:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sda 
[2:0:1:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sdb 

The last option that I will discuss is to use a small shell script, such as the one shown below, to discover and rescan all channels on all hosts:

#!/bin/bash

ls -1 /sys/class/scsi_host |
while read SCSI_HOST 
do
   echo "- - -" > /sys/class/scsi_host/${SCSI_HOST}/scan
done 


This script simply discovers and rescans all channels on all the host adapters.

By the way, the Red Hat storage documentation and a Red Hat Knowledgebase article both recommend that you install the sg3_utils package (Linux SCSI Utilities) to rescan SCSI layers using the rescan-scsi-bus.sh script in order to detect when a new virtual disk is added to the guest operating system. Do not bother using this package. It will simply waste you valuable study time and furthermore is buggy.

Deleting an Existing VM Disk

Suppose you want to delete /dev/sdb. Assuming it is not your primary disk and you have already umounted any filesystems that were using this disk, consider the following set of commands:

# lsscsi
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR10 1.00  /dev/sr0 
[2:0:0:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sda 
[2:0:1:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sdb 

# ls -l /sys/bus/scsi/drivers/sd/2\:0\:1\:0/block/
total 0
drwxr-xr-x. 7 root root 0 May  7 06:46 sdb

# cat /proc/scsi/scsi
Attached devices:
Host: scsi2 Channel: 00 Id: 00 Lun: 00
  Vendor: VMware,  Model: VMware Virtual S Rev: 1.0 
  Type:   Direct-Access                    ANSI  SCSI revision: 02
Host: scsi1 Channel: 00 Id: 00 Lun: 00
  Vendor: NECVMWar Model: VMware IDE CDR10 Rev: 1.00
  Type:   CD-ROM                           ANSI  SCSI revision: 05
Host: scsi2 Channel: 00 Id: 01 Lun: 00
  Vendor: VMware,  Model: VMware Virtual S Rev: 1.0 
  Type:   Direct-Access                    ANSI  SCSI revision: 02

# echo 1 > /sys/bus/scsi/drivers/sd/2:0:1:0/delete

# lsscsi
[1:0:0:0]    cd/dvd  NECVMWar VMware IDE CDR10 1.00  /dev/sr0 
[2:0:0:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sda 

# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 19.5G  0 part 
  ├─centos-root 253:0    0 17.5G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sr0              11:0    1 1024M  0 rom  

The first command lists the SCSI devices currently known to the OS and allows you to identify the the host adopter, channel, target and LUN that /dev/sdb is using. The second and third commands enable you to double check things via a more verbose listing (always a good idea!) The fourth command removes the device /dev/sdb from the operating system. The fifth and sixth commands confirm the device was removed.

Good luck studying for your RHCSA or RHCE exam!

2 comments to Adding and Removing Disks From VMware RHEL7 Guests Without Rebooting

  • PaweÅ‚

    There is a script rescan-scsi-bus.sh in sg3_utils package.
    You can add device with rescan-scsi-bus.sh -a and remove device with rescan-scsi-bus.sh -r