getroot.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <assert.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <dirent.h>
  27. #include <errno.h>
  28. #include <error.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #ifdef HAVE_LIMITS_H
  33. #include <limits.h>
  34. #endif
  35. #include <grub/types.h>
  36. # include <sys/param.h>
  37. # include <sys/mount.h>
  38. # include <sys/disk.h> /* DIOCGMEDIASIZE */
  39. # include <sys/param.h>
  40. # include <sys/sysctl.h>
  41. #include <libgeom.h>
  42. #include <grub/util/misc.h>
  43. #include <grub/mm.h>
  44. #include <grub/misc.h>
  45. #include <grub/emu/misc.h>
  46. #include <grub/emu/hostdisk.h>
  47. #include <grub/emu/getroot.h>
  48. #include <grub/cryptodisk.h>
  49. #include <sys/wait.h>
  50. #include <libgeom.h>
  51. #define LVM_DEV_MAPPER_STRING "/dev/linux_lvm/"
  52. static const char *
  53. grub_util_get_geom_abstraction (const char *dev)
  54. {
  55. char *whole;
  56. struct gmesh mesh;
  57. struct gclass *class;
  58. const char *name;
  59. int err;
  60. if (strncmp (dev, "/dev/", sizeof ("/dev/") - 1) != 0)
  61. return 0;
  62. name = dev + sizeof ("/dev/") - 1;
  63. grub_util_follow_gpart_up (name, NULL, &whole);
  64. grub_util_info ("following geom '%s'", name);
  65. err = geom_gettree (&mesh);
  66. if (err != 0)
  67. /* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
  68. Usually left untranslated.
  69. */
  70. grub_util_error ("%s", _("couldn't open geom"));
  71. LIST_FOREACH (class, &mesh.lg_class, lg_class)
  72. {
  73. struct ggeom *geom;
  74. LIST_FOREACH (geom, &class->lg_geom, lg_geom)
  75. {
  76. struct gprovider *provider;
  77. LIST_FOREACH (provider, &geom->lg_provider, lg_provider)
  78. if (strcmp (provider->lg_name, name) == 0)
  79. return class->lg_name;
  80. }
  81. }
  82. return NULL;
  83. }
  84. enum grub_dev_abstraction_types
  85. grub_util_get_dev_abstraction_os (const char *os_dev)
  86. {
  87. const char *abstrac;
  88. abstrac = grub_util_get_geom_abstraction (os_dev);
  89. grub_util_info ("abstraction of %s is %s", os_dev, abstrac);
  90. if (abstrac && grub_strcasecmp (abstrac, "eli") == 0)
  91. return GRUB_DEV_ABSTRACTION_GELI;
  92. /* Check for LVM. */
  93. if (!strncmp (os_dev, LVM_DEV_MAPPER_STRING, sizeof(LVM_DEV_MAPPER_STRING)-1))
  94. return GRUB_DEV_ABSTRACTION_LVM;
  95. return GRUB_DEV_ABSTRACTION_NONE;
  96. }
  97. char *
  98. grub_util_part_to_disk (const char *os_dev, struct stat *st,
  99. int *is_part)
  100. {
  101. char *out, *out2;
  102. if (! S_ISCHR (st->st_mode))
  103. {
  104. *is_part = 0;
  105. return xstrdup (os_dev);
  106. }
  107. if (strncmp (os_dev, "/dev/", sizeof ("/dev/") - 1) != 0)
  108. return xstrdup (os_dev);
  109. grub_util_follow_gpart_up (os_dev + sizeof ("/dev/") - 1, NULL, &out);
  110. if (grub_strcmp (os_dev + sizeof ("/dev/") - 1, out) != 0)
  111. *is_part = 1;
  112. out2 = xasprintf ("/dev/%s", out);
  113. free (out);
  114. return out2;
  115. }
  116. int
  117. grub_util_pull_device_os (const char *os_dev,
  118. enum grub_dev_abstraction_types ab)
  119. {
  120. switch (ab)
  121. {
  122. case GRUB_DEV_ABSTRACTION_GELI:
  123. {
  124. char *whole;
  125. struct gmesh mesh;
  126. struct gclass *class;
  127. const char *name;
  128. int err;
  129. char *lastsubdev = NULL;
  130. if (strncmp (os_dev, "/dev/", sizeof ("/dev/") - 1) != 0)
  131. return 1;
  132. name = os_dev + sizeof ("/dev/") - 1;
  133. grub_util_follow_gpart_up (name, NULL, &whole);
  134. grub_util_info ("following geom '%s'", name);
  135. err = geom_gettree (&mesh);
  136. if (err != 0)
  137. /* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
  138. Usually left untranslated.
  139. */
  140. grub_util_error ("%s", _("couldn't open geom"));
  141. LIST_FOREACH (class, &mesh.lg_class, lg_class)
  142. {
  143. struct ggeom *geom;
  144. LIST_FOREACH (geom, &class->lg_geom, lg_geom)
  145. {
  146. struct gprovider *provider;
  147. LIST_FOREACH (provider, &geom->lg_provider, lg_provider)
  148. if (strcmp (provider->lg_name, name) == 0)
  149. {
  150. struct gconsumer *consumer;
  151. char *fname;
  152. LIST_FOREACH (consumer, &geom->lg_consumer, lg_consumer)
  153. break;
  154. if (!consumer)
  155. grub_util_error ("%s",
  156. _("couldn't find geli consumer"));
  157. fname = xasprintf ("/dev/%s", consumer->lg_provider->lg_name);
  158. grub_util_info ("consumer %s", consumer->lg_provider->lg_name);
  159. lastsubdev = consumer->lg_provider->lg_name;
  160. grub_util_pull_device (fname);
  161. free (fname);
  162. }
  163. }
  164. }
  165. if (ab == GRUB_DEV_ABSTRACTION_GELI && lastsubdev)
  166. {
  167. char *fname = xasprintf ("/dev/%s", lastsubdev);
  168. char *grdev = grub_util_get_grub_dev (fname);
  169. free (fname);
  170. if (grdev)
  171. {
  172. grub_err_t gr_err;
  173. gr_err = grub_cryptodisk_cheat_mount (grdev, os_dev);
  174. if (gr_err)
  175. grub_util_error (_("can't mount encrypted volume `%s': %s"),
  176. lastsubdev, grub_errmsg);
  177. }
  178. grub_free (grdev);
  179. }
  180. }
  181. return 1;
  182. default:
  183. return 0;
  184. }
  185. }
  186. char *
  187. grub_util_get_grub_dev_os (const char *os_dev)
  188. {
  189. char *grub_dev = NULL;
  190. switch (grub_util_get_dev_abstraction (os_dev))
  191. {
  192. /* Fallback for non-devmapper build. In devmapper-builds LVM is handled
  193. in rub_util_get_devmapper_grub_dev and this point isn't reached.
  194. */
  195. case GRUB_DEV_ABSTRACTION_LVM:
  196. {
  197. unsigned short len;
  198. grub_size_t offset = sizeof (LVM_DEV_MAPPER_STRING) - 1;
  199. len = strlen (os_dev) - offset + 1;
  200. grub_dev = xmalloc (len + sizeof ("lvm/"));
  201. grub_memcpy (grub_dev, "lvm/", sizeof ("lvm/") - 1);
  202. grub_memcpy (grub_dev + sizeof ("lvm/") - 1, os_dev + offset, len);
  203. }
  204. break;
  205. case GRUB_DEV_ABSTRACTION_GELI:
  206. {
  207. char *whole;
  208. struct gmesh mesh;
  209. struct gclass *class;
  210. const char *name;
  211. int err;
  212. if (strncmp (os_dev, "/dev/", sizeof ("/dev/") - 1) != 0)
  213. return 0;
  214. name = os_dev + sizeof ("/dev/") - 1;
  215. grub_util_follow_gpart_up (name, NULL, &whole);
  216. grub_util_info ("following geom '%s'", name);
  217. err = geom_gettree (&mesh);
  218. if (err != 0)
  219. /* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
  220. Usually left untranslated.
  221. */
  222. grub_util_error ("%s", _("couldn't open geom"));
  223. LIST_FOREACH (class, &mesh.lg_class, lg_class)
  224. {
  225. struct ggeom *geom;
  226. LIST_FOREACH (geom, &class->lg_geom, lg_geom)
  227. {
  228. struct gprovider *provider;
  229. LIST_FOREACH (provider, &geom->lg_provider, lg_provider)
  230. if (strcmp (provider->lg_name, name) == 0)
  231. {
  232. struct gconsumer *consumer;
  233. char *fname;
  234. char *uuid;
  235. LIST_FOREACH (consumer, &geom->lg_consumer, lg_consumer)
  236. break;
  237. if (!consumer)
  238. grub_util_error ("%s",
  239. _("couldn't find geli consumer"));
  240. fname = xasprintf ("/dev/%s", consumer->lg_provider->lg_name);
  241. uuid = grub_util_get_geli_uuid (fname);
  242. if (!uuid)
  243. grub_util_error ("%s",
  244. _("couldn't retrieve geli UUID"));
  245. grub_dev = xasprintf ("cryptouuid/%s", uuid);
  246. free (fname);
  247. free (uuid);
  248. }
  249. }
  250. }
  251. }
  252. break;
  253. default:
  254. break;
  255. }
  256. return grub_dev;
  257. }
  258. /* FIXME: geom actually gives us the whole container hierarchy.
  259. It can be used more efficiently than this. */
  260. void
  261. grub_util_follow_gpart_up (const char *name, grub_disk_addr_t *off_out, char **name_out)
  262. {
  263. struct gmesh mesh;
  264. struct gclass *class;
  265. int err;
  266. struct ggeom *geom;
  267. grub_util_info ("following geom '%s'", name);
  268. err = geom_gettree (&mesh);
  269. if (err != 0)
  270. /* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
  271. Usually left untranslated.
  272. */
  273. grub_util_error ("%s", _("couldn't open geom"));
  274. LIST_FOREACH (class, &mesh.lg_class, lg_class)
  275. if (strcasecmp (class->lg_name, "part") == 0)
  276. break;
  277. if (!class)
  278. /* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
  279. Usually left untranslated. "part" is the identifier of one of its
  280. classes. */
  281. grub_util_error ("%s", _("couldn't find geom `part' class"));
  282. LIST_FOREACH (geom, &class->lg_geom, lg_geom)
  283. {
  284. struct gprovider *provider;
  285. LIST_FOREACH (provider, &geom->lg_provider, lg_provider)
  286. if (strcmp (provider->lg_name, name) == 0)
  287. {
  288. char *name_tmp = xstrdup (geom->lg_name);
  289. grub_disk_addr_t off = 0;
  290. struct gconfig *config;
  291. grub_util_info ("geom '%s' has parent '%s'", name, geom->lg_name);
  292. grub_util_follow_gpart_up (name_tmp, &off, name_out);
  293. free (name_tmp);
  294. LIST_FOREACH (config, &provider->lg_config, lg_config)
  295. if (strcasecmp (config->lg_name, "start") == 0)
  296. off += strtoull (config->lg_val, 0, 10);
  297. if (off_out)
  298. *off_out = off;
  299. return;
  300. }
  301. }
  302. grub_util_info ("geom '%s' has no parent", name);
  303. if (name_out)
  304. *name_out = xstrdup (name);
  305. if (off_out)
  306. *off_out = 0;
  307. }
  308. grub_disk_addr_t
  309. grub_util_find_partition_start_os (const char *dev)
  310. {
  311. grub_disk_addr_t out;
  312. if (strncmp (dev, "/dev/", sizeof ("/dev/") - 1) != 0)
  313. return 0;
  314. grub_util_follow_gpart_up (dev + sizeof ("/dev/") - 1, &out, NULL);
  315. return out;
  316. }