0026-x86-use-PARTUUID-instead-explicitly-specifying-the-device-by-default.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. From: Matthias Schiffer <mschiffer@universe-factory.net>
  2. Date: Mon, 23 Mar 2015 21:11:41 +0100
  3. Subject: x86: use PARTUUID instead explicitly specifying the device by default
  4. This changes the x86 image generation to match x86_64, using the PARTUUID for
  5. the rootfs instead of explicitly configuring the device.
  6. It unbreaks KVM with VirtIO, which uses /dev/vda2 instead of /dev/sda2.
  7. Tested in QEMU/KVM with VirtIO, VirtualBox and VMware.
  8. diff --git a/config/Config-images.in b/config/Config-images.in
  9. index 39e51e4..f44ec73 100644
  10. --- a/config/Config-images.in
  11. +++ b/config/Config-images.in
  12. @@ -267,8 +267,6 @@ menu "Target Images"
  13. config TARGET_ROOTFS_PARTNAME
  14. string "Root partition on target device"
  15. depends on OLPC_BOOTSCRIPT_IMAGES || GRUB_IMAGES
  16. - default "/dev/xvda2" if TARGET_x86_xen_domu
  17. - default "/dev/sda2" if ! TARGET_x86_xen_domu
  18. help
  19. The root partition on the final device. If you don't know,
  20. you probably want the default (/dev/sda2).
  21. diff --git a/target/linux/x86/base-files/lib/preinit/79_move_config b/target/linux/x86/base-files/lib/preinit/79_move_config
  22. index 0bffbab..1d4873d 100644
  23. --- a/target/linux/x86/base-files/lib/preinit/79_move_config
  24. +++ b/target/linux/x86/base-files/lib/preinit/79_move_config
  25. @@ -1,21 +1,14 @@
  26. #!/bin/sh
  27. -# Copyright (C) 2012 OpenWrt.org
  28. +# Copyright (C) 2012-2015 OpenWrt.org
  29. move_config() {
  30. - local rootfsdev
  31. - local rootfstype
  32. -
  33. - rootfstype="$(awk 'BEGIN { RS=" "; FS="="; } ($1 == "rootfstype") { print $2 }' < /proc/cmdline)"
  34. - case "$rootfstype" in
  35. - squashfs|jffs2)
  36. - rootfsdev="$(awk 'BEGIN { RS=" "; FS="="; } ($1 == "block2mtd.block2mtd") { print substr($2,1,index($2, ",")-1) }' < /proc/cmdline)";;
  37. - ext4)
  38. - rootfsdev="$(awk 'BEGIN { RS=" "; FS="="; } ($1 == "root") { print $2 }' < /proc/cmdline)";;
  39. - esac
  40. + . /lib/upgrade/platform.sh
  41. - mount -t ext4 -o rw,noatime "${rootfsdev%[0-9]}1" /mnt
  42. - mv -f /mnt/sysupgrade.tgz /
  43. - umount /mnt
  44. + if platform_export_bootpart; then
  45. + mount -t ext4 -o rw,noatime "$BOOTPART" /mnt
  46. + mv -f /mnt/sysupgrade.tgz /
  47. + umount /mnt
  48. + fi
  49. }
  50. boot_hook_add preinit_mount_root move_config
  51. diff --git a/target/linux/x86/base-files/lib/upgrade/platform.sh b/target/linux/x86/base-files/lib/upgrade/platform.sh
  52. index a2dd20a..73ab5ef 100644
  53. --- a/target/linux/x86/base-files/lib/upgrade/platform.sh
  54. +++ b/target/linux/x86/base-files/lib/upgrade/platform.sh
  55. @@ -1,16 +1,38 @@
  56. -x86_get_rootfs() {
  57. - local rootfsdev
  58. - local rootfstype
  59. -
  60. - rootfstype="$(awk 'BEGIN { RS=" "; FS="="; } ($1 == "rootfstype") { print $2 }' < /proc/cmdline)"
  61. - case "$rootfstype" in
  62. - squashfs|jffs2)
  63. - rootfsdev="$(awk 'BEGIN { RS=" "; FS="="; } ($1 == "block2mtd.block2mtd") { print substr($2,1,index($2, ",")-1) }' < /proc/cmdline)";;
  64. - ext4)
  65. - rootfsdev="$(awk 'BEGIN { RS=" "; FS="="; } ($1 == "root") { print $2 }' < /proc/cmdline)";;
  66. - esac
  67. -
  68. - echo "$rootfstype:$rootfsdev"
  69. +platform_export_bootpart() {
  70. + local cmdline uuid disk
  71. +
  72. + if read cmdline < /proc/cmdline; then
  73. + case "$cmdline" in
  74. + *block2mtd=*)
  75. + disk="${cmdline##*block2mtd=}"
  76. + disk="${disk%%,*}"
  77. + ;;
  78. + *root=*)
  79. + disk="${cmdline##*root=}"
  80. + disk="${disk%% *}"
  81. + ;;
  82. + esac
  83. +
  84. + case "$disk" in
  85. + PARTUUID=[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]-02)
  86. + uuid="${disk#PARTUUID=}"
  87. + uuid="${uuid%-02}"
  88. + for disk in /dev/[hsv]d[a-z]; do
  89. + set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
  90. + if [ "$4$3$2$1" = "$uuid" ]; then
  91. + export BOOTPART="${disk}1"
  92. + return 0
  93. + fi
  94. + done
  95. + ;;
  96. + /dev/*)
  97. + export BOOTPART="${disk%[0-9]}1"
  98. + return 0
  99. + ;;
  100. + esac
  101. + fi
  102. +
  103. + return 1
  104. }
  105. platform_check_image() {
  106. @@ -26,19 +48,19 @@ platform_check_image() {
  107. }
  108. platform_copy_config() {
  109. - local rootfs="$(x86_get_rootfs)"
  110. - local rootfsdev="${rootfs##*:}"
  111. -
  112. - mount -t ext4 -o rw,noatime "${rootfsdev%[0-9]}1" /mnt
  113. - cp -af "$CONF_TAR" /mnt/
  114. - umount /mnt
  115. + if [ -b "$BOOTPART" ]; then
  116. + mount -t ext4 -o rw,noatime "$BOOTPART" /mnt
  117. + cp -af "$CONF_TAR" /mnt/
  118. + umount /mnt
  119. + fi
  120. }
  121. platform_do_upgrade() {
  122. - local rootfs="$(x86_get_rootfs)"
  123. - local rootfsdev="${rootfs##*:}"
  124. + platform_export_bootpart
  125. - sync
  126. - [ -b ${rootfsdev%[0-9]} ] && get_image "$@" | dd of=${rootfsdev%[0-9]} bs=4096 conv=fsync
  127. - sleep 1
  128. + if [ -b "${BOOTPART%[0-9]}" ]; then
  129. + sync
  130. + get_image "$@" | dd of="${BOOTPART%[0-9]}" bs=4096 conv=fsync
  131. + sleep 1
  132. + fi
  133. }
  134. diff --git a/target/linux/x86/image/Makefile b/target/linux/x86/image/Makefile
  135. index 5983718..1f91b9f 100644
  136. --- a/target/linux/x86/image/Makefile
  137. +++ b/target/linux/x86/image/Makefile
  138. @@ -40,7 +40,9 @@ ifneq ($(GRUB_TERMINALS),)
  139. GRUB_TERMINAL_CONFIG := terminal_input $(GRUB_TERMINALS); terminal_output $(GRUB_TERMINALS)
  140. endif
  141. +SIGNATURE:=$(shell dd if=/dev/urandom bs=4 count=1 2>/dev/null | hexdump -v -e '"%08x"')
  142. ROOTPART:=$(call qstrip,$(CONFIG_TARGET_ROOTFS_PARTNAME))
  143. +ROOTPART:=$(if $(ROOTPART),$(ROOTPART),PARTUUID=$(SIGNATURE)-02)
  144. GRUB_TIMEOUT:=$(call qstrip,$(CONFIG_GRUB_TIMEOUT))
  145. @@ -82,7 +84,7 @@ ifneq ($(CONFIG_GRUB_IMAGES),)
  146. -e 's#@CMDLINE@#$(strip $(call Image/cmdline/$(1)) $(BOOTOPTS) $(GRUB_CONSOLE_CMDLINE))#g' \
  147. -e 's#@TIMEOUT@#$(GRUB_TIMEOUT)#g' \
  148. ./grub.cfg > $(KDIR)/root.grub/boot/grub/grub.cfg
  149. - PADDING="$(CONFIG_TARGET_IMAGES_PAD)" PATH="$(TARGET_PATH)" ./gen_image_generic.sh \
  150. + PADDING="$(CONFIG_TARGET_IMAGES_PAD)" SIGNATURE="$(SIGNATURE)" PATH="$(TARGET_PATH)" ./gen_image_generic.sh \
  151. $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).img \
  152. $(CONFIG_TARGET_KERNEL_PARTSIZE) $(KDIR)/root.grub \
  153. $(CONFIG_TARGET_ROOTFS_PARTSIZE) $(KDIR)/root.$(1) \
  154. diff --git a/target/linux/x86/image/gen_image_generic.sh b/target/linux/x86/image/gen_image_generic.sh
  155. index 9d11efb..3fb31f6 100755
  156. --- a/target/linux/x86/image/gen_image_generic.sh
  157. +++ b/target/linux/x86/image/gen_image_generic.sh
  158. @@ -20,7 +20,7 @@ sect=63
  159. cyl=$(( ($KERNELSIZE + $ROOTFSSIZE) * 1024 * 1024 / ($head * $sect * 512)))
  160. # create partition table
  161. -set `ptgen -o "$OUTPUT" -h $head -s $sect -p ${KERNELSIZE}m -p ${ROOTFSSIZE}m ${ALIGN:+-l $ALIGN}`
  162. +set `ptgen -o "$OUTPUT" -h $head -s $sect -p ${KERNELSIZE}m -p ${ROOTFSSIZE}m ${ALIGN:+-l $ALIGN} ${SIGNATURE:+-S 0x$SIGNATURE}`
  163. KERNELOFFSET="$(($1 / 512))"
  164. KERNELSIZE="$(($2 / 512))"