You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

make-base-vm 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 = "debian" -a $LXC = "1" ]; then
  78. echo "There is no support for Debian guests using LXC currently. Please use KVM or another distro for now."
  79. exit 1
  80. fi
  81. if [ $DISTRO = "ubuntu" ]; then
  82. MIRROR=$MIRROR_BASE/archive.ubuntu.com/ubuntu
  83. SECURITY_MIRROR=$MIRROR_BASE/security.ubuntu.com/ubuntu
  84. elif [ $DISTRO = "debian" ]; then
  85. MIRROR=$MIRROR_BASE/ftp.debian.org/debian
  86. SECURITY_MIRROR=$MIRROR_BASE/security.debian.org/
  87. fi
  88. mkdir -p var
  89. if [ ! -e var/id_rsa ]; then
  90. ssh-keygen -t rsa -f var/id_rsa -N ""
  91. fi
  92. OUT=base-$SUITE-$ARCH
  93. FLAVOUR=virtual
  94. if [ $ARCH = "amd64" -a $SUITE = "hardy" ]; then
  95. FLAVOUR=server
  96. fi
  97. if [ $DISTRO = "debian" -a $ARCH = "amd64" ]; then
  98. FLAVOUR=amd64
  99. 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
  100. FLAVOUR=686
  101. elif [ $DISTRO = "debian" ]; then
  102. FLAVOUR=686-pae
  103. fi
  104. LOCALE_PKG=language-pack-en
  105. if [ $DISTRO = "debian" ]; then
  106. LOCALE_PKG=locales
  107. fi
  108. addpkg=pciutils,build-essential,git-core,subversion,$LOCALE_PKG,wget,lsb-release
  109. if [ $DISTRO = "ubuntu" ]; then
  110. # Need comma at end to work around an issue with apt for Debian <= Wheezy regarding empty strings
  111. #
  112. # If we left the comma down below when adding KERNEL_PKG to addpkg, the fact that KERNEL_PKG is undefined
  113. # if DISTRO is debian would result in two commas in a row (,,), which is interpreted by apt-get as the
  114. # package with the name empty string (""). This triggers a bug with apt versions < 1.0.3. So by adding the
  115. # comma to the end of KERNEL_PKG, we are including that comma if the distro is ubuntu (and therefore we do
  116. # have a kernel package that needs to be installed). If KERNEL_PKG is not set (i.e. we have Debian as the
  117. # distro), then we don't add that extra comma and therefore, we don't end up with two commas in a row.
  118. #
  119. # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=744940
  120. # http://anonscm.debian.org/cgit/apt/apt.git/commit/?h=1.0.3&id=d99854cac4065bc7b337815fb2116269d58dab73
  121. KERNEL_PKG=linux-image-generic,
  122. fi
  123. GRUB_PKG=grub
  124. if [ $DISTRO = "ubuntu" ]; then
  125. GRUB_PKG=grub-pc
  126. fi
  127. if [ $LXC = "1" ]; then
  128. addpkg=$addpkg,lxc
  129. else
  130. # Lack of comma after KERNEL_PKG is not a typo
  131. addpkg=$addpkg,${KERNEL_PKG}${GRUB_PKG},openssh-server
  132. fi
  133. # Remove cron to work around vmbuilder issue when umounting /dev on target
  134. removepkg=cron
  135. if [ $VBOX = "1" ]; then
  136. NAME="$SUITE-$ARCH"
  137. if ! vagrant status | grep "$NAME" | grep "not created" > /dev/null; then
  138. echo "Vagrant machine "$NAME" already exists, please remove it first (vagrant destroy "$NAME")"
  139. exit 1
  140. fi
  141. vagrant up "$NAME"
  142. vagrant ssh "$NAME" -c "sudo mkdir -p /root/.ssh && sudo chmod 700 /root/.ssh"
  143. vagrant ssh "$NAME" -c "sudo sh -c 'cat >> /root/.ssh/authorized_keys'" < var/id_rsa.pub
  144. vagrant ssh "$NAME" -c "sudo -u $DISTRO mkdir -p /home/$DISTRO/.ssh && sudo -u $DISTRO chmod 700 /home/$DISTRO/.ssh"
  145. vagrant ssh "$NAME" -c "sudo sh -c 'cat >> /home/$DISTRO/.ssh/authorized_keys'" < var/id_rsa.pub
  146. VBoxManage snapshot "Gitian-$NAME" take "Gitian-Clean"
  147. vagrant suspend "$NAME"
  148. exit 0
  149. fi
  150. if [ $LXC = "1" ]; then
  151. if [ -e $OUT ]; then
  152. echo $OUT already exists, please remove it first
  153. exit 1
  154. fi
  155. sudo rm -rf $OUT-bootstrap
  156. # Need universe for lxc in lucid
  157. unset preserve_env
  158. if [ "$GITIAN_SUDO_USE_DEBOOTSTRAP_DIR" = "yes" ]; then
  159. echo "sudo will preserve (some) env flags"
  160. preserve_env=yes # if you would want to set false then unset this variable
  161. fi
  162. 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=main,universe $SUITE $OUT-bootstrap $MIRROR
  163. dd if=/dev/zero of=$OUT-lxc bs=1M count=1 seek=10240
  164. /sbin/mkfs.ext4 -F $OUT-lxc
  165. t=`mktemp -d gitian.XXXXXXXX`
  166. sudo mount $OUT-lxc $t
  167. sudo cp -a $OUT-bootstrap/* $t
  168. sudo umount $t
  169. rmdir $t
  170. sudo rm -rf $OUT-bootstrap
  171. mv $OUT-lxc $OUT
  172. # bootstrap-fixup is done in libexec/make-clean-vm
  173. else
  174. if [ -e $OUT.qcow2 ]; then
  175. echo $OUT.qcow2 already exists, please remove it first
  176. exit 1
  177. fi
  178. libexec/config-bootstrap-fixup
  179. rm -rf $OUT
  180. 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
  181. mv $OUT/*.qcow2 $OUT.qcow2
  182. rm -rf $OUT
  183. # bootstrap-fixup is done on first boot
  184. fi