50mounted-tests 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/sh
  2. # Sub-tests that require a mounted partition.
  3. set -e
  4. partition="$1"
  5. . /usr/share/os-prober/common.sh
  6. do_unmount() {
  7. if [ "$mounted" ]; then
  8. if ! umount "$tmpmnt"; then
  9. warn "failed to umount $tmpmnt"
  10. fi
  11. fi
  12. if [ -e "$dm_device" ]
  13. then
  14. debug "remove device mapper device $dm_device"
  15. dmsetup remove $dm_device
  16. fi
  17. rmdir "$tmpmnt" || true
  18. }
  19. types="$(fs_type "$partition")"
  20. if [ "$types" = NOT-DETECTED ]; then
  21. debug "$1 type not recognised; skipping"
  22. exit 0
  23. elif [ "$types" = swap ]; then
  24. debug "$1 is a swap partition; skipping"
  25. exit 0
  26. elif [ "$types" = crypto_LUKS ]; then
  27. debug "$1 is a LUKS partition; skipping"
  28. exit 0
  29. elif [ "$types" = LVM2_member ]; then
  30. debug "$1 is an LVM member; skipping"
  31. exit 0
  32. elif [ "$types" = ntfs ]; then
  33. if type ntfs-3g >/dev/null 2>&1; then
  34. types='ntfs-3g ntfs'
  35. fi
  36. elif [ -z "$types" ]; then
  37. if type cryptsetup >/dev/null 2>&1 && \
  38. cryptsetup luksDump "$partition" >/dev/null 2>&1; then
  39. debug "$1 is a LUKS partition; skipping"
  40. exit 0
  41. fi
  42. for type in $(grep -v nodev /proc/filesystems); do
  43. # hfsplus filesystems are mountable as hfs. Try hfs last so
  44. # that we can tell the difference.
  45. if [ "$type" = hfs ]; then
  46. delaytypes="${delaytypes:+$delaytypes }$type"
  47. elif [ "$type" = fuseblk ]; then
  48. if type ntfs-3g >/dev/null 2>&1; then
  49. types="${types:+$types }ntfs-3g"
  50. fi
  51. else
  52. types="${types:+$types }$type"
  53. fi
  54. done
  55. fi
  56. tmpmnt=/var/lib/os-prober/mount
  57. if [ ! -d "$tmpmnt" ]; then
  58. mkdir "$tmpmnt"
  59. fi
  60. mounted=
  61. if type grub-mount >/dev/null 2>&1 && \
  62. type grub-probe >/dev/null 2>&1 && \
  63. grub-mount "$partition" "$tmpmnt" 2>/dev/null; then
  64. mounted=1
  65. type="$(grub-probe -d "$partition" -t fs)" || true
  66. if [ "$type" ]; then
  67. debug "mounted using GRUB $type filesystem driver"
  68. else
  69. debug "mounted using GRUB, but unknown filesystem?"
  70. type=fuseblk
  71. fi
  72. elif dm_device="$(do_dmsetup osprober "$partition")" && \
  73. [ "$dm_device" ]; then
  74. for type in $types $delaytypes; do
  75. if mountinfo=`mount -o ro -t "$type" "$dm_device" "$tmpmnt" 2>&1`; then
  76. debug "mounted as $type filesystem"
  77. mounted=1
  78. break
  79. else
  80. debug "mounting $dm_device ($partition) as $type failed: $mountinfo"
  81. fi
  82. done
  83. fi
  84. if [ "$mounted" ]; then
  85. for test in /usr/lib/os-probes/mounted/*; do
  86. debug "running subtest $test"
  87. if [ -f "$test" ] && [ -x "$test" ]; then
  88. if "$test" "$partition" "$tmpmnt" "$type"; then
  89. debug "os found by subtest $test"
  90. do_unmount
  91. exit 0
  92. fi
  93. fi
  94. done
  95. fi
  96. do_unmount
  97. # No tests found anything.
  98. exit 1