Linux: Rescan for new and changes of devices

Hi,

with virtualization it simply possible to change systems within a few mouse clicks. For example adding new disks or increasing the disk size of a hard disk without a reboot.

When you do such changes you have to tell linux that something has been changed.
To scan for new disks, determine the host bus adapter to which the disk is connected

root@debdev # ls -l /sys/class/scsi_host/host*/proc_name
-r--r--r-- 1 root root 4096 Oct 17 23:05 /sys/class/scsi_host/host0/proc_name
-r--r--r-- 1 root root 4096 Oct 17 22:48 /sys/class/scsi_host/host1/proc_name
-r--r--r-- 1 root root 4096 Oct 17 23:05 /sys/class/scsi_host/host2/proc_name
root@debdev # cat /sys/class/scsi_host/host*/proc_name
ata_piix
ata_piix
vmw_pvscsi

And rescan the bus i.e. vmw_pvscsi

root@debdev # echo "- - -" > /sys/class/scsi_host/host2/scan

Let us assume you have changed the disk size of the harddisk /dev/sdb and /dev/sdb1 is the only partition at this disk.
Determine the sysfs path

root@debdev # ls -l /sys/class/scsi_device/*/device/block
/sys/class/scsi_device/1:0:0:0/device/block:
total 0
drwxr-xr-x 7 root root 0 Oct 17 18:30 sr0

/sys/class/scsi_device/2:0:0:0/device/block:
total 0
drwxr-xr-x 10 root root 0 Oct 17 18:30 sda

/sys/class/scsi_device/2:0:1:0/device/block:
total 0
drwxr-xr-x 8 root root 0 Oct 17 18:30 sdb
root@debdev # cat /sys/class/scsi_device/*/device/model
VMware IDE CDR10
Virtual disk
Virtual disk

To scan for disk changes at disk /dev/sdb

root@debdev # echo 1 > /sys/class/scsi_device/2:0:1:0/device/rescan

Check if the change is recognized by the kernel

root@debdev # dmesg|tail -n 10
ata2: soft resetting link
ata2.00: configured for UDMA/33
ata2: EH complete
sd 2:0:0:0: [sda] Cache data unavailable
sd 2:0:0:0: [sda] Assuming drive cache: write through
sd 2:0:1:0: [sdb] 419430400 512-byte logical blocks: (214 GB/200 GiB)
sd 2:0:1:0: [sdb] Cache data unavailable
sd 2:0:1:0: [sdb] Assuming drive cache: write through
sdb: detected capacity change from 107374182400 to 214748364800

Additionally , resize the filesystem to the maximum size. 2 steps necessary. Increase the partition and then the filesystem. Be sure your backup is up to date!
Delete (YES DELETE, you just edit the partition table. That is no problem if you do not call mkfs.ext4)the current partition and create a new with the maximum size. Get the old partition info by fdisk especially the start block of the partition.

root@debdev # fdisk -l
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: 0x1649ccd6

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       13054   104856223+  83  Linux

Delete Partition 1 (command d) and create a new primary partition (command n) at the same start block(cylinder). In this example Block 1

root@debdev # umount /deb/sdb1
root@debdev # fdisk /dev/sdb
Command (m for help): d
Selected partition 1
Command (m for help): n
Partition number (1-4): 1
First cylinder (1-26108, default 1): 1
Last cylinder:
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: 0x1649ccd6

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

Command (m for help): w
The partition table has been altered!
root@debdev # partx /dev/sdb

Or use growpart. This command grows partition 1 (if it is the last partition on the disk) to the maximum size

root@debdev # growpart /dev/sda 1

Check volume integrity and resize the filesystem.

root@debdev # e2fsck -f /dev/sdb1
root@debdev # resize2fs -p /dev/sdb1

Michael

Advertisment to support michlstechblog.info

2 thoughts on “Linux: Rescan for new and changes of devices”

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.