Sunday, June 03, 2012

Adding a drive to VMware Workstation Linux guest without restarting

Recently I was working on a CentOS 6.2 guest on VMware Workstation 8 and I wanted to add an additional hard drive. From VMware Workstation I knew how to do this but not from the Linux side. Being unfamiliar with this, searched and found a few handy steps to add the drive without rebooting the Linux guest.

First, let's see what drives are showing up for the Linux guest. From the host we're going to check the /dev directory and see what drives are showing up.

#cd /dev
#ls





Here we can see that there is three drives, sda, sdb and sdc listed on the guest. Under these two drives is three partitions titled sda1 and sda2, plus sdb1. Notied that drive sdc does not have any partitions. This breaks down like this.

  • SDA
    • SDA1
    • SDA2
  • SDB
    • SDB1
  • SDC
    • No partitions
So now, we want to add an additional hard drive then add an partition.

From VMware Workstation, we're going to add the drive to the guest.

  1. Right click the guest, and click the "Settings"
  2. The Virtual Machine Settings window will open
  3. Click the "add" button
  4. The "Add Hardware Wizard" window will open
  5. Select "Hard Disk" then click the "Next" button
  6. Under the "Select a Disk" leave the default as "Create a new virtual disk", then click the "Next" button
  7. Under the "Select a Disk Type" leave the default as "SCSI", then click the "Next" button
  8. Under the "Specify Disk Capacity", change the value for "Maximum disk size (GB)" to the amount you want. For this example, I will be using 5GB. Then click the "Next" button
  9. Under the "Specify Disk File" leave the file name as default and then click the "Finish" button
  10. Back at the "Virtual Machine Settings" window, click the "OK" button to complete
  11. The new drive has been added to the guest
 If you check the guest again, you will notice that the new drive does not show up automatically under the /dev directory. There are two solutions to this, one is restarting the guest, the other is a little bit longer method. We're going to follow the second step, which is handy if you wish to keep the system up.

These commands were posted on another blog, Cyberciti's how to add a new VMware hard disk article.

First we're going to scan the system for the new drives. We need the host numbers on the guest machine first.

  1. First find the host number on your system
    1. Run the following command
    2. #ls /sys/class/scsi_host
    3. You should see the return of "host0" or more 
  2.  Force the system to scan the drives
    1. The host number you found in the previous step will be used here.
    2. Run the following command
    3. #echo "- - -" > /sys/class/scsi_host/host2/scan (note on my system it's "host2")
    4. After this you may see a message of the drive scanned on the machine
  3. To confirm, check the logs for messages of new drive
    1. Run the following command
    2. #tail /var/log/messages
    3.  This will be sequential from the last drives. For example, if you see in /dev drives SDA, SDB, then you should see logs for drive SDC
  4. From fdisk we're going to also validate it's showing up
    1. Run the following command
    2. #fdisk -l
    3. You should see the drive showing up that matches the drive from step 3
  5. From /dev you should also see the new drive listed
    1. Run the following command
    2. #ls /dev
    3. You should see the drive showing up that you just added
Now that we have the drive installed and viewed from the guest, let's create the partition and format. We are going to be using the steps as mentioned in the blog Cyberciti's how to add Linux hard disk.

  1. First find the drive has been discovered on the system
    1. Run the following command
    2. #fdisk -l | grep 'Disk'
    3. You should see the drive you added in the previous steps shown near the end of the list
    4. Save this information for the next steps. 
  2. Run fdisk to create the partition on the new disk (if anytime you get lost on commands, type "m" for the command menu
    1. Run the following command
    2. #fdisk /dev/sdd (your drive may be different)
      1. In fdisk, type "n" to create a new partition
      2. Type "p" for a primary partition
      3. Type "1" for a partition number
      4. Type "1" for a first cylinder number
      5. Type "1G" for size (this can be adjusted for your needs)
      6. Type "w" to write and save the information
      7. This will automatically close fdisk
  3. Validate the partition is now showing up in /dev directory
    1. Run the following command
    2. #ls /dev
    3. You should see the new partition showing up under the drive you just created
  4. We're now going to format the partition with ext3 so that it's usable 
    1. Run the following command 
    2.  #mkfs.ext3 /dev/sdd1
    3. You should see confirmation the command was successful
  5. Finally we are going to mount the drive
    1. Run the following commands
    2. #mkdir /disk1
    3. #mount /dev/sdd1 /disk1 (your drive may be different)
  6. Confirm the drive is showing up
    1. Run the following command
    2. #df -H
    3. You should see your new drive mounted
  7. Test the new mount
    1. Run the following commands
    2. #touch  /disk1/testfile
    3. #ls /disk1
    4. You should see your file written to the new mount
  8. Now set the mount at boot
    1. Run the following command
    2. #vi /etc/fstab
    3. Add the following line to the fstab file (note, I'm not going to show basic Vi commands here, you can find them across the Internet)
    4. /dev/sdd1         /disk1      ext3     defaults    1    2
    5. Save the file
  9. Done!
Now you should have a new drive on your virtual machine without restarting the guest. If you don't mind restarting then you can skip steps scanning steps. Also be careful of editing the /etc/fstab file, this can lead to problems with the boot of the machine.












2 comments:

Tmantiko said...

Amazing tutorial - was a great help for me in creating a linux partition under vmware workstation - thx!

Tmantiko said...

Thx for the great tutorial - was a big help for me in creating a linux partition under vmware workstation