Given a block device, how to detect if names of partitions must contain “p”?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















I want to automatically partition a block device with sfdisk. This might be an SD card, a hard disk, SATA or NVME device.
Initially I thought that sfdisk requires these names and thus I was looking to generate them correctly but apparently one can leave them out anyway. :)



Unlike the traditional ATA and SATA devices that have partitions names simply appended to the device name (e.g., /dev/sda1 for the first partition of block device sda) there exists another scheme for block devices that are flash-based and use other drivers. These add a p between the device and partition name (e.g. /dev/mmcblk0p1 for the first partition of mmcblk0). Unfortunately I have not found any kernel documentation on these details.



Given a block device (e.g., /dev/mmcblk0) how do I decide if the respective (yet non-existing) partitions will be named with an p or not (e.g., /dev/mmcblk0p1 or /dev/mmcblk01)?



This is basically the reverse question of this one but with the additional twist, that the partitions do not exist yet (for the sake of this question I do not allow the answer to actually modify the partition table thus trying it out is not valid).










share|improve this question


























    2















    I want to automatically partition a block device with sfdisk. This might be an SD card, a hard disk, SATA or NVME device.
    Initially I thought that sfdisk requires these names and thus I was looking to generate them correctly but apparently one can leave them out anyway. :)



    Unlike the traditional ATA and SATA devices that have partitions names simply appended to the device name (e.g., /dev/sda1 for the first partition of block device sda) there exists another scheme for block devices that are flash-based and use other drivers. These add a p between the device and partition name (e.g. /dev/mmcblk0p1 for the first partition of mmcblk0). Unfortunately I have not found any kernel documentation on these details.



    Given a block device (e.g., /dev/mmcblk0) how do I decide if the respective (yet non-existing) partitions will be named with an p or not (e.g., /dev/mmcblk0p1 or /dev/mmcblk01)?



    This is basically the reverse question of this one but with the additional twist, that the partitions do not exist yet (for the sake of this question I do not allow the answer to actually modify the partition table thus trying it out is not valid).










    share|improve this question
























      2












      2








      2








      I want to automatically partition a block device with sfdisk. This might be an SD card, a hard disk, SATA or NVME device.
      Initially I thought that sfdisk requires these names and thus I was looking to generate them correctly but apparently one can leave them out anyway. :)



      Unlike the traditional ATA and SATA devices that have partitions names simply appended to the device name (e.g., /dev/sda1 for the first partition of block device sda) there exists another scheme for block devices that are flash-based and use other drivers. These add a p between the device and partition name (e.g. /dev/mmcblk0p1 for the first partition of mmcblk0). Unfortunately I have not found any kernel documentation on these details.



      Given a block device (e.g., /dev/mmcblk0) how do I decide if the respective (yet non-existing) partitions will be named with an p or not (e.g., /dev/mmcblk0p1 or /dev/mmcblk01)?



      This is basically the reverse question of this one but with the additional twist, that the partitions do not exist yet (for the sake of this question I do not allow the answer to actually modify the partition table thus trying it out is not valid).










      share|improve this question














      I want to automatically partition a block device with sfdisk. This might be an SD card, a hard disk, SATA or NVME device.
      Initially I thought that sfdisk requires these names and thus I was looking to generate them correctly but apparently one can leave them out anyway. :)



      Unlike the traditional ATA and SATA devices that have partitions names simply appended to the device name (e.g., /dev/sda1 for the first partition of block device sda) there exists another scheme for block devices that are flash-based and use other drivers. These add a p between the device and partition name (e.g. /dev/mmcblk0p1 for the first partition of mmcblk0). Unfortunately I have not found any kernel documentation on these details.



      Given a block device (e.g., /dev/mmcblk0) how do I decide if the respective (yet non-existing) partitions will be named with an p or not (e.g., /dev/mmcblk0p1 or /dev/mmcblk01)?



      This is basically the reverse question of this one but with the additional twist, that the partitions do not exist yet (for the sake of this question I do not allow the answer to actually modify the partition table thus trying it out is not valid).







      linux linux-kernel partition block-device






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 15 at 14:38









      stefanctstefanct

      1407




      1407




















          2 Answers
          2






          active

          oldest

          votes


















          6














          If device name ends with digit then kernel adds 'p' symbol to separate partition number from device name.



          /dev/sda -> /dev/sda1
          /dev/mmcblk0 -> /dev/mmcblk0p1


          For details see disk_name function in Linux kernel sources (linux/block/partition-generic.c):



          if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
          snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
          else
          snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno)





          share|improve this answer
































            0














            Here is what might or might not be helpful in this endeavor:



            # Try to figure out if partition names are separated by "p" from the device name or not.
            # The following cases imply a "p"
            # - If the device name starts with mmcblk (common drivers for SD card readers)
            # - If there is no device/type file for the respective device in /sys/class/block (e.g., NVME drives)
            # - If the respective device/type indicates "0" (hard disks)
            base_dev=$(basename "$sd_dev")
            if [[ "$base_dev" =~ mmcblk[0-9]+ ||
            ! -e "/sys/class/block/$base_dev/device/type" ||
            $(cat "/sys/class/block/$base_dev/device/type") != 0
            ]]; then
            part_sep="p"
            else
            part_sep=""
            fi





            share|improve this answer






















              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500887%2fgiven-a-block-device-how-to-detect-if-names-of-partitions-must-contain-p%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6














              If device name ends with digit then kernel adds 'p' symbol to separate partition number from device name.



              /dev/sda -> /dev/sda1
              /dev/mmcblk0 -> /dev/mmcblk0p1


              For details see disk_name function in Linux kernel sources (linux/block/partition-generic.c):



              if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
              snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
              else
              snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno)





              share|improve this answer





























                6














                If device name ends with digit then kernel adds 'p' symbol to separate partition number from device name.



                /dev/sda -> /dev/sda1
                /dev/mmcblk0 -> /dev/mmcblk0p1


                For details see disk_name function in Linux kernel sources (linux/block/partition-generic.c):



                if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
                snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
                else
                snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno)





                share|improve this answer



























                  6












                  6








                  6







                  If device name ends with digit then kernel adds 'p' symbol to separate partition number from device name.



                  /dev/sda -> /dev/sda1
                  /dev/mmcblk0 -> /dev/mmcblk0p1


                  For details see disk_name function in Linux kernel sources (linux/block/partition-generic.c):



                  if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
                  snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
                  else
                  snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno)





                  share|improve this answer















                  If device name ends with digit then kernel adds 'p' symbol to separate partition number from device name.



                  /dev/sda -> /dev/sda1
                  /dev/mmcblk0 -> /dev/mmcblk0p1


                  For details see disk_name function in Linux kernel sources (linux/block/partition-generic.c):



                  if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
                  snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
                  else
                  snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 18 at 16:55

























                  answered Feb 15 at 16:33









                  John DoeJohn Doe

                  1906




                  1906























                      0














                      Here is what might or might not be helpful in this endeavor:



                      # Try to figure out if partition names are separated by "p" from the device name or not.
                      # The following cases imply a "p"
                      # - If the device name starts with mmcblk (common drivers for SD card readers)
                      # - If there is no device/type file for the respective device in /sys/class/block (e.g., NVME drives)
                      # - If the respective device/type indicates "0" (hard disks)
                      base_dev=$(basename "$sd_dev")
                      if [[ "$base_dev" =~ mmcblk[0-9]+ ||
                      ! -e "/sys/class/block/$base_dev/device/type" ||
                      $(cat "/sys/class/block/$base_dev/device/type") != 0
                      ]]; then
                      part_sep="p"
                      else
                      part_sep=""
                      fi





                      share|improve this answer



























                        0














                        Here is what might or might not be helpful in this endeavor:



                        # Try to figure out if partition names are separated by "p" from the device name or not.
                        # The following cases imply a "p"
                        # - If the device name starts with mmcblk (common drivers for SD card readers)
                        # - If there is no device/type file for the respective device in /sys/class/block (e.g., NVME drives)
                        # - If the respective device/type indicates "0" (hard disks)
                        base_dev=$(basename "$sd_dev")
                        if [[ "$base_dev" =~ mmcblk[0-9]+ ||
                        ! -e "/sys/class/block/$base_dev/device/type" ||
                        $(cat "/sys/class/block/$base_dev/device/type") != 0
                        ]]; then
                        part_sep="p"
                        else
                        part_sep=""
                        fi





                        share|improve this answer

























                          0












                          0








                          0







                          Here is what might or might not be helpful in this endeavor:



                          # Try to figure out if partition names are separated by "p" from the device name or not.
                          # The following cases imply a "p"
                          # - If the device name starts with mmcblk (common drivers for SD card readers)
                          # - If there is no device/type file for the respective device in /sys/class/block (e.g., NVME drives)
                          # - If the respective device/type indicates "0" (hard disks)
                          base_dev=$(basename "$sd_dev")
                          if [[ "$base_dev" =~ mmcblk[0-9]+ ||
                          ! -e "/sys/class/block/$base_dev/device/type" ||
                          $(cat "/sys/class/block/$base_dev/device/type") != 0
                          ]]; then
                          part_sep="p"
                          else
                          part_sep=""
                          fi





                          share|improve this answer













                          Here is what might or might not be helpful in this endeavor:



                          # Try to figure out if partition names are separated by "p" from the device name or not.
                          # The following cases imply a "p"
                          # - If the device name starts with mmcblk (common drivers for SD card readers)
                          # - If there is no device/type file for the respective device in /sys/class/block (e.g., NVME drives)
                          # - If the respective device/type indicates "0" (hard disks)
                          base_dev=$(basename "$sd_dev")
                          if [[ "$base_dev" =~ mmcblk[0-9]+ ||
                          ! -e "/sys/class/block/$base_dev/device/type" ||
                          $(cat "/sys/class/block/$base_dev/device/type") != 0
                          ]]; then
                          part_sep="p"
                          else
                          part_sep=""
                          fi






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 15 at 14:38









                          stefanctstefanct

                          1407




                          1407



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500887%2fgiven-a-block-device-how-to-detect-if-names-of-partitions-must-contain-p%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown






                              Popular posts from this blog

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)