bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * No copyright is claimed. This code is in the public domain; do with |
| 3 | * it what you wish. |
| 4 | * |
| 5 | * Written by Karel Zak <kzak@redhat.com> |
| 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <ctype.h> |
| 10 | |
| 11 | #include "blkdev.h" |
| 12 | #include "wholedisk.h" |
| 13 | |
| 14 | int is_whole_disk_fd(int fd, const char *name) |
| 15 | { |
| 16 | #ifdef HDIO_GETGEO |
| 17 | if (fd != -1) { |
| 18 | struct hd_geometry geometry; |
| 19 | int i = ioctl(fd, HDIO_GETGEO, &geometry); |
| 20 | if (i == 0) |
| 21 | return geometry.start == 0; |
| 22 | } |
| 23 | #endif |
| 24 | /* |
| 25 | * The "silly heuristic" is still sexy for us, because |
| 26 | * for example Xen doesn't implement HDIO_GETGEO for virtual |
| 27 | * block devices (/dev/xvda). |
| 28 | * |
| 29 | * -- kzak@redhat.com (23-Feb-2006) |
| 30 | */ |
| 31 | while (*name) |
| 32 | name++; |
| 33 | return !isdigit(name[-1]); |
| 34 | } |
| 35 | |
| 36 | int is_whole_disk(const char *name) |
| 37 | { |
| 38 | int fd = -1, res = 0; |
| 39 | #ifdef HDIO_GETGEO |
| 40 | fd = open(name, O_RDONLY); |
| 41 | if (fd != -1) |
| 42 | #endif |
| 43 | res = is_whole_disk_fd(fd, name); |
| 44 | |
| 45 | if (fd != -1) |
| 46 | close(fd); |
| 47 | return res; |
| 48 | } |
| 49 | |
| 50 | #ifdef TEST_PROGRAM |
| 51 | int main(int argc, char **argv) |
| 52 | { |
| 53 | if (argc < 2) { |
| 54 | fprintf(stderr, "usage: %s <device>\n", argv[0]); |
| 55 | exit(EXIT_FAILURE); |
| 56 | } |
| 57 | |
| 58 | printf("%s: is%s whole disk\n", argv[1], |
| 59 | is_whole_disk(argv[1]) ? "" : " NOT"); |
| 60 | exit(EXIT_SUCCESS); |
| 61 | } |
| 62 | #endif |