在不重啟的情況下,為Vmware Linux客戶機添加新硬盤
作為一名系統(tǒng)管理員,我經(jīng)常需要用額外的硬盤來擴充存儲空間或?qū)⑾到y(tǒng)數(shù)據(jù)從用戶數(shù)據(jù)中分離出來。我將告訴你在將物理塊設(shè)備加到虛擬主機的這個過程中,如何將一個主機上的硬盤加到一臺使用 VMWare 軟件虛擬化的 Linux 客戶機上。
你可以顯式的添加或刪除一個 SCSI 設(shè)備,或者重新掃描整個 SCSI 總線而不用重啟 Linux 虛擬機。本指南在 Vmware Server 和 Vmware Workstation v6.0 中通過測試(更老版本應該也支持)。所有命令在 RHEL、Fedora、CentOS 和 Ubuntu Linux 客戶機 / 主機操作系統(tǒng)下都經(jīng)過了測試。
步驟 1:添加新硬盤到虛擬客戶機
首先,通過 vmware 硬件設(shè)置菜單添加硬盤。點擊 “VM > Settings”
Vmware Virtual Machine Settings
或者你也可以按下 CTRL + D
也能進入設(shè)置對話框。
點擊 “Add” 添加新硬盤到客戶機:
VMWare adding a new hardware
選擇硬件類型為“Hard disk”然后點擊 “Next”:
VMware Adding a new disk wizard
選擇 “create a new virtual disk” 然后點擊 “Next”:
Vmware Wizard Disk
設(shè)置虛擬磁盤類型為 “SCSI” ,然后點擊 “Next”:
Vmware Virtual Disk
按需要設(shè)置***磁盤大小,然后點擊 “Next”
Finalizing Disk Virtual Addition
***,選擇文件存放位置然后點擊 “Finish”。
步驟 2:重新掃描 SCSI 總線,在不重啟虛擬機的情況下添加 SCSI 設(shè)備
輸入下面命令重新掃描 SCSI 總線:
echo "- - -" > /sys/class/scsi_host/host# /scan
fdisk -l
tail -f /var/log/message
輸出為:
Linux Vmware Rescan New Scsi Disk Without Reboot
你需要將 host#
替換成真實的值,比如 host0
。你可以通過下面命令來查出這個值:
# ls /sys/class/scsi_host
輸出:
host0
然后輸入下面過命令來請求重新掃描:
echo "- - -" > /sys/class/scsi_host/host0/scan
fdisk -l
tail -f /var/log/message
輸出為:
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
如何刪除 /dev/sdc 這塊設(shè)備?
除了重新掃描整個總線外,你也可以使用下面命令添加或刪除指定磁盤:
# echo 1 > /sys/block/devName/device/delete
# echo 1 > /sys/block/sdc/device/delete
如何添加 /dev/sdc 這塊設(shè)備?
使用下面語法添加指定設(shè)備:
# echo "scsi add-single-device <H> <B> <T> <L>" > /proc/scsi/scsi
這里,
- :主機
- :總線(通道)
- :目標 (Id)
- :LUN 號
例如。使用參數(shù) 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
結(jié)果輸出:
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:格式化新磁盤
現(xiàn)在使用 fdisk 并通過 mkfs.ext3 命令創(chuàng)建分區(qū):
# fdisk /dev/sdc
### [if you want ext3 fs] ###
# mkfs.ext3 /dev/sdc3
### [if you want ext4 fs] ###
# mkfs.ext4 /dev/sdc3
步驟 #4:創(chuàng)建掛載點并更新 /etc/fstab
# mkdir /disk3
打開 /etc/fstab
文件,輸入:
# vi /etc/fstab
加入下面這行:
/dev/sdc3 /disk3 ext3 defaults 1 2
若是 ext4 文件系統(tǒng)則加入:
/dev/sdc3 /disk3 ext4 defaults 1 2
保存并關(guān)閉文件。
可選操作:為分區(qū)加標簽
你可以使用 e2label 命令為分區(qū)加標簽 。假設(shè),你想要為 /backupDisk
這塊新分區(qū)加標簽,則輸入:
# e2label /dev/sdc1 /backupDisk
詳情參見 "Linux 分區(qū)的重要性 。
關(guān)于作者
作者是 nixCraft 的創(chuàng)始人,也是一名經(jīng)驗豐富的系統(tǒng)管理員,還是 Linux 操作系統(tǒng) /Unix shell 腳本培訓師。他曾服務過全球客戶并與多個行業(yè)合作過,包括 IT,教育,國防和空間研究,以及非盈利機構(gòu)。你可以在 Twitter,Facebook,Google+ 上關(guān)注他。