官术网_书友最值得收藏!

Using the parted utility

The parted utility is geared towards situations where we have a hard disk or hard disks larger than 2 TB. Additionally, we can a resize a partition; the fdisk utility cannot resize a partition. Almost all of the newer Linux distributions support the parted utility. parted comes from GNU; it's a text-based partitioning utility that works with a variety of disk types such as MBR, GPT, and BSD, to name  a few.

Always backup your data before doing any partitioning.

To start with, we will use the parted command on the /dev/sdb:

root@ubuntu:/home/philip# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

From here, we are placed into the parted utility. Similar to the fdisk utility, the parted utility is interactive. Now let's say we want to view the help menu. Here, we can enlist the help at the CLI:

(parted) help 
align-check TYPE N check partition N for TYPE(min|opt) alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
(parted)

From the preceding output, we have a long list of commands at our disposal.

Remember to do a backup before making any change(s) to your partition.

Now, to view the current partition table for the /dev/sdb, we would type print:

(parted) print
Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdb: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 5370MB 5369MB primary
2 5370MB 9665MB 4295MB primary
3 9665MB 16.1GB 6441MB primary boot, esp
(parted)

This will print out the partition table for the /dev/sdb. However, we can use the print command with the list option to view all the hard disks available in this system. Let's give it a try:

(parted) print list
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 5370MB 5369MB primary
2 5370MB 9665MB 4295MB primary
3 9665MB 16.1GB 6441MB primary boot, esp
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 20.4GB 20.4GB primary ext4 boot
2 20.4GB 21.5GB 1072MB extended
5 20.4GB 21.5GB 1072MB logical linux-swap(v1)
(parted)

Great! As you can see, the /dev/sda is now also listed. Next, let's look at how we would resize a partition. To accomplish this, we're going to leverage another powerful command, the resizepart command, which in itself is appropriately named.

We will choose the second partition for this exercise; we'll say resizepart 2, and we will reduce it to 2 GB:

 (parted) resizepart
Partition number? 2
End? [5370MB]? 7518
(parted) print
Disk /dev/sdb: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 5370MB 5369MB primary
2 5370MB 7518MB 2148MB primary
3 9665MB 16.1GB 6441MB primary boot, esp
(parted)

From the preceding output, you can see that the parted utility is very powerful. We have effectively taken away 2 GB (roughly) from the second partition. Now, if you think about it, we have 2 GB of free space to use at our disposal.

Hard disk space is critical in a large data center, so bear that in mind when provisioning your servers.

Now, for the purpose of demonstrating how we would use the 2 GB of free space, let's create another partition. The parted utility is  powerful, in that it can recognize partitions that were created from another disk utility such as fdisk. In parted, we would use the mkpart command to create a partition:

(parted)
(parted) mkpart
Partition type? primary/extended?

As you can see by now, there are similarities between fdisk and parted, and they both ask whether the partition is going to be a primary or extended partition. This is vital whenever we're working with operating system installations. For our purposes, we're going to create yet another primary partition:

Partition type?  primary/extended? primary
File system type? [ext2]?
Start?

Now, at this point, we will have to specify the starting size of the partition we are about to create. We will use the size where the second partition ends:

File system type? [ext2]? 
Start? 7518
End? 9665
(parted)

Awesome! Now let's rerun the print command:

(parted) print                                                           
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 5370MB 5369MB primary
2 5370MB 7518MB 2148MB primary
4 7518MB 9665MB 2146MB primary ext2 lba
3 9665MB 16.1GB 6441MB primary boot, esp
(parted)

From the preceding output, we can now see our newly created partition at 2 GB (roughly).

Now we can change the boot flag from its present partition 3 /dev/sdb3 to part partition 4 /dev/sdb4. We would use the set command for this:

(parted) set 
Partition number? 4
Flag to Invert?

From here, we have to tell the parted utility that we want to move the boot flag:

Flag to Invert? boot                                                     
New state? [on]/off?

Now we need to confirm our changes. on is the default, so we press Enter:

New state?  [on]/off?                                                   
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 5370MB 5369MB primary
2 5370MB 7518MB 2148MB primary
4 7518MB 9665MB 2146MB primary ext2 boot, lba
3 9665MB 16.1GB 6441MB primary esp
(parted)

Great! Now we can see that the boot flag has been shifted to the fourth partition /dev/sdb4.

Finally, to save our changes, we can simply type quit:

(parted) quit                                                             
Information: You may need to update /etc/fstab.
root@ubuntu:/home/philip#
You would add entries as needed in the /etc/fstab to automount your partitions to their respective mount points.
主站蜘蛛池模板: 青海省| 崇义县| 大田县| 舟山市| 马公市| 安岳县| 永宁县| 常德市| 新建县| 阿合奇县| 章丘市| 喀喇沁旗| 湖州市| 金华市| 军事| 都江堰市| 浏阳市| 临沭县| 水城县| 克什克腾旗| 汉沽区| 双辽市| 河津市| 陆河县| 太湖县| 襄汾县| 九龙坡区| 抚州市| 芜湖县| 射阳县| 万全县| 贵德县| 玉山县| 东乌珠穆沁旗| 莱州市| 平乐县| 龙井市| 安多县| 宜黄县| 井研县| 大洼县|