Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

make-base-vm 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/bin/sh
  2. set -e
  3. DISTRO=ubuntu
  4. SUITE=xenial
  5. ARCH=amd64
  6. MIRROR_BASE=http://${MIRROR_HOST:-127.0.0.1}:3142
  7. LXC=0
  8. VBOX=0
  9. usage() {
  10. echo "Usage: ${0##*/} [OPTION]..."
  11. echo "Make a base client."
  12. echo
  13. cat << EOF
  14. --help display this help and exit
  15. --distro D build distro D (e.g. debian) instead of ubuntu
  16. --suite U build suite U instead of xenial
  17. --arch A build architecture A (e.g. i386) instead of amd64
  18. --lxc use lxc instead of kvm
  19. --vbox use VirtualBox instead of kvm
  20. The MIRROR_HOST environment variable can be used to change the
  21. apt-cacher host. It should be something that both the host and the
  22. target VM can reach. It may be set to 127.0.0.1, in which case it will be
  23. changed to 10.0.2.2 on the guest (or GITIAN_HOST_IP if it is defined)
  24. 10.0.2.2 is the host IP as visible from the guest under qemu networking.
  25. The DEBOOTSTRAP_DIR (but also GITIAN_SUDO_USE_DEBOOTSTRAP_DIR, see below!)
  26. environment variable can be set to select a directory
  27. that will contain data like in "/usr/share/debootstrap/". This allows user to
  28. make a copy of this files to some local dir and modify them locally:
  29. e.g. set env variable "DEBOOTSTRAP_DIR=./mydeboot/", then copy or link
  30. system's version of files there, and modify them there
  31. (e.g. copy your debootstrap-script file "xenial" to "./mydeboot/scripts/").
  32. Set env GITIAN_SUDO_USE_DEBOOTSTRAP_DIR="yes" to allow sudo for debootstrap
  33. to use flags like --preserve-env that are required for DEBOOTSTRAP_DIR to work.
  34. It must be equal string "yes".
  35. This is done as separate variable to make it clear that we modify sudo
  36. behaviour here regarding security (though anyway env is cleared with
  37. whitelist so should be perfectly safe).
  38. EOF
  39. }
  40. if [ $# != 0 ] ; then
  41. while true ; do
  42. case "$1" in
  43. --help|-h)
  44. usage
  45. exit 0
  46. ;;
  47. --distro|-d)
  48. DISTRO="$2"
  49. shift 2
  50. ;;
  51. --suite|-s)
  52. SUITE="$2"
  53. shift 2
  54. ;;
  55. --arch|-a)
  56. ARCH="$2"
  57. shift 2
  58. ;;
  59. --lxc)
  60. LXC=1
  61. shift 1
  62. ;;
  63. --vbox)
  64. VBOX=1
  65. shift 1
  66. ;;
  67. --*)
  68. echo "unrecognized option $1"
  69. exit 1
  70. ;;
  71. *)
  72. break
  73. ;;
  74. esac
  75. done
  76. fi
  77. if [ $DISTRO = "ubuntu" ]; then
  78. MIRROR=$MIRROR_BASE/archive.ubuntu.com/ubuntu
  79. SECURITY_MIRROR=$MIRROR_BASE/security.ubuntu.com/ubuntu
  80. components=main,universe
  81. elif [ $DISTRO = "debian" ]; then
  82. MIRROR=$MIRROR_BASE/ftp.debian.org/debian
  83. SECURITY_MIRROR=$MIRROR_BASE/security.debian.org/
  84. components=main,contrib
  85. fi
  86. mkdir -p var
  87. if [ ! -e var/id_rsa ]; then
  88. ssh-keygen -t rsa -f var/id_rsa -N ""
  89. fi
  90. OUT=base-$SUITE-$ARCH
  91. FLAVOUR=virtual
  92. if [ $ARCH = "amd64" -a $SUITE = "hardy" ]; then
  93. FLAVOUR=server
  94. fi
  95. if [ $DISTRO = "debian" -a $ARCH = "amd64" ]; then
  96. FLAVOUR=amd64
  97. elif [ $DISTRO = "debian" -a $ARCH = "i386" -a \($SUITE = "squeeze" -o $SUITE = "lenny" -o $SUITE = "etch" -o $SUITE = "sarge" -o $SUITE = "woody" -o $SUITE = "potato" -o $SUITE = "slink" -o $SUITE = "hamm" -o $SUITE = "bo" -o $SUITE = "rex" -o $SUITE = "buzz"\) ]; then
  98. FLAVOUR=686
  99. elif [ $DISTRO = "debian" ]; then
  100. FLAVOUR=686-pae
  101. fi
  102. LOCALE_PKG=language-pack-en
  103. if [ $DISTRO = "debian" ]; then
  104. LOCALE_PKG=locales
  105. fi
  106. addpkg=pciutils,build-essential,git,subversion,$LOCALE_PKG,wget,lsb-release
  107. if [ $DISTRO = "ubuntu" ]; then
  108. # Need comma at end to work around an issue with apt for Debian <= Wheezy regarding empty strings
  109. #
  110. # If we left the comma down below when adding KERNEL_PKG to addpkg, the fact that KERNEL_PKG is undefined
  111. # if DISTRO is debian would result in two commas in a row (,,), which is interpreted by apt-get as the
  112. # package with the name empty string (""). This triggers a bug with apt versions < 1.0.3. So by adding the
  113. # comma to the end of KERNEL_PKG, we are including that comma if the distro is ubuntu (and therefore we do
  114. # have a kernel package that needs to be installed). If KERNEL_PKG is not set (i.e. we have Debian as the
  115. # distro), then we don't add that extra comma and therefore, we don't end up with two commas in a row.
  116. #
  117. # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=744940
  118. # http://anonscm.debian.org/cgit/apt/apt.git/commit/?h=1.0.3&id=d99854cac4065bc7b337815fb2116269d58dab73
  119. KERNEL_PKG=linux-image-generic,
  120. fi
  121. GRUB_PKG=grub
  122. if [ $DISTRO = "ubuntu" ]; then
  123. GRUB_PKG=grub-pc
  124. fi
  125. if [ $LXC = "1" ]; then
  126. addpkg=$addpkg,lxc
  127. if [ $DISTRO = "debian" ]; then
  128. addpkg=$addpkg,sudo
  129. fi
  130. else
  131. # Lack of comma after KERNEL_PKG is not a typo
  132. addpkg=$addpkg,${KERNEL_PKG}${GRUB_PKG},openssh-server
  133. fi
  134. # Remove cron to work around vmbuilder issue when umounting /dev on target
  135. removepkg=cron
  136. if [ $VBOX = "1" ]; then
  137. NAME="$SUITE-$ARCH"
  138. if ! vagrant status | grep "$NAME" | grep "not created" > /dev/null; then
  139. echo "Vagrant machine "$NAME" already exists, please remove it first (vagrant destroy "$NAME")"
  140. exit 1
  141. fi
  142. DISTRO_USER_CREATE=0
  143. if [ $DISTRO = "debian" ]; then
  144. # we use a vagrant provider
  145. DISTRO_USER_CREATE=1
  146. fi
  147. vagrant up "$NAME"
  148. if [ $DISTRO_USER_CREATE = "1" ]; then
  149. vagrant ssh "$NAME" -c "sudo useradd -m -s /bin/bash $DISTRO"
  150. fi
  151. vagrant ssh "$NAME" -c "sudo mkdir -p /root/.ssh && sudo chmod 700 /root/.ssh"
  152. vagrant ssh "$NAME" -c "sudo sh -c 'cat >> /root/.ssh/authorized_keys'" < var/id_rsa.pub
  153. vagrant ssh "$NAME" -c "sudo -u $DISTRO mkdir -p /home/$DISTRO/.ssh && sudo -u $DISTRO chmod 700 /home/$DISTRO/.ssh"
  154. vagrant ssh "$NAME" -c "sudo sh -c 'cat >> /home/$DISTRO/.ssh/authorized_keys'" < var/id_rsa.pub
  155. VBoxManage snapshot "Gitian-$NAME" take "Gitian-Clean"
  156. vagrant suspend "$NAME"
  157. exit 0
  158. fi
  159. if [ $LXC = "1" ]; then
  160. if [ -e $OUT ]; then
  161. echo $OUT already exists, please remove it first
  162. exit 1
  163. fi
  164. sudo rm -rf $OUT-bootstrap
  165. # Need universe for lxc in lucid
  166. unset preserve_env
  167. if [ "$GITIAN_SUDO_USE_DEBOOTSTRAP_DIR" = "yes" ]; then
  168. echo "sudo will preserve (some) env flags"
  169. preserve_env=yes # if you would want to set false then unset this variable
  170. fi
  171. env -i LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 DEBOOTSTRAP_DIR="$DEBOOTSTRAP_DIR" sudo ${preserve_env+--preserve-env} debootstrap --arch=$ARCH --include=$addpkg --exclude=$removepkg --components=$components $SUITE $OUT-bootstrap $MIRROR
  172. # Fix lxc issue
  173. if [ -f $OUT-bootstrap/usr/lib/lxc/lxc-init ]
  174. then
  175. sudo cp $OUT-bootstrap/usr/lib/lxc/lxc-init $OUT-bootstrap/usr/sbin/init.lxc
  176. else
  177. if [ $ARCH = "amd64" ]
  178. then
  179. if [ -f $OUT-bootstrap/usr/lib/x86_64-linux-gnu/lxc/lxc-init ]
  180. then
  181. sudo cp $OUT-bootstrap/usr/lib/x86_64-linux-gnu/lxc/lxc-init $OUT-bootstrap/usr/sbin/init.lxc
  182. fi
  183. else
  184. if [ -f $OUT-bootstrap/usr/lib/i386-linux-gnu/lxc/lxc-init ]
  185. then
  186. sudo cp $OUT-bootstrap/usr/lib/i386-linux-gnu/lxc/lxc-init $OUT-bootstrap/usr/sbin/init.lxc
  187. fi
  188. fi
  189. fi
  190. dd if=/dev/zero of=$OUT-lxc bs=1M count=1 seek=10240
  191. /sbin/mkfs.ext4 -F $OUT-lxc
  192. t=`mktemp -d gitian.XXXXXXXX`
  193. sudo mount $OUT-lxc $t
  194. sudo cp -a $OUT-bootstrap/* $t
  195. sudo umount $t
  196. rmdir $t
  197. sudo rm -rf $OUT-bootstrap
  198. mv $OUT-lxc $OUT
  199. # bootstrap-fixup is done in libexec/make-clean-vm
  200. else
  201. if [ -e $OUT.qcow2 ]; then
  202. echo $OUT.qcow2 already exists, please remove it first
  203. exit 1
  204. fi
  205. libexec/config-bootstrap-fixup
  206. rm -rf $OUT
  207. env -i LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 sudo vmbuilder kvm $DISTRO --rootsize 10240 --arch=$ARCH --suite=$SUITE --addpkg=$addpkg --removepkg=$removepkg --ssh-key=var/id_rsa.pub --ssh-user-key=var/id_rsa.pub --mirror=$MIRROR --security-mirror=$SECURITY_MIRROR --dest=$OUT --flavour=$FLAVOUR --firstboot=`pwd`/target-bin/bootstrap-fixup
  208. mv $OUT/*.qcow2 $OUT.qcow2
  209. rm -rf $OUT
  210. # bootstrap-fixup is done on first boot
  211. fi