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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/bin/sh
  2. set -e
  3. DISTRO=ubuntu
  4. SUITE=lucid
  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 lucid
  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. EOF
  26. }
  27. if [ $# != 0 ] ; then
  28. while true ; do
  29. case "$1" in
  30. --help|-h)
  31. usage
  32. exit 0
  33. ;;
  34. --distro|-d)
  35. DISTRO="$2"
  36. shift 2
  37. ;;
  38. --suite|-s)
  39. SUITE="$2"
  40. shift 2
  41. ;;
  42. --arch|-a)
  43. ARCH="$2"
  44. shift 2
  45. ;;
  46. --lxc)
  47. LXC=1
  48. shift 1
  49. ;;
  50. --vbox)
  51. VBOX=1
  52. shift 1
  53. ;;
  54. --*)
  55. echo "unrecognized option $1"
  56. exit 1
  57. ;;
  58. *)
  59. break
  60. ;;
  61. esac
  62. done
  63. fi
  64. if [ $DISTRO = "debian" -a $LXC = "1" ]; then
  65. echo "There is no support for Debian guests using LXC currently. Please use KVM or another distro for now."
  66. exit 1
  67. fi
  68. if [ $DISTRO = "ubuntu" ]; then
  69. MIRROR=$MIRROR_BASE/archive.ubuntu.com/ubuntu
  70. SECURITY_MIRROR=$MIRROR_BASE/security.ubuntu.com/ubuntu
  71. elif [ $DISTRO = "debian" ]; then
  72. MIRROR=$MIRROR_BASE/ftp.debian.org/debian
  73. SECURITY_MIRROR=$MIRROR_BASE/security.debian.org/
  74. fi
  75. mkdir -p var
  76. if [ ! -e var/id_rsa ]; then
  77. ssh-keygen -t rsa -f var/id_rsa -N ""
  78. fi
  79. OUT=base-$SUITE-$ARCH
  80. FLAVOUR=virtual
  81. if [ $ARCH = "amd64" -a $SUITE = "hardy" ]; then
  82. FLAVOUR=server
  83. fi
  84. if [ $DISTRO = "debian" -a $ARCH = "amd64" ]; then
  85. FLAVOUR=amd64
  86. 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
  87. FLAVOUR=686
  88. elif [ $DISTRO = "debian" ]; then
  89. FLAVOUR=686-pae
  90. fi
  91. LOCALE_PKG=language-pack-en
  92. if [ $DISTRO = "debian" ]; then
  93. LOCALE_PKG=locales
  94. fi
  95. addpkg=pciutils,build-essential,git-core,subversion,$LOCALE_PKG,wget,lsb-release
  96. if [ $DISTRO = "ubuntu" ]; then
  97. # Need comma at end to work around an issue with apt for Debian <= Wheezy regarding empty strings
  98. #
  99. # If we left the comma down below when adding KERNEL_PKG to addpkg, the fact that KERNEL_PKG is undefined
  100. # if DISTRO is debian would result in two commas in a row (,,), which is interpreted by apt-get as the
  101. # package with the name empty string (""). This triggers a bug with apt versions < 1.0.3. So by adding the
  102. # comma to the end of KERNEL_PKG, we are including that comma if the distro is ubuntu (and therefore we do
  103. # have a kernel package that needs to be installed). If KERNEL_PKG is not set (i.e. we have Debian as the
  104. # distro), then we don't add that extra comma and therefore, we don't end up with two commas in a row.
  105. #
  106. # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=744940
  107. # http://anonscm.debian.org/cgit/apt/apt.git/commit/?h=1.0.3&id=d99854cac4065bc7b337815fb2116269d58dab73
  108. KERNEL_PKG=linux-image-generic,
  109. fi
  110. GRUB_PKG=grub
  111. if [ $DISTRO = "ubuntu" ]; then
  112. GRUB_PKG=grub-pc
  113. fi
  114. if [ $LXC = "1" ]; then
  115. addpkg=$addpkg,lxc
  116. else
  117. # Lack of comma after KERNEL_PKG is not a typo
  118. addpkg=$addpkg,${KERNEL_PKG}${GRUB_PKG},openssh-server
  119. fi
  120. # Remove cron to work around vmbuilder issue when umounting /dev on target
  121. removepkg=cron
  122. if [ $VBOX = "1" ]; then
  123. NAME="$SUITE-$ARCH"
  124. if ! vagrant status | grep "$NAME" | grep "not created" > /dev/null; then
  125. echo "Vagrant machine "$NAME" already exists, please remove it first (vagrant destroy "$NAME")"
  126. exit 1
  127. fi
  128. vagrant up "$NAME"
  129. vagrant ssh "$NAME" -c "sudo mkdir -p /root/.ssh && sudo chmod 700 /root/.ssh"
  130. vagrant ssh "$NAME" -c "sudo sh -c 'cat >> /root/.ssh/authorized_keys'" < var/id_rsa.pub
  131. vagrant ssh "$NAME" -c "sudo -u $DISTRO mkdir -p /home/$DISTRO/.ssh && sudo -u $DISTRO chmod 700 /home/$DISTRO/.ssh"
  132. vagrant ssh "$NAME" -c "sudo sh -c 'cat >> /home/$DISTRO/.ssh/authorized_keys'" < var/id_rsa.pub
  133. VBoxManage snapshot "Gitian-$NAME" take "Gitian-Clean"
  134. vagrant suspend "$NAME"
  135. exit 0
  136. fi
  137. if [ $LXC = "1" ]; then
  138. if [ -e $OUT ]; then
  139. echo $OUT already exists, please remove it first
  140. exit 1
  141. fi
  142. sudo rm -rf $OUT-bootstrap
  143. # Need universe for lxc in lucid
  144. env -i LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 sudo debootstrap --arch=$ARCH --include=$addpkg --exclude=$removepkg --components=main,universe $SUITE $OUT-bootstrap $MIRROR
  145. dd if=/dev/zero of=$OUT-lxc bs=1M count=1 seek=10240
  146. /sbin/mkfs.ext4 -F $OUT-lxc
  147. t=`mktemp -d gitian.XXXXXXXX`
  148. sudo mount $OUT-lxc $t
  149. sudo cp -a $OUT-bootstrap/* $t
  150. sudo umount $t
  151. rmdir $t
  152. sudo rm -rf $OUT-bootstrap
  153. mv $OUT-lxc $OUT
  154. # bootstrap-fixup is done in libexec/make-clean-vm
  155. else
  156. if [ -e $OUT.qcow2 ]; then
  157. echo $OUT.qcow2 already exists, please remove it first
  158. exit 1
  159. fi
  160. libexec/config-bootstrap-fixup
  161. rm -rf $OUT
  162. 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
  163. mv $OUT/*.qcow2 $OUT.qcow2
  164. rm -rf $OUT
  165. # bootstrap-fixup is done on first boot
  166. fi