Saturday, February 13, 2016

Linux | Don't reboot after adding a new disk on a VM

It is quite common practice when adding a new drive to a Linux system to reboot the system afterwards so that the server can re-scan its hardware and detect the change. However, every now and then we get to make changes to a server that we don't want to reboot it for some reason. Or maybe you are lazy, like me and don't want to wait 1 min to reboot. In any case, bellow is a way to make your system re-scan the hardware and detect the changes.


TL;DR
If you are in a hurry and don't want to read all than you can use:
# echo "- - -" > /sys/class/scsi_host/host0/scan

... in order to re-scan the SCSI controller and detect the newly added disk.

If you don't know the location of the new disk, than you can also use
ls /sys/class/scsi_host/ | while read host ; do echo "- - -" > /sys/class/scsi_host/$host/scan ; done

... to re-scan all of the SCSI controllers


If you want to learn more than continue on with the tutorial.


In this example I assume that you are working on a VM which has SCSI adapters.

If you take a look into /sys/class/scsi_host you can find sim-links pointing to all of the SCSI adapters available (including those which are not yet installed). From the output bellow, we can see that this particular VM can support up to 32 SCSI adapters, and each SCSI adapter can support up to 7 disks.


# ls -l /sys/class/scsi_host/
total 0
lrwxrwxrwx 1 root root 0 Feb 13 21:09 host0 -> ../../devices/pci0000:00/0000:00:10.0/host0/scsi_host/host0
lrwxrwxrwx 1 root root 0 Feb 13 21:09 host1 -> ../../devices/pci0000:00/0000:00:07.1/ata1/host1/scsi_host/host1
lrwxrwxrwx 1 root root 0 Feb 13 21:09 host10 -> ../../devices/pci0000:00/0000:00:11.0/0000:02:05.0/ata10/host10/scsi_host/host10
lrwxrwxrwx 1 root root 0 Feb 13 21:09 host11 -> ../../devices/pci0000:00/0000:00:11.0/0000:02:05.0/ata11/host11/scsi_host/host11

...
Output omited
...

lrwxrwxrwx 1 root root 0 Feb 13 21:09 host31 -> ../../devices/pci0000:00/0000:00:11.0/0000:02:05.0/ata31/host31/scsi_host/host31
lrwxrwxrwx 1 root root 0 Feb 13 21:09 host32 -> ../../devices/pci0000:00/0000:00:11.0/0000:02:05.0/ata32/host32/scsi_host/host32


And so on up to ... ... /host32/scan

If your server is a VM then there is a high probability that the new disk will be attached to host0. If you have access to the hypervisor or if you use software hypervisor like VMware Workstation or a VirtualBox than you can see to which SCSI adapter the disk is attached by looking at the hardware options for the VM.

In the below example I've looked at the 4th disk Advanced options and I can see that it is attached to the SCSI0 controller on its 3rd connector.

Note: This VM is still ON, as that is the reason why I can't change the disk location.






















Now, lets continue on further. I still haven't added a new disk as I was giving the explanation.
Lets see what we currently have on our VM:

# fdisk -l

Disk /dev/sda: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c481a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    33554431    16251904   83  Linux

Disk /dev/sdb: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000eeba8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    33554431    16776192   83  Linux

Disk /dev/sdc: 34.4 GB, 34359738368 bytes, 67108864 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00098f97

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    67108863    33553408   83  Linux

Disk /dev/sdd: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000782c3

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048     8194047     4096000   82  Linux swap / Solaris

Disk /dev/sde: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x770ab37c

   Device Boot      Start         End      Blocks   Id  System
/dev/sde1            2048    33554431    16776192   83  Linux


We have 5 disks in total:

/dev/sda
/dev/sdb
/dev/sdc
/dev/sdd
/dev/sde

Using the Workstation Console, I will add a new disk.

In order to force the system to scan those periferials we can make use of the command:
# echo "- - -" > /sys/class/scsi_host/host0/scan

And after that we will spot the new disk /dev/sdf with the fdisk command:

# fdisk -l

Disk /dev/sda: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c481a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    33554431    16251904   83  Linux

...
Some output omited
...

Disk /dev/sde: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x770ab37c

   Device Boot      Start         End      Blocks   Id  System
/dev/sde1            2048    33554431    16776192   83  Linux

Disk /dev/sdf: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


If you don't have access to the Hypervisor then you can rescan all of the SCSI adapters using this simple script:
ls /sys/class/scsi_host/ | while read host ; do echo "- - -" > /sys/class/scsi_host/$host/scan ; done



No comments:

Post a Comment