summaryrefslogtreecommitdiffstats
path: root/lib/wholedisk.c
blob: 5161a1e98ef5645f873298d7285356891405adc4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * No copyright is claimed.  This code is in the public domain; do with
 * it what you wish.
 *
 * Written by Karel Zak <kzak@redhat.com>
 */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "blkdev.h"
#include "wholedisk.h"

int is_whole_disk_fd(int fd, const char *name)
{
#ifdef HDIO_GETGEO
	if (fd != -1) {
		struct hd_geometry geometry;
		int i = ioctl(fd, HDIO_GETGEO, &geometry);
		if (i == 0)
			return geometry.start == 0;
	}
#endif
	/*
	 * The "silly heuristic" is still sexy for us, because
	 * for example Xen doesn't implement HDIO_GETGEO for virtual
	 * block devices (/dev/xvda).
	 *
	 * -- kzak@redhat.com (23-Feb-2006)
	 */
	while (*name)
		name++;
	return !isdigit(name[-1]);
}

int is_whole_disk(const char *name)
{
	int fd = -1, res = 0;
#ifdef HDIO_GETGEO
	fd = open(name, O_RDONLY|O_CLOEXEC);
	if (fd != -1)
#endif
		res = is_whole_disk_fd(fd, name);

	if (fd != -1)
		close(fd);
	return res;
}

#ifdef TEST_PROGRAM
int main(int argc, char **argv)
{
	if (argc < 2) {
		fprintf(stderr, "usage: %s <device>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	printf("%s: is%s whole disk\n", argv[1],
			is_whole_disk(argv[1]) ? "" : " NOT");
	exit(EXIT_SUCCESS);
}
#endif