Chapter 8: Configuring and Maintaining Hard Disk Drives

 < Day Day Up > 



In this chapter, you’ll learn techniques for configuring and maintaining hard disk drives—and there’s a lot more to this than most people realize. With Microsoft Windows Server 2003 and Windows XP Professional, hard disk drives can be configured with two disk types, basic and dynamic, and two disk partition types, Master Boot Record (MBR) and GUID Partition Table (GPT). The disk type and partition type you choose primarily depends on the system architecture. If you are working with x86-based systems, you can use either basic or dynamic disk types with the MBR partition style. If you are working with IA64-based systems, you can use the basic disk type with the GPT partition style.

Getting Started with DiskPart

DiskPart is the tool of choice for working with disks, partitions, and volumes. Key tasks you’ll use DiskPart for are to convert disk types, create partitions and volumes, and to configure RAID. Beyond this, you can also use DiskPart to configure automounting of new disks as well as to assign drive letters and drive paths. DiskPart isn’t used for formatting disks, however. For this, you’ll use the FORMAT command, as discussed in the section of Chapter 9, “Partitioning Basic Disks,” titled “Formatting Partitions.”

DiskPart Basics

Unlike all the other commands we’ve worked with so far in this book, DiskPart isn’t a simple command-line utility that you invoke using a command line and parameters. Rather, it is a text-mode command interpreter that you invoke so that you can manage disks, partitions, and volumes using a separate command prompt and commands that are internal to DiskPart. You invoke the DiskPart interpreter by typing diskpart in a command window and pressing Enter.

DiskPart is designed to work with physical hard disks installed on a computer. No CD/DVD drives, removable media, or USB-connected flash RAM devices are supported. Before you can use DiskPart commands, you must first list, and then select, the disk, partition, or volume you want to work with to give it focus. When a disk, partition, or volume has focus, any DiskPart commands that you type will act on that disk, partition, or volume.

List the available disks, partitions, and volumes by using the following list commands:

  • list disk Lists all physical hard disks on the computer

  • list volume Lists all volumes on the computer (including hard disk partitions and logical drives)

  • list partition Lists partitions, but only on the disk that has focus

Note

CD/DVD drives, removable media, and USB-connected flash RAM devices are included when you list volumes. However, as stated previously, you cannot use DiskPart to manage these devices.

When you use the list commands, an asterisk (*) appears next to the disk, volume, or partition with focus. You select a disk, volume, or partition by its number or drive letter, such as disk 0, partition 1, volume 2, or volume D.

When you are finished working with DiskPart, type exit at the DiskPart prompt to return to the standard command line.

DiskPart: An Example

To see how you can work with DiskPart, consider the following example that invokes DiskPart, lists the available disks, and then gives focus to disk 2:

  1. To invoke DiskPart, type diskpart at the command prompt.

  2. The command prompt changes to

    DISKPART>
  3. This tells you that you are in the text-mode interpreter for DiskPart. To list available disks, type list disk after the command prompt.

  4. The output of list disk shows you the available disks, their status, size, and free space:

    Disk ###     Status        Size       Free       Dyn    Gpt
    -------- ---------- ------- ------- --- ---
    Disk 0 Online 56 GB 0 B
    Disk 1 Online 29 GB 0 B
    Disk 2 Online 37 GB 33 GB *

  5. Because disk 2 is the one we want to work with, we give it focus by typing select disk 2 after the command prompt.

  6. DiskPart reports:

    Disk 2 is now the selected disk.
  7. Work with the disk, and when you are finished, exit the DiskPart prompt by typing exit after the command prompt.

Understanding Focus and What It Means

When you select a disk, partition, or volume, the focus remains on that object until you select a different object. In the previous example, the focus is set on disk 2, but if you were to select volume 2 on disk 0, the focus would shift from disk 2 to disk 0, volume 2. In some cases, the focus changes automatically, based on the command you use. For example, when you create a partition or volume, the focus automatically switches to the new partition or volume.

You can only give focus to a partition on the currently selected disk. When a partition has focus, the related volume, if any, also has focus. When a volume has focus, the related disk and partition also have focus if the volume maps to a single specific partition. If the volume doesn’t map to a single specific partition, only the volume has focus.

DiskPart Commands and Scripts

LIST and SELECT are only two of the many commands that DiskPart has to offer. A complete list of DiskPart commands is shown in Table 8-1. Many of the commands listed accept Noerr as an additional parameter. The Noerr parameter is used with DiskPart scripts to indicate that, when an error is encountered, DiskPart should continue to process commands in the script. Without Noerr, an error causes DiskPart to exit with an error code, which halts execution of the script.

Table 8-1: DiskPart Command Summary

Command

Description

Syntax

ACTIVE

On MBR disks, marks the partition with current focus as the active system partition, meaning it is the partition containing the operating system startup files.

active

ADD

Creates a mirrored volume on the selected dynamic disk.

add disk=n where n is the disk number that will contain the mirror

ASSIGN

Assign a drive letter or mount point to the selected partition, logical drive, or volume.

assign letter=x
assign mount=path

AUTOMOUNT

Controls whether Windows automatically mounts new basic volumes that are added to the system and assigns them drive letters (Windows Server 2003 only).

automount enable | disable | scrub

BREAK

Break a mirror set. Add Nokeep to specify that only one volume should be retained, which means the other volume is deleted.

break disk=n
break disk=n nokeep

CLEAN

Removes all partition or volume formatting on the disk that has focus. With CLEAN ALL, all disk sectors are set to zero.

Clean
clean all

CONVERT

Converts between different disk formats.

convert basic | dynamic convert gpt | mbr

CREATE

Creates a partition or volume of a specific type.

create partition efi | extended | logical | msr | primary create volume simple | raid | stripe

DELETE

Delete the disk, partition, or volume that has focus.

delete disk | partition | volume

DETAIL

Provide details about the disk, partition, or volume that has focus.

detail disk | partition | volume

EXIT

Exits the DiskPart interpreter.

exit

EXTEND

Extend the simple volume on the selected disk or spans the simple volume across multiple disks.

extend size=n disk=n

GPT

Changes GPT attributes on the partition with focus (Windows Server 2003 only).

gpt attributes=n

HELP

Displays a list of commands.

help

IMPORT

Imports a foreign disk.

import

INACTIVE

On MBR disks, marks the partition with focus as inactive, meaning the computer won’t boot from the system partition and will instead look for the next boot option in BIOS (Windows Server 2003 only).

inactive

LIST

Displays a list of disks or volumes and information about them, or a list of partitions on the disk that has focus.

list disk | partition | volume

ONLINE

Brings the selected disk or volume online. Resychronizes the mirrored or RAID-5 volume that has focus.

online

REM

Marks the start of a comment in a DiskPart script.

rem comment

REMOVE

Removes a drive letter or mount point from the currently selected volume. Optionally, you can add the All and the Dismount parameters.

remove letter=x
remove mount=path

REPAIR

Repairs the RAID-5 volume with focus by replacing the failed volume with the designated dynamic disk (Windows Server 2003 only).

repair disk=n

RESCAN

Looks for new disks that may have been added to the computer.

rescan

RETAIN

Prepares the selected simple volume to be used as the boot or system volume.

retain

SELECT

Selects a disk, partition, or volume, giving it focus.

select disk | partition | volume

  • Commands that use Noerr and exit with error codes are ADD, ASSIGN, AUTOMOUNT, BREAK, CONVERT, CREATE, DELETE, EXTEND, IMPORT, ONLINE, REMOVE, and REPAIR.

  • Commands that don’t use Noerr or exit with error codes are ACTIVE, CLEAN, DETAIL, EXIT, GPT, HELP, INACTIVE, LIST, REM, RESCAN, RETAIN, and SELECT.

Speaking of DiskPart scripts, the way you use scripts with DiskPart is a bit different from their use with other commands. The reason is that DiskPart is a text- mode interpreter, not a standard utility. When you invoke DiskPart (by typing diskpart at the command prompt), you tell the interpreter about the script you want to use by adding the /S parameter as shown here:

diskpart /s ScriptName.txt 

where ScriptName.txt is the name of the text file that contains the script you want to use. By default, the output from DiskPart is written to the current command prompt. You can redirect the output to a file as shown here:

diskpart /s ScriptName.txt > LogFile.log

or

diskpart /s ScriptName.txt >> LogFile.log

where LogFile.log is the name of the text file to which DiskPart output should be written.

Note

Remember that > is used to create or overwrite a file using output redirection and that >> is used to create or append to an existing file.

Tip

The advantage of using scripts over directly inputting commands is that you can automate disk-related tasks so that they can be performed repeatedly and in exactly the same way each time. Scripting disk management tasks is useful if you are deploying Windows using unattended Setup, RIS or Sysprep, which do not support creating volumes other than the boot volume.

The error codes to look for when working with DiskPart scripts are as follows:

  • 0 Indicates no errors occurred, execution proceeded without failure

  • 1 Indicates a fatal exception occurred and there may be a serious problem

  • 2 Indicates the parameters you specified for a command were incorrect

  • 3 Indicates DiskPart was unable to open the specified script or output file

  • 4 Indicates a service that DiskPart uses returned an error code or reported failure

  • 5 Indicates that a command syntax error occurred; typically because a disk, partition, or volume was improperly selected or was invalid for use with the command

DiskPart: A Script Example

When you use DiskPart scripts, you should complete all the operations you want to perform as part of a single session. The script should contain all of the DiskPart commands you want to execute. It is not necessary to include the EXIT command because the text-mode interpreter exits automatically at the end of the script. Consider the following example script:

Listing 8-1: Sample DiskPart Script

start example
rem Select disk 2
select disk 2

rem Create the primary partition on the disk and assign the drive letter
create partition primary size=4096assign letter=s

rem Create extended partition with 2 logical drives
create partition extended size=4096
create partition logical size=2048
assign letter=u
create partition logical size=2047
assign letter=v
end example

Here, you create a primary and an extended partition on disk 2. The primary partition is set to 4096 megabytes (MB) in size and is assigned the drive letter S. The extended partition is created, set to 4096 MB in size, and two logical partitions are added. The first logical partition is 2048 MB in size and is assigned drive letter U. The second logical partition is 2047 MB in size and is assigned drive letter V. The sizes are set this way on the logical partitions because you lose some space due to the partitioning. You could have also created a single logical partition that was 4096 MB in size.

Note

Creating partitions and assigning drive letters as shown in this example doesn’t make the partitions available for use. They must still be formatted using the FORMAT command. For more information on formatting partitions and volumes, see the section of Chapter 9 titled “Formatting Partitions.”

Tip

Because DiskPart must make and then apply changes, you shouldn’t run multiple DiskPart scripts back to back. Instead, you should wait for 10 to 15 seconds in between running scripts or handle all tasks in a single DiskPart session. Not only does this help ensure that the last command issued by the previous DiskPart session is completed; it also ensures the previous DiskPart session is shut down before the next session begins.

You can run the example script by typing diskpart /s ScriptName, such as diskpart /s disk2config.txt. When you run the script, the output you should expect is as follows:

Disk 2 is now the selected disk.
DiskPart succeeded in creating the specified partition.
DiskPart successfully assigned the drive letter or mount point.
DiskPart succeeded in creating the specified partition.
DiskPart succeeded in creating the specified partition.
DiskPart successfully assigned the drive letter or mount point.
DiskPart succeeded in creating the specified partition.
DiskPart successfully assigned the drive letter or mount point.

As you can see, DiskPart reports step-by-step success or failure. Keep in mind that the script doesn’t have to be on the local computer. If the DiskPart script was saved to the network share \\corpserver01\scripts, you can invoke it by typing

diskpart /s \\corpserver01\scripts\disk2config.txt

This assumes that the network share is available to the local system. You can also map network drives at the command line with the NET USE command. The format is

net use DriveLetter \\ComputerName\ShareName 

such as in this example:

net use X: \\corpserver01\scripts
Note

The NET USE command also accepts user name and password information provided in the form /USER:Domain\User. You can also specify whether the mapped drive is persistent (that is, whether the network share mapping should remain when the computer is restarted). Use the parameter /Persistent:Yes. Persistent network share mappings can be deleted by typing net use \\ComputerName\ShareName /DELETE.

By default, if DiskPart encounters an error while executing a command, it stops processing the script and displays an error code. If you specify the Noerr parameter, however, DiskPart will report the error and continue execution of the script. Additionally, you don’t have to type the command line that invokes DiskPart directly at the command prompt. The command could be part of a larger script, which I’ll refer to as a master script. A sample master script is shown as Listing 8-2.

Listing 8-2: Sample Master Script

start example
@echo off
@if not "%OS%"=="Windows_NT" goto :EXIT
@if "%1"=="" (set INFO=echo && set SEXIT=1) else (set INFO=rem && set SEXIT=0)

%INFO% ************************
%INFO% Script: Disk2Setup.bat
%INFO% Creation Date: 6/8/2004
%INFO% Last Modified: 9/23/2004
%INFO% Author: William R. Stanek
%INFO% Email: williamstanek@aol.com
%INFO% ************************
%INFO% Description: Configures the standard partitions on workstations
%INFO% with a third hard drive. The script is configured so
%INFO% that it will only run if you pass in a parameter, which
%INFO% can be any value. This is meant as a safeguard to help
%INFO% prevent accidental formatting of disks.
%INFO% ************************
@if "%SEXIT%"=="1" goto :EXIT

@title "Configuring Disk 2..."
cls
color 07

net use x: \\corpserver01\scripts
diskpart /s x:\disk2config.txt

format s: /fs:ntfs
format u: /fs:ntfs
format v: /fs:ntfs

:EXIT
echo Exiting...
end example

That’s it for the introduction to DiskPart, the remainder of this chapter discusses the specifics of using DiskPart and related commands such as CHKDSK and DEFRAG to create, manage, and maintain disks, partitions, and volumes.



 < Day Day Up > 



Microsoft Windows Command-Line Administrator's Pocket Consultant
MicrosoftВ® WindowsВ® Command-Line Administrators Pocket Consultant
ISBN: 0735620385
EAN: 2147483647
Year: 2004
Pages: 114

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net