5

I'm tring to add flag swap with parted but got invalid token,why?

Model: Kingston DataTraveler 3.0 (scsi)
Disk /dev/sda: 15.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End     Size    Type     File system  Flags
 1      512B   2000MB  2000MB  primary  ext4         boot, lba

(parted) set                                                              
Partition number? 1                                                       
Flag to Invert? swap                                                      
parted: invalid token: swap

2 Answers2

4

While help set (as well as the man page) just gives you an uncommented list of flags, it does not tell you which flags are supported by which partition scheme.

The invalid token message seems to be just what you get when a flag is unavailable. There should be a more user friendly error message.

In parted 3.2, for GPT partitions, the flags swap, root, lba are unavailable:

static int
gpt_partition_is_flag_available (const PedPartition *part,
                                 PedPartitionFlag flag)
{
  switch (flag)
    {
    case PED_PARTITION_RAID:
    case PED_PARTITION_LVM:
    case PED_PARTITION_BOOT:
    case PED_PARTITION_BIOS_GRUB:
    case PED_PARTITION_HPSERVICE:
    case PED_PARTITION_MSFT_RESERVED:
    case PED_PARTITION_MSFT_DATA:
    case PED_PARTITION_DIAG:
    case PED_PARTITION_APPLE_TV_RECOVERY:
    case PED_PARTITION_HIDDEN:
    case PED_PARTITION_LEGACY_BOOT:
    case PED_PARTITION_PREP:
    case PED_PARTITION_IRST:
    case PED_PARTITION_ESP:
      return 1;
    case PED_PARTITION_SWAP:
    case PED_PARTITION_ROOT:
    case PED_PARTITION_LBA:
    default:
      return 0;
    }
  return 0;
}

MSDOS only supports these flags:

static int
msdos_partition_is_flag_available (const PedPartition* part,
                                   PedPartitionFlag flag)
{
        switch (flag) {
        case PED_PARTITION_HIDDEN:
                if (part->type == PED_PARTITION_EXTENDED)
                        return 0;
                else
                        return 1;

        case PED_PARTITION_BOOT:
        case PED_PARTITION_RAID:
        case PED_PARTITION_LVM:
        case PED_PARTITION_LBA:
        case PED_PARTITION_PALO:
        case PED_PARTITION_PREP:
        case PED_PARTITION_IRST:
        case PED_PARTITION_ESP:
        case PED_PARTITION_DIAG:
                return 1;

        default:
                return 0;
        }
}

So, what's up with the swap flag?

Turns out it's supported by DVH:

static int
dvh_partition_is_flag_available (const PedPartition* part,
                                  PedPartitionFlag flag)
{
        switch (flag) {
        case PED_PARTITION_ROOT:
        case PED_PARTITION_SWAP:
        case PED_PARTITION_BOOT:
                return 1;

...as well as MAC partitions:

static int
mac_partition_is_flag_available (
        const PedPartition* part, PedPartitionFlag flag)
{
        switch (flag) {
        case PED_PARTITION_BOOT:
        case PED_PARTITION_ROOT:
        case PED_PARTITION_SWAP:
        case PED_PARTITION_LVM:
        case PED_PARTITION_RAID:
                return 1;

The good news is that Linux doesn't really care much about partition types in the first place. So you can use any partition for swap, regardless whether the partition table says so.


According to the code above, for msdos partition label, you cannot set hidden flag if the partition is extended. This is a good way to test the error message given by parted:

(parted) mklabel msdos               # new dos partition
(parted) mkpart primary 1MiB 2MiB    # primary
(parted) mkpart extended 2MiB 10MiB  # extended
(parted) mkpart logical 3MiB 4MiB    # logical
(parted) set 1 hidden on             # OK
(parted) set 2 hidden on             # FAIL
parted: invalid token: hidden        # very helpful message
Flag to Invert?                      # I didn't mistype
(parted) set 5 hidden on             # OK

So there we are. Unsupported flags just get invalid token message, and kind of implies you mistyped it or something, but you didn't do anything wrong, it's just parted not supporting these flags in some cases.

frostschutz
  • 48,978
  • awesome! Just wondering, what was your train of thought to get to the core: source code!! Could you please share? – Ashfaq Jun 14 '19 at 08:41
  • 1
    The info page says "swap (MS-DOS, GPT, Mac)", but that's clearly inconsistent with the code! – Toby Speight Jul 15 '22 at 13:13
0

A partition either contains a file system, in this case ext4, or it contains swap space and no file system. So it doesn't make sense to add the swap flag to a file system partition.

In case you want to create a swap file on the file system later, there is no need to add the flag to the partition. You just create the file and use it for swap.

RalfFriedl
  • 8,981