Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Formatting partition with KPMcore

Formatting partition with KPMcore

Scheduled Pinned Locked Moved Unsolved 3rd Party Software
2 Posts 1 Posters 91 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lenatrice
    wrote on 9 Mar 2025, 00:30 last edited by lenatrice 3 Sept 2025, 01:25
    #1

    I'm using KPM core to manage partitions, and I was able to successfully implement functions to create partition, delete partition and recreate the partition table. However, when I create the partitions, they are not formatted with any type. When I run lsblk -f or blkid [partition_node], they don't show any type for the newly created partitions. Can someone help me understand what is going wrong?

    void PartitionPage::onCreateSystemPartitionsButtonClicked(bool checked)
    {
        // Get currently selected Partition pointer and determine it is valid unallocated space for installation.
    
        Partition* partition = getSelectedPartition();
    
        if (!partition)
        {
            qWarning() << "Selected Partition is null";
            return;
        }
    
        if (partition->roles().has(PartitionRole::Unallocated))
        {
            qDebug() << "Selected valid free space for installation";
        } else {
            qWarning() << "Selected partition is not free space";
            return;
        }
    
        // Get currently selected Device pointer.
    
        Device* device = getSelectedDevice();
    
        if (!device)
        {
            qWarning() << "Selected Device is null";
            return;
        }
    
        // If selected space has less than 10GiB, issue a warning.
    
        if (static_cast<double>(partition->capacity()) / GiB < 10)
        {
            QMessageBox::StandardButton warningDialog;
            warningDialog = QMessageBox::warning(this, "Attention", "The selected free space of " + getSize(partition) + " may be insufficient to install the system, " + 
            "or may cause problems as more space is required in the future. It's recommended that you select a free space larger than 10GiB.\n\nContinue?", QMessageBox::Ok | QMessageBox::Cancel);
    
            if (warningDialog == QMessageBox::Cancel) return;
        }
    
        // Get partition table of the device
        PartitionTable* devicePartitionTable = device->partitionTable();
    
        // MBR partition table support only 4 primary partitions. Issue an error if it contains more than 2 partitions, as it will not be possible to create the necessary partitions for the system to run.
        if (devicePartitionTable->typeName() == "msdos" && countPrimaryPartitions(device) > 2)
        {
            QMessageBox::StandardButton errorDialog;
            errorDialog = QMessageBox::critical(this, "Error", "MBR partition tables support a maximum of 4 primary partitions, " + 
                QString("and it will not be possible to create the new 2 partitions necessary for the system because you already have ") + QString::number(countPrimaryPartitions(device)) + " partitions in this device." + 
                "Consider deleting existing partitions or select another device.", QMessageBox::Ok
            );
            return;
        }
        
        // Create the filesystems
        qint64 firstBootSector = partition->firstSector();
        qint64 lastBootSector = partition->firstSector() + (MiB * 256) / partition->sectorSize() - 1;
        qint64 firstRootSector = lastBootSector + 1;
        qint64 lastRootSector = partition->lastSector();
    
    
        FileSystem* newBootFs = FileSystemFactory::create(FileSystem::Type::Fat32,
            firstBootSector, lastBootSector, device->logicalSize()
        );
            
        FileSystem* newRootFs = FileSystemFactory::create(FileSystem::Type::Ext4,
            firstRootSector, lastRootSector, device->logicalSize()
        );
        
        // Get available flags from the partition table
        PartitionTable::Flags availableFlags;
        for (PartitionTable::Flag _flag : devicePartitionTable->flagList())
        {
            availableFlags |= _flag;
        }
    
        // Create the partitions
        QString bootDeviceNode = determineNewPartitionNodePath(device->deviceNode(), countPrimaryPartitions(device) + 1);
        QString rootDeviceNode = determineNewPartitionNodePath(device->deviceNode(), countPrimaryPartitions(device) + 2);
        
        Partition* newBootPartition = new Partition(
            devicePartitionTable,                     // PartitionTable
            *device,                                  // Device
            PartitionRole(PartitionRole::Primary),    // Partition Role
            newBootFs,                                // Filesystem 
            firstBootSector, lastBootSector,          // Size of 256 MiB
            bootDeviceNode,                           // Partition node path
            availableFlags,                           // Available flags
            "/mnt/new_root/boot",                     // Mount point
            false,                                    // Not mounted
            PartitionTable::Flag::Boot                // Bootable flag
        );
        
        Partition* newRootPartition = new Partition(
            devicePartitionTable,                     // PartitionTable
            *device,                                  // Device
            PartitionRole(PartitionRole::Primary),    // Partition Role
            newRootFs,                                // Filesystem 
            firstRootSector, lastRootSector,          // Remaining size of the free space [TODO: add option to use less space]
            rootDeviceNode,                           // Partition node path
            availableFlags,                           // Available flags
            "/mnt/new_root/",                         // Mount point
            false,                                    // Not mounted
            PartitionTable::Flag::None                // No flag
        );
        
        // Show an error message if the space is insufficient
        if (newBootPartition->capacity() + newRootPartition->capacity() > partition->capacity())
        {
            QMessageBox::StandardButton errorDialog;
            errorDialog = QMessageBox::critical(this, "Error", "The selected free space is insufficient to install the system.\n"
                + QString("(Space occupied by the new partitions:") + QString::number(static_cast<double>(newBootPartition->capacity() + newRootPartition->capacity()) / GiB) + " GiB)", QMessageBox::Ok
            );
            return;
        }
        
        if (!createBootPartition->canCreateNew(partition))
        {
            qWarning() << "Cannot create new partition";
            return;
        }
    
        // Set partition type for the new partitions
        if (devicePartitionTable->typeName() == "gpt")
        {
            qDebug() << "Setting partition type for GPT table";
            newBootPartition->setType("C12A7328-F81F-11D2-BA4B-00A0C93EC93B"); // GPT GUID for EFI System Partition
            newRootPartition->setType("0FC63DAF-8483-4772-8E79-3D69D8477DE4"); // GPT GUID for Linux Filesystem
        } else if (devicePartitionTable->typeName() == "msdos") {
            qDebug() << "Setting partition type for MBR table";
            newBootPartition->setType("0xEF"); // MBR type code for EFI System Partition
            newRootPartition->setType("0x83"); // MBR type code for Linux Filesystem
        }
    
        NewOperation* createBootPartition = new NewOperation(*device, newBootPartition);
        NewOperation* createRootPartition = new NewOperation(*device, newRootPartition);
    
        qDebug() << "Pushing the operations to create partitions to the operation stack";
    
        operationStack->push(createBootPartition);
        operationStack->push(createRootPartition);
    
        runOperations();
    
        updatePartitionTable();
    }
    

    I'm thinking of just running system commands for mkfs.fat -F32 /dev/sdb1 and mkfs.ext4 /dev/sdb2, but it would be good to understand why the filesystems are not correctly formatted so I can understand this library better.

    PS: I tried without this before, added this part while trying to solve the problem

       // Set partition type for the new partitions
       if (devicePartitionTable->typeName() == "gpt")
       {
           qDebug() << "Setting partition type for GPT table";
           newBootPartition->setType("C12A7328-F81F-11D2-BA4B-00A0C93EC93B"); // GPT GUID for EFI System Partition
           newRootPartition->setType("0FC63DAF-8483-4772-8E79-3D69D8477DE4"); // GPT GUID for Linux Filesystem
       } else if (devicePartitionTable->typeName() == "msdos") {
           qDebug() << "Setting partition type for MBR table";
           newBootPartition->setType("0xEF"); // MBR type code for EFI System Partition
           newRootPartition->setType("0x83"); // MBR type code for Linux Filesystem
       }
    
    1 Reply Last reply
    0
    • L Offline
      L Offline
      lenatrice
      wrote on 9 Mar 2025, 01:41 last edited by
      #2

      Found the solution. Added this to the end of the function and now it the partitions filesystems are correctly formatted:

          newBootFs->create(*rootReport, bootDeviceNode);
          newRootFs->create(*rootReport, rootDeviceNode);
      

      Also I could delete this:

          if (devicePartitionTable->typeName() == "gpt")
          {
              qDebug() << "Setting partition type for GPT table";
              newBootPartition->setType("C12A7328-F81F-11D2-BA4B-00A0C93EC93B"); // GPT GUID for EFI System Partition
              newRootPartition->setType("0FC63DAF-8483-4772-8E79-3D69D8477DE4"); // GPT GUID for Linux Filesystem
          } else if (devicePartitionTable->typeName() == "msdos") {
              qDebug() << "Setting partition type for MBR table";
              newBootPartition->setType("0xEF"); // MBR type code for EFI System Partition
              newRootPartition->setType("0x83"); // MBR type code for Linux Filesystem
          }
      
      1 Reply Last reply
      0

      1/2

      9 Mar 2025, 00:30

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved