全部博文(16)
分类: LINUX
2008-03-27 01:14:43
The parted utility is becoming increasingly popular. It's an excellent tool developed by the GNU foundation. As with , you can use it to create, check, and destroy partitions, but it can do more. You can also use it to resize and copy partitions, as well as the filesystems contained therein. For the latest information, see .
On the Job |
It's much easier to make a mistake with parted . For example, I accidentally ran the mklabel command from the (parted) prompt on an existing RHEL system. It deleted all existing partitions. Fortunately, I had a snapshot of this system on a VMware server and was able to recover quickly with little trouble. |
During our discussion of parted, we'll proceed from section to section assuming that parted is still open with the following prompt:
(parted)
If you use parted and then check your partitions with , you might get errors such as:
Partition 1 does not end on cylinder boundary.
Don't worry about it. While partitions are associated with hard drive cylinders, parted is not so limited.
The next screen output lists commands that show how to start the parted utility, how to get help, and how to quit the program. In this case, the /dev/sdb drive is associated with the second SATA drive on a regular PC. Your computer may have a different hard drive; you can check the output from the df and commands for clues.
As you can see in , once you start parted, it opens its own command line prompt. The explanations are line-wrapped due to limitations in formatting in this book.
As you can see, a wide variety of commands are available within the parted interface. If you're familiar with , you can see that parted can do more: you can even format and resize partitions from parted. Unfortunately, the format functionality is limited and does not allow you to create or resize ext3 partitions, at least as of this writing. The current Exam Prep guide lists only resizing logical volumes as an RHCE requirement.
At the parted command line prompt, start with the print the partition table command. This allows you to review the current entries in the partition table, assuming one exists. Assuming you have free space, you then make a new (mkpart) partition or even make and format the filesystem (mkpartfs). If you need more information about command options, use the help command with it; here's an example:
(parted) help mkpart mkpart PART-TYPE [FS-TYPE] START END make a partition PART-TYPE is one of: primary, logical, extended FS-TYPE is one of: ext3, ext2, fat32, fat16, hfsx, hfs+, hfs, jfs, linux-swap,ntfs, reiserfs, hp-ufs, sun-ufs, xfs, apfs2, apfs1, asfs, amufs5, amufs4, amufs3, amufs2, amufs1, amufs0, amufs, affs7, affs6, affs5, affs4, affs3, affs2, affs1, affs0 START and END are disk locations, such as 4GB or 10%. Negative values count from the end of the disk. For example, -1s specifies exactly the last sector. mkpart makes a partition without creating a new file system on the partition. FS-TYPE may be specified to set an appropriate partition ID.
If that's too much for you, just run the command. You'll be prompted for the necessary information. Remember that you can have up to four primary partitions, corresponding to numbers 1 through 4. One of the primary partitions can be redesignated as an extended partition. The remaining partitions are logical partitions, numbered 5 and above. While the Linux parted utility allows you to create more than 15 partitions, in this case, anything beyond /dev/sdb15 is not recognized by Linux.
Deleting partitions is easy. All you need to do from the (parted) prompt is use the rm command to delete the partition that you no longer need.
Of course, before deleting any partition, you should:
Save any data you need from that partition.
Unmount the partition.
Make sure it isn't configured in /etc/fstab, so Linux doesn't try to mount it the next time you boot.
After starting parted, run the print command to identify the partition you want to delete, as well as its ID number.
For example, if you want to delete partition /dev/sdb10 from the (parted) prompt, run the following command:
(parted) rm 10
Whenever you install a new hard drive, you may need to create a new partition table. For example, after I add a new hard drive to my virtual RHEL system, just about any command I run in parted leads to the following message:
Error: Unable to open /dev/sdb - unrecognised disk label.
Before I can do anything else with this drive, I need to create a label. As shown from the list of available commands, I can do so with the mklabel command. As strange as it sounds, the default label to be used for Linux is msdos; here are the commands I run:
(parted) mklabel New disk label type? msdos
On the Job |
Be careful! Never run mklabel from the (parted) prompt on a hard drive that stores data that you need. |
Now you can create a new partition. Let me show you how mkpart works on the new hard drive. Naturally, if an extended partition already exists, you'll be able to create a logical partition.
(parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? ext3 Start? 0 End? 100MB
Now you can review the results:
(parted) print Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 0.51kB 100MB 100MB primary ext2
If this is the first partition you've created, the filesystem type is empty; otherwise it's ext2, even if you've specified another format such as ext3. Unfortunately, parted does not work perfectly, and it does not always create ext3 filesystems from the command line interface. (It works if an ext3 filesystem is already on the hard drive.)
If you now exit from parted, you can reboot or run the command to get Linux to read the new partition table. For the purpose of this chapter, don't exit from parted just yet.
On the Job |
The GUI parted tools (GParted, QTParted) do support formatting to a wider variety of filesystem formats, even though they're just "front ends" to parted. They may be available from third-party repositories such as those described in . |
Now let's repeat the process to create a swap partition. Make the start of the new partition 1MB after the end of the previous partition. You can still use the same commands, just substitute the linux-swap file system type as appropriate:
(parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? linux-swap Start? 101MB End? 1100MB
Now you can review the result:
(parted) print Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 0.51kB 100MB 100MB primary ext2 2 101MB 1100MB 1000MB primary
Let's repeat the process, creating a regular partition after the swap partition:
(parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? ext2 Start? 1101MB End? 2100MB
If you exit from parted, you can reboot, or run the command to make sure Linux reads the new partition table. Now go ahead and exit from parted. After exiting, you can implement your changes; format and then activate the swap partition on /dev/sdb2 using the following commands:
(parted) quit # partprobe # mkswap /dev/sdb2 # swapon /dev/sdb2
On the Job |
Sometimes you'll see errors when you run the command, even on a correctly configured system. For example, if you haven't put a disk in a floppy drive, you'll see errors related to the associated device (usually fd0). If the disk in your CD/DVD drive is read-only (as are most CD/DVD disks), you'll see an error message about being unable to open read-write. |
Now the new regular Linux partition is formatted to ext2. You can change it to ext3 with the following command:
# tune2fs -j /dev/sdb2
McGraw-Hill/Osborne 2007 (896 pages) | |
ISBN:9780072264548 |