Saturday, October 22, 2016

Disk management(chapter 9)

Disk management

 

Most users don’t think about disk management – they turn on their computers, plug in their flash drives, and expect everything to simply work. However, if you want to install a second internal hard drive in your computer, you'll need to partition and format it. Or you might need to repartition and reformat an external USB hard drive, or (for really advanced users) a USB flash drive. 

You can accomplish all these tasks via the Disk Management snap-in in the Microsoft Computer Management graphical application. However, you can just easily do so from the command line. The Command Prompt includes a powerful utility called DISKPART that allows you to manage disks, partitions, and volumes from the command line. 

In this chapter we'll show you the basics of using DISKPART.

 

TWO CAVEATS

 

Before we start using DISKPART, there are two points you should keep in mind.

First, DISKPART has to be run as an administrator. You'll need to run DISKPART in a Command Prompt window with administrative rights (we discussed how to do that in Chapter 1). If you try to run DISKPART without administrative rights, you'll only receive the usual "Access is denied" error message.

Second, DISKPART is a powerful utility. With a few keystrokes, you can wipe out entire hard drive partitions - and all the data contained on those partitions. Needless to say, you should take extreme caution while using DISKPART, lest you accidentally destroy large quantities of critical data.

With those points in mind, let's discuss the DISKPART prompt.

 

THE DISKPART PROMPT

 

DISKPART is different from the other commands we've discussed in this book. For most of these commands, you type the command and its options at the prompt, and view the output after the command finishes processing. DISKPART, however, has its own prompt. When you type the DISKPART command, the familiar C:\> prompt will change to the DISKPART prompt, which will look like this:

DISKPART>

The normal commands will not work in DISKPART. You will need to use special DISKPART commands, which we will discuss later in this chapter. To leave the DISKPART prompt and return to the normal Command Prompt window, simply type EXIT.

 

DISKS, PARTITIONS, VOLUMES

 

DISKPART works with disks, partitions, and volumes. But what do these terms mean?

A “disk”, in DISKPART parlance, refers to the actual physical disks themselves. Your hard drive is a disk. So are any USB flash drives, USB hard drives, or optical disks. 

A “volume” is a bit more nebulous and harder to define. A “volume”, in Windows, describes a mounted storage device represented by a drive letter. Your C: drive is a volume by this definition. The actual physical disk itself is a “disk” in DISKPART, but the logical filesystem on the disk, along with the drive letter, counts as a volume.

A “partition” in DISKPART is a virtual division on a disk. You could, if you chose, divide a hard drive into four separate virtual parts, and each one of those pieces would count as a partition. Note that a partition is not the same thing as a volume. A volume is formatted with a file system (usually NTFS, but sometimes FAT or FAT32) and has been assigned a drive letter by Windows. A partition is simply a logical division of a physical disk – the partition might be empty, and might not even have been formatted with a file system.

Every volume, however, is placed within a partition. Your hard drive has a partition – most computer hard drives have one big partition that fills the primary hard drive. (Consumer laptop and desktop hard drives generally have one big partition, and a smaller “system restore” partition that contains the Windows reinstallation files in case the computer’s operating system becomes corrupt or virus infected.) To sum up the terms – a physical disk can be divided into partitions, and the partitions can be formatted and mounted as volumes.

 

LISTING DISKS, PARTITONS, AND VOLUMES

 

DISKPART’s commands are context-specific. That means before using a command to alter a disk, a partition, or a volume, you have to first select the volume from the DISKPART prompt. (Needless to say, this makes selecting the proper disk, partition, or volume extremely important!)

But before you can select the appropriate disk, volume, or partition, you need to first employ the LIST command. Using the LIST command, you can find out what disks, volumes, and partitions are on your system. More importantly, you can find out what numbers Windows has assigned those items, since you’ll need to know the numbers in order to select them. So, to find out all the disks on your system, you would use this command from the DISKPART prompt:

LIST DISK

The output will look something like this:

  Disk ###  Status         Size     Free     Dyn  Gpt

  --------  -------------  -------  -------  ---  ---

* Disk 0    Online          149 GB      0 B

  Disk 1    Online         1959 MB      0 B

In this example, the computer has two disks, Disk 0 and Disk 1. (Computers, using binary, start counting from 0 instead of 1.) Disk 0 is a 149 gigabyte hard drive, while Disk 1 is a two gigabyte (approximately) USB flash drive. Why does Disk 0 have an asterisk character (*) in front of it? That means that Disk 0 is the currently selected disk – any DISKPART commands that affect a disk will affect Disk 0.

To see all the current volumes on your system, use this command:

LIST VOLUME

The output will look something like this:

 Volume ###  Ltr  Label        Fs     Type        Size     Status     Info

  ----------  ---  -----------  -----  ----------  -------  ---------  --------

  Volume 0     D                       DVD-ROM         0 B  No Media

* Volume 1     C                NTFS   Partition    148 GB  Healthy    System

  Volume 2     E                FAT    Removable   1959 MB  Healthy

In this example, the computer has three volumes, Volumes 0, 1, and 2. The output also shows the volume’s drive letter in the “Ltr” column, its filesystem in the “Fs” column, the kind of volume (DVD-ROM, Removable disk, etc.) in the “Type”, and the size in gigabytes or megabytes in the “Size” column. In this example, Volume 1 is selected – hence the asterisk before its entry in the output. Any commands that affect volumes will affect Volume 1 (in this case, the computer’s C drive). 

To find the partitions on the currently selected disk, use this command:

LIST PARTITION

The LIST PARTITION command will generate an output like this:

 Partition ###  Type              Size     Offset

  -------------  ----------------  -------  -------

* Partition 1    Primary           1959 MB    31 KB

Remember, the LIST PARTITION command only lists the partitions on the currently selected disk, not the partitions on every single disk attached to the system. In this example, we are looking at the single partition on the USB flash drive from the earlier examples. As before, Partition 1 (the only partition on the flash drive) is selected, and therefore has the asterisk in front of it.

 

SELECTING DISKS, PARTITIONS, AND VOLUMES

 

Listing disks, partitions, and volumes is well and good, but you’ll still need to select them, since using a DISKPART command on the wrong target can prove disastrous. You can select targets of your choosing using the SELECT command. To use the SELECT command, first use the LIST command to view the disks, volumes, or partitions currently available. Make note of the number assigned to each object. For instance, if you wanted to select disk 1, you would use this command:

SELECT DISK 1

This will select disk 1. If you use the LIST DISK command afterward, you will see that disk 1 is now the current disk. Selecting volumes works much the same way. If you wanted to SELECT volume 1, use this command:

SELECT VOLUME 1

If you run the LIST VOLUME command after this, you will see that Volume 1 is now the selected volume.

Selecting partitions works a little differently. To select a partition, you must first use SELECT DISK to select the disk you want to use, and then LIST PARTITION to see the available partitions on the disk. To then select Partition 0 on the disk, you would use this command:

SELECT PARTITION 0

If you then use LIST PARTITION, you will see that Partition 0 is now the selected partition. 

Remember that you can only select partitions after you have first selected the disk containing the partition. 

 

CLEANING DISKS

 

Once you’ve selected a disk, you can then issue commands that will affect that disk.

The most drastic command you can use is the CLEAN command. The CLEAN command completely clears off a disk – all volumes and partitions on the disk are deleted.  Generally, it’s best to use CLEAN when preparing a new disk, or wiping an old one. Make sure you’ve selected the right disk before using CLEAN – it is quite possible to wipe out valuable data with a single mistyped command!

(Note that the CLEAN command does not overwrite the deleted data – it is still there, and can be removed. To securely wipe a hard drive, you’ll need to find a third-party utility that does secure disk erasure.) 

 

CREATING PARTITIONS

 

Once you have a blank disk, you can use DISKPART to create partitions on it. You could do that with DISKPART’S CREATE command. You can create five kinds of partitions using CREATE PARTITION, but for this book we will focus on three of them – a primary partition, the extended partition, and the logical partition.

Every disk can have only one primary partition. If a disk has only one partition, it’s almost always one big primary partition filling up the drive. Windows’s key boot files (like boot.ini and ntldr.exe) must be located on a primary partition in order for the system to boot up. 

Likewise, each disk can only have one extended partition. The extended partition is essentially a secondary partition – people will sometimes partition their hard drives so that the system files are located on one partition, and their data files on the second. However, some hard drives have more than two partitions on the disk. How is this accomplished? The answer is logical partitions – each extended partition can contain a larger number of logical partitions. So, if you wanted to divide your hard disk into five separate volumes, you could create a primary partition, an extended partition, and then create the remaining number of logical partitions inside the extended partition.

To create a partition on a disk, first select it with the SELECT command. Then, use the CREATE command to create a partition of the type and size you want. For instance, to create a primary partition:

CREATE PARTITION PRIMARY

This command will create a partition that fills up the entire disk. If you want to limit the size, use CREATE with the SIZE modifier. For instance, this command will create a primary partition of 20 gigabytes on the currently selected disk:

CREATE PARTITION PRIMARY SIZE=20000

Note that the SIZE modifier uses megabytes for its measurement, and there are (approximately) 1000 megabytes in a single gigabyte. So if you wanted a partition of 20 gigabytes, you would tell the CREATE command to create a partition with a size of 20000 megabytes. 

To create an extended partition, use this command:

CREATE PARTITION EXTENDED

As with a primary partition, you can also use SIZE to specific the size in megabytes of the desired partition.

To create a logical partition, you’ll need to first select an extended partition using the SELECT PARTITION command. Once you’ve selected the logical partition, use this command to create the logical partition:

CREATE PARTITION LOGICAL

As with the previous two examples, you can use the SIZE modifier to limit the space the newly created logical partition will use. This is useful if you want to put more than one logical partition within the extended partition. 

 

CREATING VOLUMES

 

After you’ve create partitions, you can create volumes upon your partitions. DISKPART lets you create several different kinds of volumes, including RAID partitions, but for the purposes of this demonstration we'll stick to creating simple volumes. To create a simple volume, use this command:

CREATE VOLUME SIMPLE

You can also use the SIZE modifier to regulate the size of the simple volume. For instance, to create a simple volume with a size of 20 gigabytes:

CREATE VOLUME SIMPLE SIZE=20000

As before, the size is measured in megabytes. 

 

FORMATTING VOLUMES

 

To "format" a disk means to prepare it with a filesystem for use with your computer. A disk (usually) doesn't come with a filesystem preinstalled - it's simply a blank slate. Formatting the disk with a filesystem marks the disk with a storage system, which means your computer can then read and write data to the disk. 

In DISKPART, you format volumes, not disks or partitions. To format a volume, first select it with the SELECT command, and then use the FORMAT command. The FORMAT command comes with a few options to alter its performance. The most commonly used one is the FS options, which lets you specify which filesystem to use with the volume. For instance, to format the volume with the NTFS filesystem:

FORMAT FS=NTFS

You can also select the FAT or FAT32 filesystems. Why would you use a different filesystem than NTFS? Generally, any internal hard disks on your computer should be formatted with NTFS (and any partition where Windows is installed has to be formatted NTFS). Using NTFS on your internal hard disks let you make use of NTFS permissions to secure your files. If you have a USB flash drive, however, it's often best to format it with FAT or FAT32. Mac OS X and Linux computers generally can read from, but not write to, NTFS volumes, and if your flash drive is formatted NTFS Mac and Linux systems will not be able to write data to it. (Of course, for certain secure implementations, you may want to format your USB flash drive with NTFS.) 

 

ASSIGNING DRIVE LETTERS

 

Once you've created volumes and formatted them, you can use DISKPART to assign the volumes drive letters. You can do this with the ASSIGN command. To use it, select the volume you that will receive a letter. Once you've selected the appropriate volume, use this command to assign it (for example) the drive letter H:

ASSIGN LETTER=H

This will assign the drive letter H to the selected volume. 

You can use this command to change the letter of already created volumes - just select the appropriate volume with the SELECT command and then use the ASSIGN command to change the letter. However, take care when doing this, since you might accidentally alter file paths that a program needs to use - Windows Backup, for instance, might become annoyed that the path of your backup hard drive has changed from E:\ to F:\. 

Note that you cannot change the drive letter of the volume containing the Windows pagefile. In most cases, the pagefile is located on the same volume as the Windows system files. 

 

DELETING PARTITIONS

 

Deleting partitions from DISKPART is easy. It is so easy, in fact, that you should exercise extreme caution while doing so, lest you accidentally destroy important data in the process. You can delete partitions using the DELETE PARTITION command. If you wanted to delete Partition 1 on Disk 0, first select Disk 0, and then Partition 1, using the SELECT command. Once the appropriate partition on the correct disk has been selected, use the DELETE PARTITION command:

DELETE PARTITION

This will delete Partition 1 on the currently selected disk. If the partition is an extended partition, any logical partitions within it will be deleted as well. 

Note that you must first select the correct disk before deleting any partitions upon it. It's best to be very careful about this - if you accidentally delete Partition 1 on Disk 1 instead of Partition 1 on Disk 0, you run the risk of destroying important data. 

 

DELETING VOLUMES

 

Deleting volumes is somewhat less destructive than deleting partitions, and runs less risk of accidentally wiping out large chunks of data (provided, of course, you choose the correct volume). To delete a volume, first select the volume you want to delete with the SELECT command, and then use the DELETE VOLUME command:

DELETE VOLUME

This will delete the selected volume.

As always, take care when deleting volumes, and double-check that you have selected the correct volume before deleting it.

Place your comment down.

 

No comments:

Post a Comment