gentooinstall 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. # get max size of disk in sectors
  60. function get_max_size {
  61. maxsize=$(env LC_ALL=C parted $1 -s unit s print |awk '/^Disk/ { print $3 }'|sed -e 's/s//')
  62. rootsize=$(($maxsize-$cfgfssize))
  63. print device has $maxsize sectors. using $rootsize for root.
  64. }
  65. # create partition, with fstype start and end in sectors
  66. function create_partition {
  67. print creating partition on $1
  68. parted -s $1 unit s mkpart primary $2 $3 $4 > /dev/null 2>&1
  69. if [ $? -ne 0 ]; then
  70. echo "creating primary partition failed!"
  71. exit 1
  72. fi
  73. }
  74. function set_boot_flag {
  75. print setting bootflag on $1 partition $2
  76. parted -s $1 set $2 boot on > /dev/null 2>&1
  77. if [ $? -ne 0 ]; then
  78. echo "setting bootflag failed!"
  79. exit 1
  80. fi
  81. }
  82. function change_part_type {
  83. print setting partition type on $1 partition $2 to $3
  84. sfdisk --change-id $1 $2 $3 >/dev/null 2>&1
  85. if [ $? -ne 0 ]; then
  86. echo "changing partition type failed!"
  87. exit 1
  88. fi
  89. }
  90. function create_filesystem {
  91. print creating filesystem $2 on $1 partition $3
  92. mkfs.ext2 -j -F -q ${1}${3} >/dev/null 2>&1
  93. if [ $? -ne 0 ]; then
  94. echo "creating filesystem on partition failed!"
  95. exit 1
  96. fi
  97. }
  98. function mount_fs {
  99. print mounting ${1}${2} to $4 with filesystem $3
  100. mount -t $3 ${1}${2} $4
  101. if [ $? -ne 0 ]; then
  102. echo "mounting filesystem failed!"
  103. exit 1
  104. fi
  105. }
  106. function extract_archive {
  107. print extracting archive $1 onto $2
  108. tar -C $2 -xpf $1
  109. if [ $? -ne 0 ]; then
  110. echo "archive extraction failed!"
  111. exit 1
  112. fi
  113. }
  114. function create_chroot_installer {
  115. (
  116. emerge-websync
  117. emerge rsyslog
  118. rc-update add sshd default
  119. emerge grub:2
  120. print grub-mkconfig > /boot/grub/grub.cfg
  121. print Installing Grub into /dev/sda
  122. print grub-install /dev/sda
  123. ) >/mnt/install
  124. chmod 755 /mnt/install
  125. }
  126. function chroot_install {
  127. print Installing Gentoo
  128. mount -t proc proc /mnt/proc
  129. mount -t sysfs sys /mnt/sys
  130. mount -o bind /dev /mnt/dev
  131. mkdir /mnt/dev/shm
  132. mount -t tmpfs tmpfs /mnt/dev/shm
  133. chroot /mnt /install
  134. if [ $? -ne 0 ]; then
  135. echo "Gentoo installation failed!"
  136. exit 1
  137. fi
  138. }
  139. function fix_perm {
  140. print fixing permissions
  141. chmod 1777 ${1}/tmp
  142. }
  143. case $arch {
  144. (x86|x86_64)
  145. get_max_size $device
  146. create_label $device
  147. create_partition $device swap 0 $swapsize
  148. create_partition $device ext2 $(($swapsize+1)) $(($maxsize-1))
  149. set_boot_flag $device 1
  150. change_part_type $device 1 82
  151. partprobe $device
  152. sync
  153. create_filesystem $device $fs 1
  154. [[ -x /sbin/mdev ]] && mdev -s
  155. mount_fs $device 1 $fs /mnt
  156. extract_archive $archive /mnt
  157. create_chroot_installer
  158. chroot_install /mnt
  159. print "/dev/sda2 / ext4 defaults 0 1" > /mnt/etc/fstab
  160. print hostname=\"$hostname\" > /mnt/etc/conf.d/hostname
  161. fix_perm /mnt
  162. umount /mnt/dev/shm
  163. umount /mnt/{proc,dev,sys}
  164. umount /mnt
  165. ;;
  166. }
  167. echo "Successfully installed Gentoo on $target."
  168. exit 0