- CompTIA Linux+ Certification Guide
- Philip Inshanally
- 1113字
- 2021-08-13 15:49:50
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.
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.
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.
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#
- 大學(xué)計(jì)算機(jī)信息技術(shù)導(dǎo)論
- GNU-Linux Rapid Embedded Programming
- Microsoft Power BI Quick Start Guide
- 手把手教你學(xué)AutoCAD 2010
- Cloud Analytics with Microsoft Azure
- 腦動(dòng)力:PHP函數(shù)速查效率手冊(cè)
- UTM(統(tǒng)一威脅管理)技術(shù)概論
- 快學(xué)Flash動(dòng)畫(huà)百例
- Visual C# 2008開(kāi)發(fā)技術(shù)詳解
- Maya 2012從入門(mén)到精通
- 現(xiàn)代機(jī)械運(yùn)動(dòng)控制技術(shù)
- 網(wǎng)絡(luò)化分布式系統(tǒng)預(yù)測(cè)控制
- Ruby on Rails敏捷開(kāi)發(fā)最佳實(shí)踐
- 內(nèi)模控制及其應(yīng)用
- TensorFlow Reinforcement Learning Quick Start Guide