Home  >  Article  >  System Tutorial  >  Add a new hard drive to a Linux guest without restarting the virtual machine

Add a new hard drive to a Linux guest without restarting the virtual machine

PHPz
PHPzforward
2024-04-03 16:34:11907browse

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.

在不重启虚拟机的情况下为 Linux 客户机添加新硬盘

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.

Step 1: Add a new hard drive to the virtual guest

First, add the hard disk through the vmware hardware settings menu. Click “VM > Settings”

在不重启虚拟机的情况下为 Linux 客户机添加新硬盘

Or you can also press CTRL D to enter the settings dialog box.

Click “Add” to add a new hard drive to the client:

在不重启虚拟机的情况下为 Linux 客户机添加新硬盘

Select the hardware type as "Hard disk" and click "Next":

在不重启虚拟机的情况下为 Linux 客户机添加新硬盘

Select “create a new virtual disk” and click “Next”:

在不重启虚拟机的情况下为 Linux 客户机添加新硬盘

Set the virtual disk type to "SCSI" and click "Next":

在不重启虚拟机的情况下为 Linux 客户机添加新硬盘

Set the maximum disk size as needed and click “Next”

在不重启虚拟机的情况下为 Linux 客户机添加新硬盘

Finally, select the file location and click “Finish”.

Step 2: Rescan the SCSI bus and add SCSI devices without restarting the virtual machine

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:

在不重启虚拟机的情况下为 Linux 客户机添加新硬盘

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
How to delete the /dev/sdc device?

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
How to add the /dev/sdc device?

使用下面语法添加指定设备:

# echo "scsi add-single-device <H> <B> <T> <L>" > /proc/scsi/scsi

这里,

  • :主机
  • :总线(通道)
  • :目标 (Id)
  • :LUN 号

例如。使用参数 host#0bus#0target#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
步骤 #3:格式化新磁盘

现在使用 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
步骤 #4:创建挂载点并更新 /etc/fstab
# 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!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete