As a system administrator, I often need to use additional hard drives to expand storage space or separate system data from user data. I will show you how to add a hard drive from a host to a Linux guest virtualized using VMWare software, in the process of adding a physical block device to the virtual host.
You can explicitly add or remove a SCSI device, or rescan the entire SCSI bus without restarting the Linux virtual machine. This guide was tested with Vmware Server and Vmware Workstation v6.0 (older versions should also be supported). All commands are tested under RHEL, Fedora, CentOS and Ubuntu Linux guest/host operating systems.
First, add the hard disk through the vmware hardware settings menu. Click “VM > Settings”
Or you can also press CTRL D to enter the settings dialog box.
Click “Add” to add a new hard drive to the client:
Select the hardware type as "Hard disk" and click "Next":
Select “create a new virtual disk” and click “Next”:
Set the virtual disk type to "SCSI" and click "Next":
Set the maximum disk size as needed and click “Next”
Finally, select the file location and click “Finish”.
Enter the following command to rescan the SCSI bus:
echo "- - -" > /sys/class/scsi_host/host# /scan fdisk -l tail -f /var/log/message
The output is:
You need to replace host# with a real value, such as host0. You can check this value with the following command:
# ls /sys/class/scsi_host
Output:
host0
Then enter the following command to request a rescan:
echo "- - -" > /sys/class/scsi_host/host0/scan fdisk -l tail -f /var/log/message
The output is:
Jul 18 16:29:39 localhost kernel: Vendor: VMware, Model: VMware Virtual S Rev: 1.0 Jul 18 16:29:39 localhost kernel: Type: Direct-Access ANSI SCSI revision: 02 Jul 18 16:29:39 localhost kernel: target0:0:1: Beginning Domain Validation Jul 18 16:29:39 localhost kernel: target0:0:1: Domain Validation skipping write tests Jul 18 16:29:39 localhost kernel: target0:0:1: Ending Domain Validation Jul 18 16:29:39 localhost kernel: target0:0:1: FAST-40 WIDE SCSI 80.0 MB/s ST (25 ns, offset 127) Jul 18 16:29:39 localhost kernel: SCSI device sdb: 2097152 512-byte hdwr sectors (1074 MB) Jul 18 16:29:39 localhost kernel: sdb: Write Protect is off Jul 18 16:29:39 localhost kernel: sdb: cache data unavailable Jul 18 16:29:39 localhost kernel: sdb: assuming drive cache: write through Jul 18 16:29:39 localhost kernel: SCSI device sdb: 2097152 512-byte hdwr sectors (1074 MB) Jul 18 16:29:39 localhost kernel: sdb: Write Protect is off Jul 18 16:29:39 localhost kernel: sdb: cache data unavailable Jul 18 16:29:39 localhost kernel: sdb: assuming drive cache: write through Jul 18 16:29:39 localhost kernel: sdb: unknown partition table Jul 18 16:29:39 localhost kernel: sd 0:0:1:0: Attached scsi disk sdb Jul 18 16:29:39 localhost kernel: sd 0:0:1:0: Attached scsi generic sg1 type 0 Jul 18 16:29:39 localhost kernel: Vendor: VMware, Model: VMware Virtual S Rev: 1.0 Jul 18 16:29:39 localhost kernel: Type: Direct-Access ANSI SCSI revision: 02 Jul 18 16:29:39 localhost kernel: target0:0:2: Beginning Domain Validation Jul 18 16:29:39 localhost kernel: target0:0:2: Domain Validation skipping write tests Jul 18 16:29:39 localhost kernel: target0:0:2: Ending Domain Validation Jul 18 16:29:39 localhost kernel: target0:0:2: FAST-40 WIDE SCSI 80.0 MB/s ST (25 ns, offset 127) Jul 18 16:29:39 localhost kernel: SCSI device sdc: 2097152 512-byte hdwr sectors (1074 MB) Jul 18 16:29:39 localhost kernel: sdc: Write Protect is off Jul 18 16:29:39 localhost kernel: sdc: cache data unavailable Jul 18 16:29:39 localhost kernel: sdc: assuming drive cache: write through Jul 18 16:29:39 localhost kernel: SCSI device sdc: 2097152 512-byte hdwr sectors (1074 MB) Jul 18 16:29:39 localhost kernel: sdc: Write Protect is off Jul 18 16:29:39 localhost kernel: sdc: cache data unavailable Jul 18 16:29:39 localhost kernel: sdc: assuming drive cache: write through Jul 18 16:29:39 localhost kernel: sdc: unknown partition table Jul 18 16:29:39 localhost kernel: sd 0:0:2:0: Attached scsi disk sdc Jul 18 16:29:39 localhost kernel: sd 0:0:2:0: Attached scsi generic sg2 type 0
In addition to rescanning the entire bus, you can also use the following command to add or delete specified disks:
# echo 1 > /sys/block/devName/device/delete # echo 1 > /sys/block/sdc/device/delete
使用下面语法添加指定设备:
# echo "scsi add-single-device <H> <B> <T> <L>" > /proc/scsi/scsi
这里,
例如。使用参数 host#0,bus#0,target#2,以及 LUN#0 来添加 /dev/sdc,则输入:
# echo "scsi add-single-device 0 0 2 0">/proc/scsi/scsi # fdisk -l # cat /proc/scsi/scsi
结果输出:
Attached devices: Host: scsi0 Channel: 00 Id: 00 Lun: 00 Vendor: VMware, Model: VMware Virtual S Rev: 1.0 Type: Direct-Access ANSI SCSI revision: 02 Host: scsi0 Channel: 00 Id: 01 Lun: 00 Vendor: VMware, Model: VMware Virtual S Rev: 1.0 Type: Direct-Access ANSI SCSI revision: 02 Host: scsi0 Channel: 00 Id: 02 Lun: 00 Vendor: VMware, Model: VMware Virtual S Rev: 1.0 Type: Direct-Access ANSI SCSI revision: 02
现在使用 fdisk 并通过 mkfs.ext3[1] 命令创建分区:
# fdisk /dev/sdc ### [if you want ext3 fs] ### # mkfs.ext3 /dev/sdc3 ### [if you want ext4 fs] ### # mkfs.ext4 /dev/sdc3
# mkdir /disk3
打开 /etc/fstab 文件,输入:
# vi /etc/fstab
加入下面这行:
/dev/sdc3 /disk3 ext3 defaults 1 2
若是 ext4 文件系统则加入:
/dev/sdc3 /disk3 ext4 defaults 1 2
保存并关闭文件。
你可以使用 e2label 命令为分区加标签 [2]。假设,你想要为 /backupDisk 这块新分区加标签,则输入:
# e2label /dev/sdc1 /backupDisk
详情参见 "Linux 分区的重要性 [3]。
The above is the detailed content of Add a new hard drive to a Linux guest without restarting the virtual machine. For more information, please follow other related articles on the PHP Chinese website!