pvcreate Command in Linux



The pvcreate command in Linux initializes a physical volume (PV). It prepares a Physical Volume (PV) for use with the Logical Volume Manager (LVM). A Physical Volume can be a disk partition, an entire disk, a loopback file, or a meta device. For DOS disk partitions, set the partition ID to 0x8E using fdisk, cfdisk, or an equivalent tool. For entire disk devices, erase the partition table to remove existing data. This can be done by zeroing the first sector with the dd tool.

Table of Contents

Here is a comprehensive guide to the options available with the pvcreate command −

Syntax of pvcreate Command

The syntax of the pvcreate command in Linux is as follows −

pvcreate [options] [device…]

In the above syntax, the [options] field is used to specify the options to modify the command’s output. The [device...] field is used to specify one or more block devices (example: /dev/sdX, /dev/sdY) to be initialized as physical volumes.

pvcreate Command Options

The options of the pvcreate command are listed below −

Flag Option Description
--bootloaderareasize Size[m|UNIT] Reserves space for the bootloader before the first PE. Alignment rules may increase the actual size.
--commandprofile String Specifies a command profile for configuration.
--dataalignment Size[k|UNIT] Aligns the start of the PV data area to a multiple of the specified size.
--dataalignmentoffset Size[k|UNIT] Shifts in the PV data area start with the specified offset.
-d --debug Increases debug level (1-6) for detailed logging.
--devices PV Limits the command to specific devices. Accepts multiple values.
--devicesfile String Specifies a file listing devices for LVM. Managed via lvmdevices.
--driverloaded y|n Disables device-mapper usage if set to n.
-f --force Overrides various checks and protections. Use with caution.
-h --help Displays command usage help text.
--journal String Logs additional command information in the systemd journal.
--labelsector Number Changes the sector used for LVM labels (default: sector 1).
--lockopt String Passes options for special cases to lvmlockd.
--longhelp Displays extended help text.
--metadataignore y|n Determines whether LVM stores metadata on the PV.
--metadatasize Size[m|UNIT] Defines the approximate size allocated for VG metadata.
-M lvm2 --metadatatype lvm2 Specifies the on-disk metadata format (default: lvm2).
--nohints Disables use of hints for locating PVs.
--nolocking Disables locking mechanisms.
--norestorefile Allows specifying a UUID without requiring a backup restore file.
--profile String Alias for --commandprofile or --metadataprofile.
--pvmetadatacopies 0|1|2 Defines the number of VG metadata copies stored on the PV.
-q --quiet Suppresses output and logs. Repeat to also suppress prompts.
--reportformat basic|json Sets output format (default: basic; alternative: JSON).
--restorefile String Uses metadata from a backup file (vgcfgbackup) for consistency.
--setphysicalvolumesize Size[m|UNIT] Manually sets the PV size. Use with caution.
-t --test Runs in test mode without updating metadata.
-u String --uuid String Assigns a specific UUID to the PV. Required for metadata restoration.
-v --verbose Increases verbosity (up to 4 levels).
--version Displays version information.
-y --yes Automatically confirms all prompts with yes.
-Z y|n --zero y|n Wipes the first 4 sectors (2048 bytes) unless --restorefile or --uuid is used.

Examples of pvcreate Command in Linux

This section will discuss the usage of the pvcreate command in Linux with examples.

  • Initializing Physical Volume (PV) on a Disk Partition
  • Forcefully Initializing Physical Volume (PV) on a Disk Partition
  • Specifying the UUID for Restoring Physical Volume (PV)
  • Wiping the First 4 Sector before Physical Volume (PV) Creation
  • Running the pvcreate Command in Test Mode
  • Displaying Usage Help

Initializing Physical Volume (PV) on a Disk Partition

To initialize a partition as a Physical Volume (PV) for use in LVM, use the pvcreate command in the following manner −

sudo pvcreate /dev/vda3

Before initializing the PV, ensure the partition is unmounted otherwise the pvcreate will throw an error as shown in the following image −

pvcreate Command in Linux1

To unmount a partition, use the following command −

sudo umount /dev/vda3

Use the mount command to check whether a partition is mounted. For example, to check whether the /dev/vda3 is mounted or not use the following command −

mount | grep /dev/vda3

Forcefully Initializing Physical Volume (PV) on a Disk Partition

To forcefully initialize the physical volume (PV) on a disk partition, use the -f or --force option −

sudo pvcreate -f /dev/vda3

The -ff option in pvcreate is used to force the initialization of a physical volume, even if it belongs to an existing volume group. It overrides safety checks and is used in emergency situations.

sudo pvcreate -ff /dev/vda3
pvcreate Command in Linux2

The -f skips confirmation prompts but does not allow reinitializing a PV that belongs to an active volume group. The -ff forces reinitialization even if the PV is part of an existing volume group.

Caution − The -ff option forces reinitialization and can cause data loss by erasing metadata. Use with extreme care.

Specifying the UUID for Restoring Physical Volume (PV)

To specify a custom UUID when restoring a PV, use the --uuid option with the pvcreate command −

sudo pvcreate --uuid "12345678-85ab-cdef-7896-567890abcdef" --norestorefile /dev/vda3

To restore the PV, using an existing restore file, use the --restorefile option with the file path.

sudo pvcreate --uuid "12345678-85ab-cdef-7896-567890abcdef" --restorefile /path/to/restore_file /dev/vda3

Wiping the First 4 Sector before Physical Volume (PV) Creation

To remove the first 4 sectors or 2048 bytes before PV creation, use the -Z or --zero option −

sudo pvcreate -Z y /dev/vda3

The -Z option wipes the first 4 sectors (2048 bytes) of a device to remove existing LVM metadata or filesystem signatures, ensuring the device is clean before initializing it as a PV.

Running the pvcreate Command in Test Mode

To dry run the pvcreate command, use the -t or --test option −

sudo pvcreate -t /dev/vda3

Displaying Usage Help

To display usage help of the pvcreate command, use the -h or --help option −

pvcreate -h

To display the detailed help, use the --longhelp option −

pvcreate --longhelp

Conclusion

The pvcreate command in Linux is used to initialize a physical volume (PV) for the Logical Volume Manager (LVM). It prepares disk partitions, entire disks, loopback files, or meta devices for use with LVM. The command offers various options for customization, including setting metadata size, specifying UUIDs, and forcefully initializing PVs. It also provides safety measures, such as requiring confirmation for reinitialization. Use force options carefully to prevent data loss.

Advertisements