Add virtual image generation to install script
[yardstick.git] / ansible / roles / build_yardstick_image / tasks / pre_build.yml
diff --git a/ansible/roles/build_yardstick_image/tasks/pre_build.yml b/ansible/roles/build_yardstick_image/tasks/pre_build.yml
new file mode 100644 (file)
index 0000000..2dae380
--- /dev/null
@@ -0,0 +1,202 @@
+# Copyright (c) 2018 Intel Corporation.\r
+#\r
+# Licensed under the Apache License, Version 2.0 (the "License");\r
+# you may not use this file except in compliance with the License.\r
+# You may obtain a copy of the License at\r
+#\r
+#      http://www.apache.org/licenses/LICENSE-2.0\r
+#\r
+# Unless required by applicable law or agreed to in writing, software\r
+# distributed under the License is distributed on an "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+# See the License for the specific language governing permissions and\r
+# limitations under the License.\r
+---\r
+- name: Group\r
+  group_by:\r
+    key: image_builder\r
+\r
+- package: name=parted state=present\r
+  environment: "{{ proxy_env }}"\r
+\r
+- package: name=kpartx state=present\r
+  environment: "{{ proxy_env }}"\r
+\r
+- package: name="{{ growpart_package[ansible_os_family] }}" state=present\r
+  environment: "{{ proxy_env }}"\r
+\r
+- set_fact:\r
+    imgfile: "{{ normal_image_file }}"\r
+  when: img_prop_item == "normal"\r
+\r
+- set_fact:\r
+    imgfile: "{{ nsb_image_file }}"\r
+  when: img_prop_item == "nsb"\r
+\r
+- set_fact:\r
+    mountdir: "{{ lookup('env', 'mountdir')|default('/mnt/yardstick', true) }}"\r
+    raw_imgfile: "{{ workspace }}/{{ raw_imgfile_basename }}"\r
+\r
+# cleanup non-lxd\r
+- name: unmount all old mount points\r
+  mount:\r
+    name: "{{ item }}"\r
+    state: unmounted\r
+  with_items:\r
+    # order matters\r
+    - "{{ mountdir }}/proc"\r
+    - "{{ mountdir }}"\r
+    - "/mnt/{{ release }}"\r
+\r
+- name: kpartx -dv to delete all image partition device nodes\r
+  command: kpartx -dv "{{ raw_imgfile }}"\r
+  ignore_errors: true\r
+\r
+- name: Debug dump loop devices\r
+  command: losetup -a\r
+  ignore_errors: true\r
+\r
+- name: delete loop devices for image file\r
+  # use this because kpartx -dv will fail if raw_imgfile was delete\r
+  # but in theory we could have deleted file still attached to loopback device?\r
+  # use grep because of // and awk\r
+  shell: losetup -O NAME,BACK-FILE | grep "{{ raw_imgfile_basename }}" | awk '{ print $1 }' | xargs -l1 losetup -v -d\r
+  ignore_errors: true\r
+\r
+- name: Debug dump loop devices again\r
+  command: losetup -a\r
+  ignore_errors: true\r
+\r
+- name: delete {{ raw_imgfile }}\r
+  file:\r
+    path: "{{ raw_imgfile }}"\r
+    state: absent\r
+\r
+# common\r
+- name: remove {{ mountdir }}\r
+  file:\r
+    path: "{{ mountdir }}"\r
+    state: absent\r
+\r
+# download-common\r
+- name: remove {{ workspace }}\r
+  file:\r
+    path: "{{ workspace }}"\r
+    state: directory\r
+\r
+- name: "fetch {{ image_url }} and verify "\r
+  fetch_url_and_verify:\r
+    url: "{{ image_url }}"\r
+    sha256url: "{{ sha256sums_url }}"\r
+    dest: "{{ image_dest }}"\r
+\r
+- name: convert image to raw\r
+  command: "qemu-img convert {{ image_dest }} {{ raw_imgfile }}"\r
+\r
+- name: resize image to allow for more VNFs\r
+  command: "qemu-img resize -f raw {{ raw_imgfile }} +2G"\r
+\r
+- name: resize parition to allow for more VNFs\r
+  # use growpart because maybe it handles GPT better than parted\r
+  command: growpart {{ raw_imgfile }}  1\r
+\r
+- name: create mknod devices in chroot\r
+  command: "mknod -m 0660 /dev/loop{{ item }} b 7 {{ item }}"\r
+  args:\r
+    creates: "/dev/loop{{ item }}"\r
+  with_sequence: start=0 end=9\r
+  tags: mknod_devices\r
+\r
+- name: find first partition device\r
+  command: kpartx -l "{{ raw_imgfile }}"\r
+  register: kpartx_res\r
+\r
+- set_fact:\r
+    image_first_partition: "{{ kpartx_res.stdout_lines[0].split()[0] }}"\r
+\r
+- set_fact:\r
+    # assume / is the first partition\r
+    image_first_partition_device: "/dev/mapper/{{ image_first_partition }}"\r
+\r
+- name: use kpartx to create device nodes for the raw image loop device\r
+  # operate on the loop device to avoid /dev namespace missing devices\r
+  command: kpartx -avs "{{ raw_imgfile }}"\r
+\r
+- name: parted dump raw image\r
+  command: parted "{{ raw_imgfile }}" print\r
+  register: parted_res\r
+\r
+- debug:\r
+    var: parted_res\r
+    verbosity: 2\r
+\r
+- name: use blkid to find filesystem type of first partition device\r
+  command: blkid -o value -s TYPE {{ image_first_partition_device }}\r
+  register: blkid_res\r
+\r
+- set_fact:\r
+    image_fs_type: "{{ blkid_res.stdout.strip() }}"\r
+\r
+- fail:\r
+    msg: "We only support ext4 image filesystems because we have to resize"\r
+  when: image_fs_type != "ext4"\r
+\r
+- name: fsck the image filesystem\r
+  command: "e2fsck -y -f {{ image_first_partition_device  }}"\r
+\r
+- name: resize filesystem to full partition size\r
+  command: resize2fs {{ image_first_partition_device }}\r
+\r
+- name: fsck the image filesystem\r
+  command: "e2fsck -y -f {{ image_first_partition_device  }}"\r
+\r
+- name: make tmp disposable fstab\r
+  command: mktemp --tmpdir fake_fstab.XXXXXXXXXX\r
+  register: mktemp_res\r
+\r
+- set_fact:\r
+    fake_fstab: "{{ mktemp_res.stdout.strip() }}"\r
+\r
+- name: mount first parition on image device\r
+  mount:\r
+    src: "{{ image_first_partition_device }}"\r
+    name: "{{ mountdir }}"\r
+    # fstype is required\r
+    fstype: "{{ image_fs_type }}"\r
+    # !!!!!!! this is required otherwise we add entries to /etc/fstab\r
+    # and prevent the system from booting\r
+    fstab: "{{ fake_fstab }}"\r
+    state: mounted\r
+\r
+- name: mount chroot /proc\r
+  mount:\r
+    src: none\r
+    name: "{{ mountdir }}/proc"\r
+    fstype: proc\r
+    # !!!!!!! this is required otherwise we add entries to /etc/fstab\r
+    # and prevent the system from booting\r
+    fstab: "{{ fake_fstab }}"\r
+    state: mounted\r
+\r
+- name: if arm copy qemu-aarch64-static into chroot\r
+  copy:\r
+    src: /usr/bin/qemu-aarch64-static\r
+    dest: "{{ mountdir }}/usr/bin"\r
+  when: img_arch == arch_arm64\r
+\r
+- name: create ubuntu policy-rc.d workaround\r
+  copy:\r
+    content: "{{ '#!/bin/sh\nexit 101\n' }}"\r
+    dest: "{{ mountdir }}/usr/sbin/policy-rc.d"\r
+    mode: 0755\r
+  when: "target_os == 'Ubuntu'"\r
+\r
+- name: add chroot as host\r
+  add_host:\r
+    name: "{{ mountdir }}"\r
+    groups: chroot_image,image_builder\r
+    connection: chroot\r
+    ansible_python_interpreter: /usr/bin/python3\r
+    # set this host variable here\r
+    nameserver_ip: "{{ ansible_dns.nameservers[0] }}"\r
+    image_type: vm\r