Advertisement
Guest User

Untitled

a guest
Jun 27th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. #!/bin/bash -e
  2.  
  3. WORKDIR=`dirname $0`
  4. CONFIG_FILE=$WORKDIR/config.sh
  5. [ -f $CONFIG_FILE ] && source $CONFIG_FILE
  6.  
  7. SUPPORTED="(lucid|precise|saucy|trusty|utopic|vivid)"
  8. ARCH=amd64
  9.  
  10. SITE=${UBUNTU_SITE:-http://ftp.riken.go.jp/Linux/ubuntu}
  11. PROXY=${UBUNTU_PROXY:-${PROXY:-$http_proxy}}
  12. DISKIMG_DIR=${DISKIMG_DIR:-$HOME/images}
  13. ISO_DIR=${UBUNTU_ISO_DIR:-$HOME/iso/ubuntu}
  14. NTPSERVER=${NTPSERVER:-ntp.nict.jp}
  15.  
  16. USERNAME=ubuntu
  17. # Unless password is specified NAME is used for password by default
  18. #PASSWORD=
  19. NUM_CPU=1
  20. MEMORY=4096
  21. DISKSIZE=20G
  22. DISKFORMAT=qcow2
  23.  
  24. # You can use the following keyword
  25. # %ISO_DIR%
  26. # %ARCH%
  27. # %RELEASE_NAME% : precise, quantal, ....
  28. # %RELEASE_VERSION% : 12.04, 12.10, ....
  29. # %RELEASE_FULLVER% (including minor version for LTS) : 12.04.3, 10.04.4
  30. ISO_LOCATION_FORMAT_DEFAULT=%ISO_DIR%/ubuntu-%RELEASE_FULLVER%-server-%ARCH%.iso
  31. ISO_LOCATION_FORMAT=${UBUNTU_ISO_LOCATION_FORMAT:-$ISO_LOCATION_FORMAT_DEFAULT}
  32.  
  33. function usage() {
  34. cat <<EOF
  35. Usage: $0 [options] NAME RELEASE
  36. Options:
  37. -a ARCH : VM architecture: x86_64, i386 (default: $ARCH)
  38. -c NUM_CPU : VM number of CPU (default: $NUM_CPU)
  39. -m MEMORY : VM memory size [MB] (default: $MEMORY)
  40. -f DISKFORMAT : QEMU image format: qcow2, raw (default: $DISKFORMAT)
  41. -s DISKSIZE : QEMU image size, e.g., 50G (default: $DISKSIZE)
  42. -u USERNAME : Username of the default user (default: $USERNAME)
  43. -p PASSWORD : Password for the default user (default: $PASSWORD)
  44. -P : Do not use preseed.cfg
  45. Configurations:
  46. DISKIMG_DIR=$DISKIMG_DIR
  47. UBUNTU_SITE=$SITE
  48. UBUNTU_PROXY=$PROXY
  49. UBUNTU_ISO_DIR=$ISO_DIR
  50. UBUNTU_ISO_LOCATION_FORMAT=$ISO_LOCATION_FORMAT
  51. EOF
  52. exit 1
  53. }
  54.  
  55. while getopts "a:c:m:f:s:u:p:Ph" OPT; do
  56. case $OPT in
  57. a) ARCH=$OPTARG
  58. if [ "$ARCH" != "i386" -a "$ARCH" != "amd64" ]; then
  59. echo "Arch must be either amd64 or i386."
  60. exit 1
  61. fi
  62. ;;
  63. c) NUM_CPU=$OPTARG; ;;
  64. m) MEMORY=$OPTARG; ;;
  65. f) DISKFORMAT=$OPTARG
  66. if [ "$DISKFORMAT" != "qcow2" -a "$DISKFORMAT" != "raw" ]; then
  67. echo "Disk format must be either qcow2 or raw."
  68. exit 1
  69. fi
  70. ;;
  71. s) DISKSIZE=$OPTARG; ;;
  72. u) USERNAME=$OPTARG; ;;
  73. p) PASSWORD=$OPTARG; ;;
  74. P) NO_PRESEED=true; ;;
  75. ?) usage
  76. ;;
  77. esac
  78. done
  79. shift `expr $OPTIND - 1`
  80.  
  81. if [ -z "$1" ]; then
  82. echo "Name must be specified!"
  83. usage
  84. exit 1
  85. fi
  86.  
  87. if [ -z "$2" ]; then
  88. echo "release must be specified! $SUPPORTED"
  89. echo "Usage: $0 [options] NAME RELEASE"
  90. exit 1
  91. fi
  92.  
  93. NAME=$1
  94. DISK=$DISKIMG_DIR/$NAME.img
  95.  
  96. if [ -z "$PASSWORD" ]; then
  97. PASSWORD=$NAME
  98. fi
  99.  
  100. RELEASE_NAME=$2
  101. if [[ ! "$RELEASE_NAME" =~ $SUPPORTED ]]; then
  102. echo "Release '$RELEASE_NAME' is not supported."
  103. echo "$SUPPORTED must be specified"
  104. exit 2
  105. fi
  106.  
  107. if [ "$ARCH" == "amd64" ]; then
  108. VIRT_ARCH=x86_64
  109. else
  110. VIRT_ARCH=i386
  111. fi
  112. case "$RELEASE_NAME" in
  113. lucid)
  114. RELEASE_FULLVER=10.04.4
  115. ;;
  116. precise)
  117. RELEASE_FULLVER=12.04.3
  118. ;;
  119. saucy)
  120. RELEASE_FULLVER=13.10
  121. ;;
  122. trusty)
  123. RELEASE_FULLVER=14.04.01
  124. ;;
  125. utopic)
  126. RELEASE_FULLVER=14.10
  127. ;;
  128. vivid)
  129. RELEASE_FULLVER=15.04
  130. OS_VARIANT=ubuntuutopic
  131. ;;
  132. esac
  133. if [ -z "$OS_VARIANT" ]; then
  134. OS_VARIANT=ubuntu${RELEASE_NAME}
  135. fi
  136.  
  137. LOCATION=$SITE/dists/$RELEASE_NAME/main/installer-$ARCH/
  138. if [ -n "$RELEASE_FULLVER" ]; then
  139. RELEASE_VERSION=`echo $RELEASE_FULLVER | cut -d . -f 1-2`
  140. ISO_LOCATION=`echo $ISO_LOCATION_FORMAT | sed \
  141. -e "s|%ISO_DIR%|$ISO_DIR|g" \
  142. -e "s|%ARCH%|$ARCH|g" \
  143. -e "s|%RELEASE_NAME%|$RELEASE_NAME|g" \
  144. -e "s|%RELEASE_VERSION%|$RELEASE_VERSION|g" \
  145. -e "s|%RELEASE_FULLVER%|$RELEASE_FULLVER|g" \
  146. `
  147. if [ -f $ISO_LOCATION ]; then
  148. LOCATION=$ISO_LOCATION
  149. fi
  150. fi
  151.  
  152. function generate_preseed_cfg() {
  153. PRESEED_DIR=/tmp/preseed$$
  154. PRESEED_BASENAME=preseed.cfg
  155. PRESEED_FILE=$PRESEED_DIR/$PRESEED_BASENAME
  156. mkdir -p $PRESEED_DIR
  157. cat > $PRESEED_FILE <<EOF
  158. d-i debian-installer/language string en
  159. d-i debian-installer/locale string en_US.UTF-8
  160. d-i debian-installer/country string JP
  161. d-i console-setup/ask_detect boolean false
  162. d-i console-setup/layoutcode string jp
  163. d-i netcfg/choose_interface select auto
  164. d-i netcfg/get_hostname string $NAME
  165. d-i netcfg/get_domain string localdomain
  166. d-i netcfg/wireless_wep string
  167. d-i mirror/country string JP
  168. d-i mirror/http/hostname string jp.archive.ubuntu.com
  169. d-i mirror/http/directory string /ubuntu
  170. d-i mirror/http/proxy string $PROXY
  171. d-i mirror/http/mirror select jp.archive.ubuntu.com
  172. d-i clock-setup/utc boolean true
  173. d-i time/zone string Asia/Tokyo
  174. d-i clock-setup/ntp boolean true
  175. d-i clock-setup/ntp-server string $NTPSERVER
  176. d-i partman-auto/method string lvm
  177. d-i partman-lvm/device_remove_lvm boolean true
  178. d-i partman-lvm/confirm boolean true
  179. d-i partman-lvm/confirm_nooverwrite boolean true
  180. d-i partman-auto-lvm/guided_size string max
  181. d-i partman-auto/choose_recipe select atomic
  182. d-i partman-partitioning/confirm_write_new_label boolean true
  183. d-i partman/choose_partition select Finish partitioning and write changes to disk
  184. d-i partman/confirm boolean true
  185. d-i partman/confirm_nooverwrite boolean true
  186. d-i passwd/user-fullname string $USERNAME
  187. d-i passwd/username string $USERNAME
  188. d-i passwd/user-password password $PASSWORD
  189. d-i passwd/user-password-again password $PASSWORD
  190. d-i user-setup/allow-password-weak boolean true
  191. d-i user-setup/encrypt-home boolean false
  192. #tasksel tasksel/first multiselect
  193. tasksel tasksel/first multiselect standard
  194. d-i pkgsel/include string openssh-server build-essential acpid git-core ntp
  195. d-i pkgsel/upgrade select none
  196. d-i pkgsel/update-policy select none
  197. d-i grub-installer/only_debian boolean true
  198. d-i grub-installer/with_other_os boolean true
  199. d-i finish-install/reboot_in_progress note
  200. EOF
  201. }
  202.  
  203. function cleanup_preseed_cfg() {
  204. rm -v -f $PRESEED_FILE
  205. rmdir -v $PRESEED_DIR
  206. }
  207.  
  208. function create_disk() {
  209. if [ ! -f $DISK ]; then
  210. qemu-img create -f $DISKFORMAT $DISK $DISKSIZE
  211. fi
  212. }
  213.  
  214. function virtinst_with_preseed() {
  215. sudo virt-install \
  216. --name $NAME \
  217. --os-type linux \
  218. --os-variant $OS_VARIANT \
  219. --virt-type kvm \
  220. --connect=qemu:///system \
  221. --vcpus $NUM_CPU \
  222. --ram $MEMORY \
  223. --arch $VIRT_ARCH \
  224. --serial pty \
  225. --console pty \
  226. --disk=$DISK,format=$DISKFORMAT,bus=virtio \
  227. --nographics \
  228. --location $LOCATION \
  229. --initrd-inject $PRESEED_FILE \
  230. --extra-args "
  231. console=ttyS0,115200
  232. file=/$PRESEED_BASENAME
  233. auto=true
  234. priority=critical
  235. interface=auto
  236. language=en
  237. country=JP
  238. locale=en_US.UTF-8
  239. console-setup/layoutcode=jp
  240. console-setup/ask_detect=false
  241. " \
  242. --network network=default,model=virtio
  243. }
  244.  
  245. function virtinst_without_preseed() {
  246. sudo virt-install \
  247. --name $NAME \
  248. --os-type linux \
  249. --os-variant ubuntu${RELEASE_NAME} \
  250. --virt-type kvm \
  251. --connect=qemu:///system \
  252. --vcpus $NUM_CPU \
  253. --ram $MEMORY \
  254. --arch $VIRT_ARCH \
  255. --serial pty \
  256. --console pty \
  257. --disk=$DISK,format=qcow2,bus=virtio \
  258. --nographics \
  259. --location $LOCATION \
  260. --extra-args "console=ttyS0,115200" \
  261. --network network=default,model=virtio
  262. }
  263.  
  264. create_disk
  265. if [ "$NO_PRESEED" != "true" ]; then
  266. generate_preseed_cfg
  267. virtinst_with_preseed
  268. cleanup_preseed_cfg
  269. else
  270. virtinst_without_preseed
  271. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement