uuid.patch 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # DP: Add support for specifying the root device using UUIDs on the
  2. # DP: kernel command line like this without the need for an initrd:
  3. # DP: linux ... root=UUID=ce40d6b2-18eb-4a75-aefe-7ddb0995ce63
  4. # DP: Note: debugging code is kept in, commented out “//” style.
  5. # DP:
  6. # DP: Written © 2010 by Thorsten Glaser <tg@debian.org>
  7. # DP: Idea from 1999 by David Balazic <david.balazic@uni-mb.si>
  8. --- linux-2.6.36/block/genhd.c~ Wed Oct 20 22:30:22 2010
  9. +++ linux-2.6.36/block/genhd.c Sat Nov 20 23:14:03 2010
  10. @@ -35,7 +35,7 @@ struct kobject *block_depr;
  11. static DEFINE_MUTEX(ext_devt_mutex);
  12. static DEFINE_IDR(ext_devt_idr);
  13. -static struct device_type disk_type;
  14. +struct device_type disk_type;
  15. /**
  16. * disk_get_part - get partition
  17. @@ -1019,7 +1019,7 @@ static char *block_devnode(struct device *dev, mode_t
  18. return NULL;
  19. }
  20. -static struct device_type disk_type = {
  21. +struct device_type disk_type = {
  22. .name = "disk",
  23. .groups = disk_attr_groups,
  24. .release = disk_release,
  25. --- linux-2.6.36/init/do_mounts.c~ Wed Oct 20 20:29:58 2010
  26. +++ linux-2.6.36/init/do_mounts.c Sun Nov 21 18:37:36 2010
  27. @@ -32,6 +32,132 @@ static int __initdata root_wait;
  28. dev_t ROOT_DEV;
  29. +#ifdef CONFIG_EXT2_FS
  30. +/* support for root=UUID=ce40d6b2-18eb-4a75-aefe-7ddb0995ce63 bootargs */
  31. +
  32. +#include <linux/ext2_fs.h>
  33. +
  34. +__u8 root_dev_uuid[16];
  35. +int root_dev_type; /* 0 = normal (/dev/hda1, 0301); 1 = UUID; 3 = bad */
  36. +
  37. +/* imported from block/genhd.c after removing its static qualifier */
  38. +extern struct device_type disk_type;
  39. +
  40. +/* helper function */
  41. +static __u8 __init fromhex(char c)
  42. +{
  43. + if (c >= '0' && c <= '9')
  44. + return (c - '0');
  45. + c &= ~32;
  46. + if (c >= 'A' && c <= 'F')
  47. + return (c - 'A' + 10);
  48. + return (0xFF);
  49. +}
  50. +
  51. +static void __init parse_uuid(const char *s)
  52. +{
  53. + int i;
  54. + __u8 j, k;
  55. +
  56. + if (strlen(s) != 36 || s[8] != '-' || s[13] != '-' ||
  57. + s[18] != '-' || s[23] != '-')
  58. + goto bad_uuid;
  59. + for (i = 0; i < 16; i++) {
  60. + if (*s == '-')
  61. + ++s;
  62. + j = fromhex(*s++);
  63. + k = fromhex(*s++);
  64. + if (j == 0xFF || k == 0xFF)
  65. + goto bad_uuid;
  66. + root_dev_uuid[i] = (j << 4) | k;
  67. + }
  68. + return;
  69. + bad_uuid:
  70. + /* we cannot panic here, defer */
  71. + root_dev_type = 3;
  72. +}
  73. +
  74. +/* from drivers/md/md.c */
  75. +static void __init initcode_bi_complete(struct bio *bio, int error)
  76. +{
  77. + complete((struct completion*)bio->bi_private);
  78. +}
  79. +
  80. +static int __init initcode_sync_page_read(struct block_device *bdev,
  81. + sector_t sector, int size, struct page *page)
  82. +{
  83. + struct bio *bio = bio_alloc(GFP_NOIO, 1);
  84. + struct completion event;
  85. + int ret, rw = READ;
  86. +
  87. + rw |= REQ_SYNC | REQ_UNPLUG;
  88. +
  89. + bio->bi_bdev = bdev;
  90. + bio->bi_sector = sector;
  91. + bio_add_page(bio, page, size, 0);
  92. + init_completion(&event);
  93. + bio->bi_private = &event;
  94. + bio->bi_end_io = initcode_bi_complete;
  95. + submit_bio(rw, bio);
  96. + wait_for_completion(&event);
  97. +
  98. + ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
  99. + bio_put(bio);
  100. + /* 0 = failure */
  101. + return ret;
  102. +}
  103. +
  104. +/* most of this taken from fs/ext2/super.c */
  105. +static int __init check_dev(struct gendisk *thedisk, dev_t devt,
  106. + int blocksize, struct page *page)
  107. +{
  108. + struct ext2_super_block * es;
  109. + struct block_device *bdev;
  110. + unsigned long sb_block = 1;
  111. + unsigned long logic_sb_block;
  112. + unsigned long offset = 0;
  113. + int rv = /* not found */ 0;
  114. + char bff[22];
  115. +
  116. + bdev = bdget(devt);
  117. + if (blkdev_get(bdev, FMODE_READ)) {
  118. + printk(KERN_ERR "VFS: opening block device %s failed!\n",
  119. + format_dev_t(bff, devt));
  120. + return (0);
  121. + }
  122. +
  123. + if (blocksize != BLOCK_SIZE) {
  124. + logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  125. + offset = (sb_block*BLOCK_SIZE) % blocksize;
  126. + } else {
  127. + logic_sb_block = sb_block;
  128. + }
  129. +
  130. +// printk(KERN_ERR "D: attempting to read %d @%lu from "
  131. +// "bdev %p devt %08X %s\n", blocksize, logic_sb_block,
  132. +// bdev, devt, format_dev_t(bff, devt));
  133. + if (!initcode_sync_page_read(bdev, logic_sb_block, blocksize, page)) {
  134. +// printk(KERN_ERR "D: failed!\n");
  135. + goto out;
  136. + }
  137. + es = (struct ext2_super_block *)(((char *)page_address(page)) + offset);
  138. + if (le16_to_cpu(es->s_magic) == EXT2_SUPER_MAGIC) {
  139. +// printk(KERN_ERR "D: has uuid "
  140. +// "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
  141. +// es->s_uuid[0], es->s_uuid[1], es->s_uuid[2], es->s_uuid[3],
  142. +// es->s_uuid[4], es->s_uuid[5], es->s_uuid[6], es->s_uuid[7],
  143. +// es->s_uuid[8], es->s_uuid[9], es->s_uuid[10], es->s_uuid[11],
  144. +// es->s_uuid[12], es->s_uuid[13], es->s_uuid[14], es->s_uuid[15]);
  145. + if (!memcmp(es->s_uuid, root_dev_uuid, 16))
  146. + rv = /* found */ 1;
  147. + }
  148. +// else printk(KERN_ERR "D: bad ext2fs magic\n");
  149. + out:
  150. + blkdev_put(bdev, FMODE_READ);
  151. + return (rv);
  152. +}
  153. +#endif /* CONFIG_EXT2_FS for UUID support */
  154. +
  155. static int __init load_ramdisk(char *str)
  156. {
  157. rd_doload = simple_strtol(str,NULL,0) & 3;
  158. @@ -148,6 +274,13 @@ done:
  159. static int __init root_dev_setup(char *line)
  160. {
  161. strlcpy(saved_root_name, line, sizeof(saved_root_name));
  162. +#ifdef CONFIG_EXT2_FS
  163. + root_dev_type = 0;
  164. + if (!strncmp(line, "UUID=", 5)) {
  165. + root_dev_type = 1;
  166. + parse_uuid(line + 5);
  167. + }
  168. +#endif /* CONFIG_EXT2_FS for UUID support */
  169. return 1;
  170. }
  171. @@ -333,6 +466,83 @@ void __init change_floppy(char *fmt, ...
  172. void __init mount_root(void)
  173. {
  174. +#ifdef CONFIG_EXT2_FS
  175. + /* UUID support */
  176. +// printk_all_partitions();
  177. + if (root_dev_type == 1) {
  178. + int blocksize;
  179. +
  180. + /* from block/genhd.c printk_all_partitions */
  181. + struct class_dev_iter iter;
  182. + struct device *dev;
  183. +
  184. + /* from drivers/md/md.c */
  185. + struct page *sb_page;
  186. +
  187. + if (!(sb_page = alloc_page(GFP_KERNEL))) {
  188. + printk(KERN_ERR "VFS: no memory for bio page\n");
  189. + goto nomemforbio;
  190. + }
  191. +
  192. +// printk(KERN_ERR "D: root is: "
  193. +// "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
  194. +// root_dev_uuid[0], root_dev_uuid[1], root_dev_uuid[2], root_dev_uuid[3],
  195. +// root_dev_uuid[4], root_dev_uuid[5], root_dev_uuid[6], root_dev_uuid[7],
  196. +// root_dev_uuid[8], root_dev_uuid[9], root_dev_uuid[10], root_dev_uuid[11],
  197. +// root_dev_uuid[12], root_dev_uuid[13], root_dev_uuid[14], root_dev_uuid[15]);
  198. + /* from block/genhd.c printk_all_partitions */
  199. +// printk(KERN_ERR "D: begin iter\n");
  200. + class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  201. + while (root_dev_type && (dev = class_dev_iter_next(&iter))) {
  202. +// char bff[22];
  203. + struct gendisk *disk = dev_to_disk(dev);
  204. + struct disk_part_iter piter;
  205. + struct hd_struct *part;
  206. + if (get_capacity(disk) == 0 ||
  207. + (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)) {
  208. +// printk(KERN_ERR "D: ignoring\n");
  209. + continue;
  210. + }
  211. + blocksize = queue_logical_block_size(disk->queue);
  212. +// printk(KERN_ERR "D: gendisk, blocksize %d "
  213. +// "name '%s' devt %08X %s #part %d\n", blocksize,
  214. +// disk->disk_name, dev->devt,
  215. +// format_dev_t(bff, dev->devt),
  216. +// disk_max_parts(disk));
  217. + disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
  218. + while (root_dev_type && (part = disk_part_iter_next(&piter))) {
  219. + /* avoid empty or too small partitions */
  220. +// printk(KERN_ERR "D: part #%d start %llu "
  221. +// "nr %llu\n", part->partno,
  222. +// (__u64)part->start_sect,
  223. +// (__u64)part->nr_sects);
  224. + if (part->nr_sects < 8)
  225. + continue;
  226. + if (check_dev(disk, MKDEV(MAJOR(dev->devt),
  227. + MINOR(dev->devt) + part->partno),
  228. + blocksize, sb_page)) {
  229. + ROOT_DEV = part_devt(part);
  230. +// printk(KERN_ERR "D: got match!\n");
  231. + // comment out below for debugging
  232. + root_dev_type = 0;
  233. + }
  234. + }
  235. + disk_part_iter_exit(&piter);
  236. + }
  237. +// printk(KERN_ERR "D: end iter\n");
  238. + class_dev_iter_exit(&iter);
  239. + put_page(sb_page);
  240. + }
  241. + nomemforbio:
  242. + if (root_dev_type == 1)
  243. + printk(KERN_ERR "VFS: Unable to find root by UUID %s.\n",
  244. + saved_root_name + 5);
  245. + else if (root_dev_type == 3)
  246. + /* execute deferred panic from parse_uuid */
  247. + panic("Badly formatted UUID %s was supplied as kernel "
  248. + "parameter root", saved_root_name + 5);
  249. +#endif /* CONFIG_EXT2_FS for UUID support */
  250. +
  251. #ifdef CONFIG_ROOT_NFS
  252. if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) {
  253. if (mount_nfs_root())