Working with Parted – Linux Partition tool
1. Introduction to Partitioning
Parted is a program to manipulate disk partitions. It supports multiple partition table formats, including MS-DOS and GPT. It is useful for creating space for new operating systems, reorganising disk usage, and copying data to new hard disks.
2 Using GNU Parted
Parted has two modes: command line and interactive. Parted should always be started with:
# parted DEVICE
where DEVICE is the hard disk device to edit. (If you’re lazy and omit the DEVICE argument, Parted will attempt to guess which device you want.)
In command line mode, this is followed by one or more commands.
For example:
# parted /dev/sda mklabel gpt mkpart P1 ext3 1MiB 8MiB
Options (like ‘–help’) can only be specified on the command line.
In interactive mode, commands are entered one at a time at a prompt, and modify the disk immediately. For example:
(parted) mklabel gpt
(parted) mkpart P1 ext3 1MiB 8MiB
Unambiguous abbreviations are allowed. For example, you can type “p” instead of “print”, and “u” instead of “units”. Commands can be typed either in English, or your native language (if your language has been translated). This may create ambiguities. Commands are case-insensitive.
Numbers indicating partition locations can be whole numbers or decimals. The suffix selects the unit, which may be one of those described in *note unit::, except CHS and compact. If no suffix is given, then the default unit is assumed. Negative numbers count back from the end of the disk, with “-1s” indicating the sector at the end of the disk.
Parted will compute sensible ranges for the locations you specify (e.g. a range of +/- 500 MB when you specify the location in “G”). Use the sector unit “s” to specify exact locations. With parted-2.4 and newer, IEC binary units like “MiB”, “GiB”, “TiB”, etc.,
specify exact locations as well.
If you don’t give a parameter to a command, Parted will prompt you for it. For example:
(parted) mklabel
New disk label type? gpt
Parted will always warn you before doing something that is potentially dangerous, unless the command is one of those that is inherently dangerous (viz., rm, mklabel and mkpart). Since many partitioning
systems have complicated constraints, Parted will usually do something slightly different to what you asked. (For example, create a partition starting at 10.352Mb, not 10.4Mb) If the calculated values differ too
much, Parted will ask you for confirmation.
3. Command Line Options
When invoked from the command line, Parted supports the following
syntax:
# parted [OPTION] DEVICE [COMMAND [ARGUMENT]]
Available options and commands follow. For detailed explanations of the use of Parted commands, Options begin with a hyphen, commands do not:
Options:
‘-h’ | ‘–help’
display a help message‘-s’ | ‘–script’
never prompt the user‘-a alignment-type’ | ‘–align alignment-type’
Set alignment for newly created partitions, valid alignment types are: none, cylinder, minimal and optimal.‘-v’ | ‘–version’
display the version
4 Parted Session Commands
GNU Parted provides the following commands:
* Menu:
* align-check::
* disk_set::
* help::
* mklabel::
* mkpart::
* name::
* print::
* quit::
* rescue::
* rm::
* select::
* set::
* unit::
Note that after version 2.4, the following commands were removed: check, cp, mkfs, mkpartfs, move, resize.
4.1 align-check
Determine whether the starting sector of partition N meets the disk’s selected alignment criteria. ALIGN-TYPE must be ‘minimal’,’optimal’ or an abbreviation. When in script mode, if the
partition does not meet the alignment requirement, exit with status 1; otherwise (including on older kernels for which alignment data is not available), continue processing any remaining commands.
Without ‘–script’, print either ‘N aligned’ or ‘N not aligned’.
Example:
(parted) align-check minimal 1
1 aligned
4.2 disk_set
Changes a flag on the disk. A flag can be either “on” or “off”. Some or all of these flags will be available, depending on what disk label you are using:
‘pmbr_boot’
(GPT) – this flag enables the boot flag on the GPT’s protective MBR partition.
The disk’s flags are displayed by the print command on the “Disk Flags:” line. They are also output as the last field of the disk information in machine mode.
(parted) disk_set pmbr_boot on
Set the PMBR’s boot flag.
4.3 help
Prints general help, or help on COMMAND.
Example:
(parted) help mklabel
Print help for the mklabel command.
4.4 mklabel
Creates a new disk label, of type LABEL-TYPE. The new disk label will have no partitions. This command (normally) won’t technically destroy your data, but it will make it basically unusable, and you
will need to use the rescue command to recover any partitions. Parted works on all partition tables.
LABEL-TYPE must be one of these supported disk labels:
* bsd
* loop (raw disk access)
* gpt
* mac
* msdos
* pc98
* sun
Example:
(parted) mklabel msdos
Create an MS-DOS disk label. This is still the most common disk label for PCs.
4.5 mkpart
Creates a new partition, _without_ creating a new file system on that partition. This is useful for creating partitions for file systems (or LVM, etc.) that Parted doesn’t support. You may
specify a file system type, to set the appropriate partition code in the partition table for the new partition. FS-TYPE is required for data partitions (i.e., non-extended partitions). START and END
are the offset from the beginning of the disk, that is, the “distance” from the start of the disk.
PART-TYPE is one of ‘primary’, ‘extended’ or ‘logical’, and may be specified only with ‘msdos’ or ‘dvh’ partition tables. A NAME must be specified for a ‘gpt’ partition table. Neither PART-TYPE nor
NAME may be used with a ‘sun’ partition table.
FS-TYPE must be one of these supported file systems:
* ext2
* fat16, fat32
* hfs, hfs+, hfsx
* linux-swap
* NTFS
* reiserfs
* ufs
* btrfs
Example:
(parted) mkpart logical 0.0 692.1
Create a logical partition that will contain an ext2 file system. The partition will start at the beginning of the disk, and end 692.1 megabytes into the disk.
4.6 name
Sets the name for the partition NUMBER (GPT, Mac, MIPS and PC98 only). The name can be placed in quotes.
Example:
(parted) name 2 ‘Secret Documents’
Set the name of partition 2 to ‘Secret Documents’.
4.7 print
Displays the partition table on the device parted is editing, or detailed information about a particular partition.
Example:
(parted) print
Disk geometry for /dev/hda: 0.000-2445.679 megabytes
Disk label type: msdos
Minor Start End Type Filesystem Flags
1 0.031 945.000 primary fat32 boot, lba
2 945.000 2358.562 primary ext2
3 2358.562 2445.187 primary linux-swap
(parted) print 1
Minor: 1
Flags: boot, lba
File System: fat32
Size: 945.000Mb (0%)
Minimum size: 84.361Mb (0%)
Maximum size: 2445.679Mb (100%)
4.8 quit
Quits Parted.
It is only after Parted exits that the Linux kernel knows about the changes Parted has made to the disks. However, the changes caused by typing your commands will _probably_ be made to the disk immediately after typing a command. However, the operating system’s cache and the disk’s hardware cache may delay this.
4.9 rescue
Rescue a lost partition that used to be located approximately between START and END. If such a partition is found, Parted will ask you if you want to create a partition for it. This is useful if you accidently deleted a partition with parted’s rm command, for example.
Example:
(parted) print
Disk geometry for /dev/hdc: 0.000-8063.507 megabytes
Disk label type: msdos
Minor Start End Type Filesystem Flags
1 0.031 8056.032 primary ext3
(parted) rm
Partition number? 1
(parted) print
Disk geometry for /dev/hdc: 0.000-8063.507 megabytes
Disk label type: msdos
Minor Start End Type Filesystem Flags
OUCH! We deleted our ext3 partition!!! Parted comes to the rescue…
(parted) rescue
Start? 0
End? 8056
Information: A ext3 primary partition was found at 0.031MB ->
8056.030MB. Do you want to add it to the partition table?
Yes/No/Cancel? y
(parted) print
Disk geometry for /dev/hdc: 0.000-8063.507 megabytes
Disk label type: msdos
Minor Start End Type Filesystem Flags
1 0.031 8056.032 primary ext3
It’s back! :)
4.10 rm
Removes the partition with number NUMBER. If you accidently delete a partition with this command, use mkpart to recover it. Also, you can use the gpart program (*note Related information::) to recover damaged disk labels.
Note for msdos disk labels: if you delete a logical partition, all logical partitions with a larger partition number will be renumbered. For example, if you delete a logical partition with a partition number of 6, then logical partitions that were number 7,8 and 9 would be renumbered to 6, 7 and 8 respectively. This means, for example, that you have to update ‘/etc/fstab’ on GNU/Linux systems.
Example:
(parted) rm 3
Remove partition 3.
4.11 select
Selects the device, DEVICE, for Parted to edit. The device can be a Linux hard disk device, a partition, a software RAID device or LVM logical volume.
Example:
(parted) select /dev/hdb
Select ‘/dev/hdb’ (the slave device on the first ide controller on Linux) as the device to edit.
4.12 set
Changes a flag on the partition with number NUMBER. A flag can be either “on” or “off”. Some or all of these flags will be available, depending on what disk label you are using:
‘bios_grub’
(GPT) – Enable this to record that the selected partition is a GRUB BIOS partition.‘legacy_boot’
(GPT) – this flag is used to tell special purpose software that the GPT partition may be bootable.‘boot’
(Mac, MS-DOS, PC98) – should be enabled if you want to boot off the partition. The semantics vary between disk labels. For MS-DOS disk labels, only one partition can be bootable. If you are installing LILO on a partition that partition must be bootable. For PC98 disk labels, all ext2 partitions must be bootable (this is enforced by Parted).‘lba’
(MS-DOS) – this flag can be enabled to tell MS DOS, MS Windows 9x and MS Windows ME based operating systems to use Linear (LBA) mode.‘root’
(Mac) – this flag should be enabled if the partition is the root device to be used by Linux.‘swap’
(Mac) – this flag should be enabled if the partition is the swap device to be used by Linux.‘hidden’
(MS-DOS, PC98) – this flag can be enabled to hide partitions from Microsoft operating systems.‘raid’
(MS-DOS) – this flag can be enabled to tell linux the partition is a software RAID partition.‘LVM’
(MS-DOS) – this flag can be enabled to tell linux the partition is a physical volume.‘PALO’
(MS-DOS) – this flag can be enabled so that the partition can be used by the Linux/PA-RISC boot loader, palo.‘PREP’
(MS-DOS, GPT) – this flag can be enabled so that the partition can be used as a PReP boot partition on PowerPC PReP or IBM RS6K/CHRP hardware.‘DIAG’
(MS-DOS) – Enable this to indicate that a partition can be used as a diagnostics / recovery partition.
The print command displays all enabled flags for each partition.
Example:
(parted) set 1 boot on
Set the ‘boot’ flag on partition 1.
4.13 unit
Selects the current default unit that Parted will use to display locations and capacities on the disk and to interpret those given by the user if they are not suffixed by an UNIT.
UNIT may be one of:
‘s’
sector (n bytes depending on the sector size, often 512)‘B’
byte‘KiB’
kibibyte (1024 bytes)‘MiB’
mebibyte (1048576 bytes)‘GiB’
gibibyte (1073741824 bytes)‘TiB’
tebibyte (1099511627776 bytes)‘kB’
kilobyte (1000 bytes)‘MB’
megabyte (1000000 bytes)‘GB’
gigabyte (1000000000 bytes)‘TB’
terabyte (1000000000000 bytes)‘%’
percentage of the device (between 0 and 100)‘cyl’
cylinders (related to the BIOS CHS geometry)‘chs’
cylinders, heads, sectors addressing (related to the BIOS CHS geometry)‘compact’
This is a special unit that defaults to megabytes for input,and picks a unit that gives a compact human readable representation for output.
The default unit apply only for the output and when no unit is specified after an input number. Input numbers can be followed by an unit (without any space or other character between them), in which case this unit apply instead of the default unit for this particular number, but CHS and cylinder units are not supported as a suffix. If no suffix is given, then the default unit is assumed.
Parted will compute sensible ranges for the locations you specify (e.g. a range of +/- 500 MB when you specify the location in “G”,and a range of +/- 500 KB when you specify the location in “M”) and will select the nearest location in this range from the one you wrote that satisfies constraints from both the operation, the filesystem being worked on, the disk label, other partitions and so
on. Use the sector unit “s” to specify exact locations (if they do not satisfy all constraints, Parted will ask you for the nearest solution). Note that negative numbers count back from the end of the disk, with “-1s” pointing to the last sector of the disk.
Note that as of parted-2.4, when you specify start and/or end values using IEC binary units like “MiB”, “GiB”, “TiB”, etc.,parted treats those values as exact, and equivalent to the same number specified in bytes (i.e., with the “B” suffix), in that it provides _no_ “helpful” range of sloppiness. Contrast that with a partition start request of “4GB”, which may actually resolve to
some sector up to 500MB before or after that point. Thus, when creating a partition, you should prefer to specify units of bytes (“B”), sectors (“s”), or IEC binary units like “MiB”, but not “MB”, “GB”, etc.
Example:
(parted) unit compact
(parted) print
Disk geometry for /dev/hda: 0kB – 123GB
Disk label type: msdos
Number Start End Size Type File system Flags
1 32kB 1078MB 1077MB primary reiserfs boot
2 1078MB 2155MB 1078MB primary linux-swap
3 2155MB 123GB 121GB extended
5 2155MB 7452MB 5297MB logical reiserfs
(parted) unit chs print
Disk geometry for /dev/hda: 0,0,0 – 14946,225,62
BIOS cylinder,head,sector geometry: 14946,255,63. Each cylinder
is 8225kB.
Disk label type: msdos
Number Start End Type File system Flags
1 0,1,0 130,254,62 primary reiserfs boot
2 131,0,0 261,254,62 primary linux-swap
3 262,0,0 14945,254,62 extended
5 262,2,0 905,254,62 logical reiserfs
(parted) unit mb print
Disk geometry for /dev/hda: 0MB – 122942MB
Disk label type: msdos
Number Start End Size Type File system Flags
1 0MB 1078MB 1077MB primary reiserfs boot
2 1078MB 2155MB 1078MB primary linux-swap
3 2155MB 122935MB 120780MB extended
5 2155MB 7452MB 5297MB logical reiserfs
1 Response
[…] Working with Parted – Linux Partition tool […]