Advertisement
Guest User

Untitled

a guest
Jul 21st, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # This script will setup booting from disk (USB or harddrive)
  4. #
  5. # If you see this file in a text editor instead of getting it executed,
  6. # then it is missing executable permissions (chmod). You can try to set
  7. # exec permissions for this file by using: chmod a+x bootinst.sh
  8. # Alternatively, you may try to run bootinst.bat file instead
  9. #
  10. # Scrolling down will reveal the actual code of this script.
  11. #
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. # if we're running this from X, re-run the script in konsole or xterm
  65. if [ "$DISPLAY" != "" ]; then
  66. if [ "$1" != "--rex" -a "$2" != "--rex" ]; then
  67. konsole --nofork -e /bin/sh $0 --rex 2>/dev/null || xterm -e /bin/sh $0 --rex 2>/dev/null || /bin/sh $0 --rex 2>/dev/null
  68. exit
  69. fi
  70. fi
  71.  
  72. # make sure I am root
  73. if [ "$UID" != "0" -a "$UID" != "" ]; then
  74. echo ""
  75. echo "You are not root. You must run bootinst script as root."
  76. echo "The bootinst script needs direct access to your boot device."
  77. echo "Use sudo or kdesudo or similar wrapper to execute this."
  78. read junk
  79. exit 1
  80. fi
  81.  
  82. # change working directory to dir from which we are started
  83. CWD="$(pwd)"
  84. BOOT="$(dirname "$0")"
  85. BOOT="$(realpath "$BOOT" 2>/dev/null || echo $BOOT)"
  86. cd "$BOOT"
  87.  
  88. # find out device and mountpoint
  89. PART="$(df . | tail -n 1 | tr -s " " | cut -d " " -f 1)"
  90. DEV="$(echo "$PART" | sed -r "s:[0-9]+\$::" | sed -r "s:([0-9])[a-z]+\$:\\1:i")" #"
  91.  
  92. # check if disk is already bootable. Mostly for Windows discovery
  93. if [ "$(fdisk -l "$DEV" | fgrep "$DEV" | fgrep "*")" != "" ]; then
  94. echo ""
  95. echo "Partition $PART seems to be located on a physical disk,"
  96. echo "which is already bootable. If you continue, your drive $DEV"
  97. echo "will boot only Slax by default."
  98. echo "Press [Enter] to continue, or [Ctrl+C] to abort..."
  99. read junk
  100. fi
  101.  
  102. ARCH=$(uname -m)
  103. if [ "$ARCH" = "x86_64" ]; then ARCH=64; else ARCH=32; fi
  104. EXTLINUX=extlinux.x$ARCH
  105.  
  106. if [ ! -x ./$EXTLINUX ]; then
  107. # extlinux is not executable. There are two possible reasons:
  108. # either the fs is mounted with noexec, or file perms are wrong.
  109. # Try to fix both, no fail on error yet
  110. mount -o remount,exec $DEV
  111. chmod a+x ./$EXTLINUX
  112. fi
  113.  
  114. if [ ! -x ./$EXTLINUX ]; then
  115. # extlinux is still not executable. As a last try, copy it to .exe
  116. # because the partition may be mounted with showexec option, from which
  117. # we probably can't escape by remount
  118. cp -f ./$EXTLINUX ./extlinux.exe
  119. EXTLINUX=extlinux.exe
  120. fi
  121.  
  122. # install syslinux bootloader
  123. echo "* attempting to install bootloader to $BOOT..."
  124.  
  125. ./"$EXTLINUX" --install "$BOOT"
  126.  
  127. if [ $? -ne 0 ]; then
  128. echo "Error installing boot loader."
  129. echo "Read the errors above and press enter to exit..."
  130. read junk
  131. exit 1
  132. fi
  133.  
  134. if [ "$DEV" != "$PART" ]; then
  135. # Setup MBR on the first block
  136. echo "* setup MBR on $DEV"
  137. dd bs=440 count=1 conv=notrunc if="$BOOT/mbr.bin" of="$DEV" 2>/dev/null
  138.  
  139. # Toggle bootable flags
  140. echo "* set bootable flag for $PART"
  141. PART="$(echo "$PART" | sed -r "s:.*[^0-9]::")"
  142. (
  143. fdisk -l "$DEV" | fgrep "*" | fgrep "$DEV" | cut -d " " -f 1 \
  144. | sed -r "s:.*[^0-9]::" | xargs -I '{}' echo -ne "a\n{}\n"
  145. echo a
  146. echo $PART
  147. echo w
  148. ) | fdisk $DEV >/dev/null 2>&1
  149. fi
  150.  
  151. echo "Boot installation finished."
  152. echo "Press Enter..."
  153. read junk
  154. cd "$CWD"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement