getroot.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config-util.h>
  19. #include <config.h>
  20. #include <grub/emu/getroot.h>
  21. #include <grub/mm.h>
  22. #ifdef HAVE_DEVICE_MAPPER
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <assert.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <dirent.h>
  30. #include <errno.h>
  31. #include <error.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <stdint.h>
  35. #ifdef HAVE_LIMITS_H
  36. #include <limits.h>
  37. #endif
  38. #if defined(MAJOR_IN_MKDEV)
  39. #include <sys/mkdev.h>
  40. #elif defined(MAJOR_IN_SYSMACROS)
  41. #include <sys/sysmacros.h>
  42. #endif
  43. #include <libdevmapper.h>
  44. #include <grub/types.h>
  45. #include <grub/util/misc.h>
  46. #include <grub/mm.h>
  47. #include <grub/misc.h>
  48. #include <grub/emu/misc.h>
  49. #include <grub/emu/hostdisk.h>
  50. static int
  51. grub_util_open_dm (const char *os_dev, struct dm_tree **tree,
  52. struct dm_tree_node **node)
  53. {
  54. uint32_t maj, min;
  55. struct stat st;
  56. *node = NULL;
  57. *tree = NULL;
  58. if (stat (os_dev, &st) < 0)
  59. return 0;
  60. maj = major (st.st_rdev);
  61. min = minor (st.st_rdev);
  62. if (!dm_is_dm_major (maj))
  63. return 0;
  64. *tree = dm_tree_create ();
  65. if (! *tree)
  66. {
  67. grub_puts_ (N_("Failed to create `device-mapper' tree"));
  68. grub_dprintf ("hostdisk", "dm_tree_create failed\n");
  69. return 0;
  70. }
  71. if (! dm_tree_add_dev (*tree, maj, min))
  72. {
  73. grub_dprintf ("hostdisk", "dm_tree_add_dev failed\n");
  74. dm_tree_free (*tree);
  75. *tree = NULL;
  76. return 0;
  77. }
  78. *node = dm_tree_find_node (*tree, maj, min);
  79. if (! *node)
  80. {
  81. grub_dprintf ("hostdisk", "dm_tree_find_node failed\n");
  82. dm_tree_free (*tree);
  83. *tree = NULL;
  84. return 0;
  85. }
  86. return 1;
  87. }
  88. static char *
  89. get_dm_uuid (const char *os_dev)
  90. {
  91. struct dm_tree *tree;
  92. struct dm_tree_node *node;
  93. const char *node_uuid;
  94. char *ret;
  95. if (!grub_util_open_dm (os_dev, &tree, &node))
  96. return NULL;
  97. node_uuid = dm_tree_node_get_uuid (node);
  98. if (! node_uuid)
  99. {
  100. grub_dprintf ("hostdisk", "%s has no DM uuid\n", os_dev);
  101. dm_tree_free (tree);
  102. return NULL;
  103. }
  104. ret = grub_strdup (node_uuid);
  105. dm_tree_free (tree);
  106. return ret;
  107. }
  108. enum grub_dev_abstraction_types
  109. grub_util_get_dm_abstraction (const char *os_dev)
  110. {
  111. char *uuid;
  112. uuid = get_dm_uuid (os_dev);
  113. if (uuid == NULL)
  114. return GRUB_DEV_ABSTRACTION_NONE;
  115. if (strncmp (uuid, "LVM-", 4) == 0)
  116. {
  117. grub_free (uuid);
  118. return GRUB_DEV_ABSTRACTION_LVM;
  119. }
  120. if (strncmp (uuid, "CRYPT-LUKS1-", 12) == 0)
  121. {
  122. grub_free (uuid);
  123. return GRUB_DEV_ABSTRACTION_LUKS;
  124. }
  125. grub_free (uuid);
  126. return GRUB_DEV_ABSTRACTION_NONE;
  127. }
  128. void
  129. grub_util_pull_devmapper (const char *os_dev)
  130. {
  131. struct dm_tree *tree;
  132. struct dm_tree_node *node;
  133. struct dm_tree_node *child;
  134. void *handle = NULL;
  135. char *lastsubdev = NULL;
  136. char *uuid;
  137. uuid = get_dm_uuid (os_dev);
  138. if (!grub_util_open_dm (os_dev, &tree, &node))
  139. {
  140. grub_free (uuid);
  141. return;
  142. }
  143. while ((child = dm_tree_next_child (&handle, node, 0)))
  144. {
  145. const struct dm_info *dm = dm_tree_node_get_info (child);
  146. char *subdev;
  147. if (!dm)
  148. continue;
  149. subdev = grub_find_device ("/dev", makedev (dm->major, dm->minor));
  150. if (subdev)
  151. {
  152. lastsubdev = subdev;
  153. grub_util_pull_device (subdev);
  154. }
  155. }
  156. if (uuid && strncmp (uuid, "CRYPT-LUKS1-", sizeof ("CRYPT-LUKS1-") - 1) == 0
  157. && lastsubdev)
  158. {
  159. char *grdev = grub_util_get_grub_dev (lastsubdev);
  160. dm_tree_free (tree);
  161. if (grdev)
  162. {
  163. grub_err_t err;
  164. err = grub_cryptodisk_cheat_mount (grdev, os_dev);
  165. if (err)
  166. grub_util_error (_("can't mount encrypted volume `%s': %s"),
  167. lastsubdev, grub_errmsg);
  168. }
  169. grub_free (grdev);
  170. }
  171. else
  172. dm_tree_free (tree);
  173. grub_free (uuid);
  174. }
  175. char *
  176. grub_util_devmapper_part_to_disk (struct stat *st,
  177. int *is_part, const char *path)
  178. {
  179. int major, minor;
  180. if (grub_util_get_dm_node_linear_info (st->st_rdev,
  181. &major, &minor, 0))
  182. {
  183. *is_part = 1;
  184. return grub_find_device ("/dev", makedev (major, minor));
  185. }
  186. *is_part = 0;
  187. return xstrdup (path);
  188. }
  189. char *
  190. grub_util_get_devmapper_grub_dev (const char *os_dev)
  191. {
  192. char *uuid, *optr;
  193. char *grub_dev;
  194. uuid = get_dm_uuid (os_dev);
  195. if (!uuid)
  196. return NULL;
  197. switch (grub_util_get_dev_abstraction (os_dev))
  198. {
  199. case GRUB_DEV_ABSTRACTION_LVM:
  200. {
  201. unsigned i;
  202. int dashes[] = { 0, 6, 10, 14, 18, 22, 26, 32, 38, 42, 46, 50, 54, 58};
  203. grub_dev = xmalloc (grub_strlen (uuid) + 40);
  204. optr = grub_stpcpy (grub_dev, "lvmid/");
  205. for (i = 0; i < ARRAY_SIZE (dashes) - 1; i++)
  206. {
  207. memcpy (optr, uuid + sizeof ("LVM-") - 1 + dashes[i],
  208. dashes[i+1] - dashes[i]);
  209. optr += dashes[i+1] - dashes[i];
  210. *optr++ = '-';
  211. }
  212. optr = stpcpy (optr, uuid + sizeof ("LVM-") - 1 + dashes[i]);
  213. *optr = '\0';
  214. grub_dev[sizeof("lvmid/xxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxxxx") - 1]
  215. = '/';
  216. free (uuid);
  217. return grub_dev;
  218. }
  219. case GRUB_DEV_ABSTRACTION_LUKS:
  220. {
  221. char *dash;
  222. dash = grub_strchr (uuid + sizeof ("CRYPT-LUKS1-") - 1, '-');
  223. if (dash)
  224. *dash = 0;
  225. grub_dev = grub_xasprintf ("cryptouuid/%s",
  226. uuid + sizeof ("CRYPT-LUKS1-") - 1);
  227. grub_free (uuid);
  228. return grub_dev;
  229. }
  230. default:
  231. grub_free (uuid);
  232. return NULL;
  233. }
  234. }
  235. char *
  236. grub_util_get_vg_uuid (const char *os_dev)
  237. {
  238. char *uuid, *vgid;
  239. int dashes[] = { 0, 6, 10, 14, 18, 22, 26, 32};
  240. unsigned i;
  241. char *optr;
  242. uuid = get_dm_uuid (os_dev);
  243. if (!uuid)
  244. return NULL;
  245. vgid = xmalloc (grub_strlen (uuid));
  246. optr = vgid;
  247. for (i = 0; i < ARRAY_SIZE (dashes) - 1; i++)
  248. {
  249. memcpy (optr, uuid + sizeof ("LVM-") - 1 + dashes[i],
  250. dashes[i+1] - dashes[i]);
  251. optr += dashes[i+1] - dashes[i];
  252. *optr++ = '-';
  253. }
  254. optr--;
  255. *optr = '\0';
  256. grub_free (uuid);
  257. return vgid;
  258. }
  259. void
  260. grub_util_devmapper_cleanup (void)
  261. {
  262. dm_lib_release ();
  263. }
  264. #else
  265. void
  266. grub_util_pull_devmapper (const char *os_dev __attribute__ ((unused)))
  267. {
  268. return;
  269. }
  270. void
  271. grub_util_devmapper_cleanup (void)
  272. {
  273. }
  274. enum grub_dev_abstraction_types
  275. grub_util_get_dm_abstraction (const char *os_dev __attribute__ ((unused)))
  276. {
  277. return GRUB_DEV_ABSTRACTION_NONE;
  278. }
  279. char *
  280. grub_util_get_vg_uuid (const char *os_dev __attribute__ ((unused)))
  281. {
  282. return NULL;
  283. }
  284. char *
  285. grub_util_devmapper_part_to_disk (struct stat *st __attribute__ ((unused)),
  286. int *is_part __attribute__ ((unused)),
  287. const char *os_dev __attribute__ ((unused)))
  288. {
  289. return NULL;
  290. }
  291. char *
  292. grub_util_get_devmapper_grub_dev (const char *os_dev __attribute__ ((unused)))
  293. {
  294. return NULL;
  295. }
  296. #endif