Last active
July 9, 2025 09:53
-
-
Save teknoraver/d1aae047c2a1818028089df37e1e4cb7 to your computer and use it in GitHub Desktop.
Get the parent device of a partition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 3-Clause BSD NON-AI License | |
* | |
* Copyright 2025 Matteo Croce | |
* | |
* Redistribution and use in source and binary forms, with or without modification, | |
* are permitted provided that the following conditions are met: | |
* | |
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | |
* | |
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer | |
* in the documentation and/or other materials provided with the distribution. | |
* | |
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products | |
* derived from this software without specific prior written permission. | |
* | |
* 4. The source code and the binary form, and any modifications made to them may not be used for the purpose of training or improving machine learning algorithms, | |
* including but not limited to artificial intelligence, natural language processing, or data mining. This condition applies to any derivatives, | |
* modifications, or updates based on the Software code. Any usage of the source code or the binary form in an AI-training dataset is considered a breach of this License. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, | |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | |
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <limits.h> | |
#include <libgen.h> | |
int parentdev(const char *dev, char *parent) | |
{ | |
char path[PATH_MAX]; | |
if (!dev || !parent || !dev[0]) { | |
errno = EINVAL; | |
return 1; | |
} | |
snprintf(path, sizeof(path), "/sys/class/block/%s", dev); | |
if (access(path, F_OK) != 0) | |
return 1; // dev is not a block device | |
snprintf(path, sizeof(path), "/sys/class/block/%s/partition", dev); | |
if (access(path, F_OK) != 0) { | |
strcpy(parent, dev); | |
return 0; // dev is a whole device | |
} | |
snprintf(path, sizeof(path), "/sys/class/block/%s/..", dev); | |
if (realpath(path, parent) == NULL) | |
return 1; | |
strcpy(parent, basename(parent)); | |
return 0; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if (argc < 2) { | |
fprintf(stderr, "Usage: %s <device> ...\n", argv[0]); | |
return 1; | |
} | |
char parent[PATH_MAX]; | |
for (; argv[1]; argv++) { | |
const char *dev = argv[1]; | |
if (strncmp(dev, "/dev/", 5) == 0) | |
dev += 5; | |
if (!parentdev(dev, parent) != 0) | |
printf("parent of %s is %s\n", dev, parent); | |
else | |
fprintf(stderr, "parent of %s not found: %m\n", dev); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment