LinuxCommandLibrary

swaplabel

Create or modify a swap space label

TLDR

Display the current label and UUID of a swap area

$ swaplabel [path/to/file]
copy

Set the label of a swap area
$ swaplabel [[-L|--label]] [new_label] [path/to/file]
copy

Set the UUID of a swap area (you can generate a UUID using uuidgen)
$ swaplabel [[-U|--uuid]] [new_uuid] [path/to/file]
copy

SYNOPSIS

swaplabel [options] device

PARAMETERS

device
    The block device representing the swap area (e.g., /dev/sda2, /dev/nvme0n1p3). This is a mandatory argument.

-L
    Specifies a new label for the swap area. The label can be up to 16 characters long.

-U , --uuid
    Specifies a new UUID for the swap area. The UUID must be a valid 36-character UUID string (e.g., '12345678-abcd-ef01-2345-6789abcdef01').

-e, --erase-all
    Erases both the label and UUID from the swap area's header. This makes the swap area identifiable only by its device path.

-f, --force
    Forces the operation to proceed, even if the specified device is mounted or in use. Use this option with extreme caution as it can lead to data corruption or system instability.

-v, --verbose
    Enables verbose output, providing more details about the operation being performed.

-h, --help
    Displays a help message with command usage information and exits.

-V, --version
    Displays the version information of the swaplabel utility and exits.

DESCRIPTION

swaplabel is a utility from the util-linux package used to display or modify the label and Universal Unique Identifier (UUID) of a Linux swap area. Unlike mkswap, which initializes a new swap partition from scratch, swaplabel operates on an existing swap area without reformatting it, preserving its contents. This command is particularly useful for identifying swap partitions by their human-readable labels or unique UUIDs.

Common use cases include:

1. Querying Information: When invoked without any options, swaplabel displays the current label and UUID of the specified swap device. This helps in verifying the metadata of a swap partition.
2. Changing Metadata: Users can assign a new label or UUID to a swap area. This is often necessary when cloning a disk, migrating a system, or fixing an /etc/fstab entry that relies on a specific label or UUID. If an fstab entry points to a non-existent or conflicting UUID/label, swaplabel can correct the swap partition's metadata to match.
3. Erasing Metadata: The command also provides an option to erase both the label and UUID, effectively removing these identifiers from the swap header.

It's crucial to use swaplabel with caution, especially when the swap device is active, as incorrect modifications can lead to system instability or render the swap area unusable until corrected in /etc/fstab. It's generally recommended to deactivate the swap (swapoff) before making any changes.

CAVEATS

Data Loss Risk: Modifying the label or UUID of an active swap area can lead to system instability or data corruption. Always deactivate the swap device using swapoff before making changes, if possible.

/etc/fstab Consistency: If your system uses labels or UUIDs in /etc/fstab to identify swap partitions, any changes made with swaplabel must be manually updated in /etc/fstab to ensure the swap area is correctly recognized and activated on subsequent boots.

Permissions: swaplabel typically requires root privileges to operate on block devices.

COMMON USE CASE: UPDATING /ETC/FSTAB

If you clone a disk or move a system, and the original swap partition's UUID conflicts with another or is simply unknown, you might face issues activating swap on boot. swaplabel can resolve this.

1. Identify the swap partition: Use lsblk -f or blkid to find your swap partition (e.g., /dev/sdb2) and its current UUID/label.
2. Deactivate swap: sudo swapoff /dev/sdb2
3. Generate a new UUID (optional but recommended): sudo swaplabel -U $(uuidgen) /dev/sdb2 (or use -L for a label).
4. Get the new UUID/label: sudo blkid /dev/sdb2
5. Update /etc/fstab: Edit /etc/fstab to replace the old UUID/label with the new one. For example, change UUID=old-uuid-here none swap sw 0 0 to UUID=new-uuid-here none swap sw 0 0.
6. Activate swap: sudo swapon -a or sudo swapon /dev/sdb2
7. Verify: cat /proc/swaps to confirm the swap is active with the correct identifier.

HISTORY

swaplabel is a part of the util-linux project, a standard collection of miscellaneous utilities for Linux. Its functionality complements other swap-related tools like mkswap and swapon by providing a mechanism to modify the metadata (label and UUID) of an existing swap area without requiring a full reformat. This capability became particularly important as Linux systems increasingly adopted UUIDs and labels for more robust and flexible device identification in configuration files like /etc/fstab, allowing systems to cope better with changes in device paths (e.g., /dev/sda1 vs. /dev/sdb1). Its primary development and maintenance are handled within the util-linux project, ensuring its continued relevance and integration with core Linux functionalities.

SEE ALSO

mkswap(8), swapon(8), swapoff(8), blkid(8), fstab(5)

Copied to clipboard