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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. vagrant up "$NAME"
  143. vagrant ssh "$NAME" -c "sudo mkdir -p /root/.ssh && sudo chmod 700 /root/.ssh"
  144. vagrant ssh "$NAME" -c "sudo sh -c 'cat >> /root/.ssh/authorized_keys'" < var/id_rsa.pub
  145. vagrant ssh "$NAME" -c "sudo -u $DISTRO mkdir -p /home/$DISTRO/.ssh && sudo -u $DISTRO chmod 700 /home/$DISTRO/.ssh"
  146. vagrant ssh "$NAME" -c "sudo sh -c 'cat >> /home/$DISTRO/.ssh/authorized_keys'" < var/id_rsa.pub
  147. VBoxManage snapshot "Gitian-$NAME" take "Gitian-Clean"
  148. vagrant suspend "$NAME"
  149. exit 0
  150. fi
  151. if [ $LXC = "1" ]; then
  152. if [ -e $OUT ]; then
  153. echo $OUT already exists, please remove it first
  154. exit 1
  155. fi
  156. sudo rm -rf $OUT-bootstrap
  157. # Need universe for lxc in lucid
  158. unset preserve_env
  159. if [ "$GITIAN_SUDO_USE_DEBOOTSTRAP_DIR" = "yes" ]; then
  160. echo "sudo will preserve (some) env flags"
  161. preserve_env=yes # if you would want to set false then unset this variable
  162. fi
  163. 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
  164. # Fix lxc issue
  165. if [ -f $OUT-bootstrap/usr/lib/lxc/lxc-init ]
  166. then
  167. sudo cp $OUT-bootstrap/usr/lib/lxc/lxc-init $OUT-bootstrap/usr/sbin/init.lxc
  168. else
  169. if [ $ARCH = "amd64" ]
  170. then
  171. if [ -f $OUT-bootstrap/usr/lib/x86_64-linux-gnu/lxc/lxc-init ]
  172. then
  173. sudo cp $OUT-bootstrap/usr/lib/x86_64-linux-gnu/lxc/lxc-init $OUT-bootstrap/usr/sbin/init.lxc
  174. fi
  175. else
  176. if [ -f $OUT-bootstrap/usr/lib/i386-linux-gnu/lxc/lxc-init ]
  177. then
  178. sudo cp $OUT-bootstrap/usr/lib/i386-linux-gnu/lxc/lxc-init $OUT-bootstrap/usr/sbin/init.lxc
  179. fi
  180. fi
  181. fi
  182. dd if=/dev/zero of=$OUT-lxc bs=1M count=1 seek=10240
  183. /sbin/mkfs.ext4 -F $OUT-lxc
  184. t=`mktemp -d gitian.XXXXXXXX`
  185. sudo mount $OUT-lxc $t
  186. sudo cp -a $OUT-bootstrap/* $t
  187. sudo umount $t
  188. rmdir $t
  189. sudo rm -rf $OUT-bootstrap
  190. mv $OUT-lxc $OUT
  191. # bootstrap-fixup is done in libexec/make-clean-vm
  192. else
  193. if [ -e $OUT.qcow2 ]; then
  194. echo $OUT.qcow2 already exists, please remove it first
  195. exit 1
  196. fi
  197. libexec/config-bootstrap-fixup
  198. rm -rf $OUT
  199. 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
  200. mv $OUT/*.qcow2 $OUT.qcow2
  201. rm -rf $OUT
  202. # bootstrap-fixup is done on first boot
  203. fi