chiark / gitweb /
Make run_directory.c stat the place it is going to try to run.
[elogind.git] / extras / volume_id / libvolume_id / lvm.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25
26 #include "volume_id.h"
27 #include "logging.h"
28 #include "util.h"
29
30 struct lvm1_super_block {
31         uint8_t id[2];
32 } __attribute__((packed));
33
34 struct lvm2_super_block {
35         uint8_t         id[8];
36         uint64_t        sector_xl;
37         uint32_t        crc_xl;
38         uint32_t        offset_xl;
39         uint8_t         type[8];
40 } __attribute__((packed));
41
42 #define LVM1_SB_OFF                     0x400
43 #define LVM1_MAGIC                      "HM"
44
45 int volume_id_probe_lvm1(struct volume_id *id, uint64_t off)
46 {
47         const uint8_t *buf;
48         struct lvm1_super_block *lvm;
49
50         dbg("probing at offset 0x%llx", (unsigned long long) off);
51
52         buf = volume_id_get_buffer(id, off + LVM1_SB_OFF, 0x800);
53         if (buf == NULL)
54                 return -1;
55
56         lvm = (struct lvm1_super_block *) buf;
57
58         if (memcmp(lvm->id, LVM1_MAGIC, 2) != 0)
59                 return -1;
60
61         volume_id_set_usage(id, VOLUME_ID_RAID);
62         id->type = "LVM1_member";
63
64         return 0;
65 }
66
67 #define LVM2_LABEL_ID                   "LABELONE"
68 #define LVM2LABEL_SCAN_SECTORS          4
69
70 int volume_id_probe_lvm2(struct volume_id *id, uint64_t off)
71 {
72         const uint8_t *buf;
73         unsigned int soff;
74         struct lvm2_super_block *lvm;
75
76         dbg("probing at offset 0x%llx", (unsigned long long) off);
77
78         buf = volume_id_get_buffer(id, off, LVM2LABEL_SCAN_SECTORS * 0x200);
79         if (buf == NULL)
80                 return -1;
81
82
83         for (soff = 0; soff < LVM2LABEL_SCAN_SECTORS * 0x200; soff += 0x200) {
84                 lvm = (struct lvm2_super_block *) &buf[soff];
85
86                 if (memcmp(lvm->id, LVM2_LABEL_ID, 8) == 0)
87                         goto found;
88         }
89
90         return -1;
91
92 found:
93         memcpy(id->type_version, lvm->type, 8);
94         volume_id_set_usage(id, VOLUME_ID_RAID);
95         id->type = "LVM2_member";
96
97         return 0;
98 }