gentooinstall 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/bin/mksh
  2. # This file is part of the OpenADK project.
  3. # install Gentoo to a block/flash device
  4. if [ $(id -u) -ne 0 ]; then
  5. print installation is only possible as root
  6. exit 1
  7. fi
  8. # get architecture
  9. arch=$(uname -m)
  10. # get adk target system
  11. target=$(cat /etc/.adktarget)
  12. if [ -z $target ]; then
  13. print autodetection of target failed
  14. exit 1
  15. fi
  16. function help {
  17. cat >&2 <<EOF
  18. Syntax: gentooinstall <archive> <device> <hostname>
  19. EOF
  20. exit 1
  21. }
  22. if [ -z $1 ]; then
  23. print no archive given
  24. help
  25. fi
  26. if [ -z $2 ]; then
  27. print no device given
  28. help
  29. fi
  30. if [ -z $3 ]; then
  31. print no hostname given
  32. help
  33. fi
  34. archive=$1
  35. device=$2
  36. hostname=$3
  37. swapsize=2048000
  38. fs=ext4
  39. tools="parted partprobe sfdisk mkfs.ext2"
  40. f=0
  41. for tool in $tools;do
  42. if ! which $tool >/dev/null; then
  43. echo "checking if $tool is installed... failed"
  44. f=1
  45. fi
  46. done
  47. if [ $f -eq 1 ]; then
  48. exit 1
  49. fi
  50. # create empty partition table
  51. function create_label {
  52. print "creating empty partition table"
  53. parted -s $1 mklabel msdos > /dev/null 2>&1
  54. if [ $? -ne 0 ]; then
  55. echo "creating empty partition failed!"
  56. exit 1
  57. fi
  58. }
  59. # create partition, with fstype start and end in sectors
  60. function create_partition {
  61. print creating partition on $1 with $2
  62. parted -s $1 -- unit MiB mkpart primary $2 $3 $4 > /dev/null 2>&1
  63. if [ $? -ne 0 ]; then
  64. echo "creating primary partition failed!"
  65. exit 1
  66. fi
  67. }
  68. function set_boot_flag {
  69. print setting bootflag on $1 partition $2
  70. parted -s $1 set $2 boot on > /dev/null 2>&1
  71. if [ $? -ne 0 ]; then
  72. echo "setting bootflag failed!"
  73. exit 1
  74. fi
  75. }
  76. function change_part_type {
  77. print setting partition type on $1 partition $2 to $3
  78. sfdisk --change-id $1 $2 $3 >/dev/null 2>&1
  79. if [ $? -ne 0 ]; then
  80. echo "changing partition type failed!"
  81. exit 1
  82. fi
  83. }
  84. function create_filesystem {
  85. print creating filesystem $2 on $1 partition $3
  86. mkfs.ext2 -j -F -q ${1}${3} >/dev/null 2>&1
  87. if [ $? -ne 0 ]; then
  88. echo "creating filesystem on partition failed!"
  89. exit 1
  90. fi
  91. }
  92. function mount_fs {
  93. print mounting ${1}${2} to $4 with filesystem $3
  94. mount -t $3 ${1}${2} $4
  95. if [ $? -ne 0 ]; then
  96. echo "mounting filesystem failed!"
  97. exit 1
  98. fi
  99. }
  100. function extract_archive {
  101. print extracting archive $1 onto $2
  102. tar -C $2 -xpf $1
  103. if [ $? -ne 0 ]; then
  104. echo "archive extraction failed!"
  105. exit 1
  106. fi
  107. }
  108. function create_chroot_installer {
  109. (
  110. print emerge-webrsync
  111. print emerge dhcpcd
  112. print rc-update add sshd default
  113. print emerge grub:2
  114. ) >/mnt/install
  115. chmod 755 /mnt/install
  116. }
  117. function create_chroot_grubinstaller {
  118. (
  119. print 'grub-mkconfig > /boot/grub/grub.cfg'
  120. print grub-install /dev/sda
  121. ) >/mnt/installgrub
  122. chmod 755 /mnt/installgrub
  123. }
  124. function chroot_install {
  125. print Installing Gentoo
  126. cat /etc/resolv.conf > /mnt/etc/resolv.conf
  127. mount -t proc proc /mnt/proc
  128. mount -t sysfs sys /mnt/sys
  129. mount -o bind /dev /mnt/dev
  130. mount -t tmpfs tmpfs /mnt/dev/shm
  131. chroot /mnt /install
  132. if [ $? -ne 0 ]; then
  133. echo "Gentoo installation failed!"
  134. umount /mnt/dev/shm
  135. umount /mnt/{proc,dev,sys}
  136. umount /mnt
  137. exit 1
  138. fi
  139. printf "Now manually install a kernel"
  140. chroot /mnt /bin/bash
  141. printf "Now installang bootloader"
  142. chroot /mnt /installgrub
  143. if [ $? -ne 0 ]; then
  144. echo "Gentoo installation failed!"
  145. umount /mnt/dev/shm
  146. umount /mnt/{proc,dev,sys}
  147. umount /mnt
  148. exit 1
  149. fi
  150. }
  151. case $arch {
  152. (x86|x86_64)
  153. create_label $device
  154. create_partition $device sw 2 2048
  155. create_partition $device ext2 2049 -1
  156. set_boot_flag $device 2
  157. partprobe $device
  158. sync
  159. create_filesystem $device $fs 2
  160. [[ -x /sbin/mdev ]] && mdev -s
  161. mount_fs $device 2 $fs /mnt
  162. extract_archive $archive /mnt
  163. create_chroot_installer
  164. chroot_install /mnt
  165. print "/dev/sda2 / ext4 defaults 0 1" > /mnt/etc/fstab
  166. print hostname=\"$hostname\" > /mnt/etc/conf.d/hostname
  167. chmod 1777 /mnt/tmp
  168. sync
  169. umount /mnt/dev/shm
  170. umount /mnt/{proc,dev,sys}
  171. umount /mnt
  172. ;;
  173. }
  174. echo "Successfully installed Gentoo on $target."
  175. exit 0