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

Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
add a comment |
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
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
linux linux-kernel partition block-device
asked Feb 15 at 14:38
stefanctstefanct
1407
1407
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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)
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
edited Feb 18 at 16:55
answered Feb 15 at 16:33
John DoeJohn Doe
1906
1906
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Feb 15 at 14:38
stefanctstefanct
1407
1407
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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