Merge "Provide average latency results in IXIA KPIs"
[yardstick.git] / ansible / roles / build_yardstick_image / tasks / pre_build.yml
1 # Copyright (c) 2018 Intel Corporation.\r
2 #\r
3 # Licensed under the Apache License, Version 2.0 (the "License");\r
4 # you may not use this file except in compliance with the License.\r
5 # You may obtain a copy of the License at\r
6 #\r
7 #      http://www.apache.org/licenses/LICENSE-2.0\r
8 #\r
9 # Unless required by applicable law or agreed to in writing, software\r
10 # distributed under the License is distributed on an "AS IS" BASIS,\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 # See the License for the specific language governing permissions and\r
13 # limitations under the License.\r
14 ---\r
15 - name: Group\r
16   group_by:\r
17     key: image_builder\r
18 \r
19 - package: name=parted state=present\r
20   environment: "{{ proxy_env }}"\r
21 \r
22 - package: name=kpartx state=present\r
23   environment: "{{ proxy_env }}"\r
24 \r
25 - package: name="{{ growpart_package[ansible_os_family] }}" state=present\r
26   environment: "{{ proxy_env }}"\r
27 \r
28 - set_fact:\r
29     imgfile: "{{ normal_image_file }}"\r
30   when: img_prop_item == "normal"\r
31 \r
32 - set_fact:\r
33     imgfile: "{{ nsb_image_file }}"\r
34   when: img_prop_item == "nsb"\r
35 \r
36 - set_fact:\r
37     mountdir: "{{ lookup('env', 'mountdir')|default('/mnt/yardstick', true) }}"\r
38     raw_imgfile: "{{ workspace }}/{{ raw_imgfile_basename }}"\r
39 \r
40 # cleanup non-lxd\r
41 - name: unmount all old mount points\r
42   mount:\r
43     name: "{{ item }}"\r
44     state: unmounted\r
45   with_items:\r
46     # order matters\r
47     - "{{ mountdir }}/proc"\r
48     - "{{ mountdir }}"\r
49     - "/mnt/{{ release }}"\r
50 \r
51 - name: kpartx -dv to delete all image partition device nodes\r
52   command: kpartx -dv "{{ raw_imgfile }}"\r
53   ignore_errors: true\r
54 \r
55 - name: Debug dump loop devices\r
56   command: losetup -a\r
57   ignore_errors: true\r
58 \r
59 - name: delete loop devices for image file\r
60   # use this because kpartx -dv will fail if raw_imgfile was delete\r
61   # but in theory we could have deleted file still attached to loopback device?\r
62   # use grep because of // and awk\r
63   shell: losetup -O NAME,BACK-FILE | grep "{{ raw_imgfile_basename }}" | awk '{ print $1 }' | xargs -l1 losetup -v -d\r
64   ignore_errors: true\r
65 \r
66 - name: Debug dump loop devices again\r
67   command: losetup -a\r
68   ignore_errors: true\r
69 \r
70 - name: delete {{ raw_imgfile }}\r
71   file:\r
72     path: "{{ raw_imgfile }}"\r
73     state: absent\r
74 \r
75 # common\r
76 - name: remove {{ mountdir }}\r
77   file:\r
78     path: "{{ mountdir }}"\r
79     state: absent\r
80 \r
81 # download-common\r
82 - name: remove {{ workspace }}\r
83   file:\r
84     path: "{{ workspace }}"\r
85     state: directory\r
86 \r
87 - name: "fetch {{ image_url }} and verify "\r
88   fetch_url_and_verify:\r
89     url: "{{ image_url }}"\r
90     sha256url: "{{ sha256sums_url }}"\r
91     dest: "{{ image_dest }}"\r
92 \r
93 - name: convert image to raw\r
94   command: "qemu-img convert {{ image_dest }} {{ raw_imgfile }}"\r
95 \r
96 - name: resize image to allow for more VNFs\r
97   command: "qemu-img resize -f raw {{ raw_imgfile }} +2G"\r
98 \r
99 - name: resize parition to allow for more VNFs\r
100   # use growpart because maybe it handles GPT better than parted\r
101   command: growpart {{ raw_imgfile }}  1\r
102 \r
103 - name: create mknod devices in chroot\r
104   command: "mknod -m 0660 /dev/loop{{ item }} b 7 {{ item }}"\r
105   args:\r
106     creates: "/dev/loop{{ item }}"\r
107   with_sequence: start=0 end=9\r
108   tags: mknod_devices\r
109 \r
110 - name: find first partition device\r
111   command: kpartx -l "{{ raw_imgfile }}"\r
112   register: kpartx_res\r
113 \r
114 - set_fact:\r
115     image_first_partition: "{{ kpartx_res.stdout_lines[0].split()[0] }}"\r
116 \r
117 - set_fact:\r
118     # assume / is the first partition\r
119     image_first_partition_device: "/dev/mapper/{{ image_first_partition }}"\r
120 \r
121 - name: use kpartx to create device nodes for the raw image loop device\r
122   # operate on the loop device to avoid /dev namespace missing devices\r
123   command: kpartx -avs "{{ raw_imgfile }}"\r
124 \r
125 - name: parted dump raw image\r
126   command: parted "{{ raw_imgfile }}" print\r
127   register: parted_res\r
128 \r
129 - debug:\r
130     var: parted_res\r
131     verbosity: 2\r
132 \r
133 - name: use blkid to find filesystem type of first partition device\r
134   command: blkid -o value -s TYPE {{ image_first_partition_device }}\r
135   register: blkid_res\r
136 \r
137 - set_fact:\r
138     image_fs_type: "{{ blkid_res.stdout.strip() }}"\r
139 \r
140 - fail:\r
141     msg: "We only support ext4 image filesystems because we have to resize"\r
142   when: image_fs_type != "ext4"\r
143 \r
144 - name: fsck the image filesystem\r
145   command: "e2fsck -y -f {{ image_first_partition_device  }}"\r
146 \r
147 - name: resize filesystem to full partition size\r
148   command: resize2fs {{ image_first_partition_device }}\r
149 \r
150 - name: fsck the image filesystem\r
151   command: "e2fsck -y -f {{ image_first_partition_device  }}"\r
152 \r
153 - name: make tmp disposable fstab\r
154   command: mktemp --tmpdir fake_fstab.XXXXXXXXXX\r
155   register: mktemp_res\r
156 \r
157 - set_fact:\r
158     fake_fstab: "{{ mktemp_res.stdout.strip() }}"\r
159 \r
160 - name: mount first parition on image device\r
161   mount:\r
162     src: "{{ image_first_partition_device }}"\r
163     name: "{{ mountdir }}"\r
164     # fstype is required\r
165     fstype: "{{ image_fs_type }}"\r
166     # !!!!!!! this is required otherwise we add entries to /etc/fstab\r
167     # and prevent the system from booting\r
168     fstab: "{{ fake_fstab }}"\r
169     state: mounted\r
170 \r
171 - name: mount chroot /proc\r
172   mount:\r
173     src: none\r
174     name: "{{ mountdir }}/proc"\r
175     fstype: proc\r
176     # !!!!!!! this is required otherwise we add entries to /etc/fstab\r
177     # and prevent the system from booting\r
178     fstab: "{{ fake_fstab }}"\r
179     state: mounted\r
180 \r
181 - name: if arm copy qemu-aarch64-static into chroot\r
182   copy:\r
183     src: /usr/bin/qemu-aarch64-static\r
184     dest: "{{ mountdir }}/usr/bin"\r
185   when: img_arch == arch_arm64\r
186 \r
187 - name: create ubuntu policy-rc.d workaround\r
188   copy:\r
189     content: "{{ '#!/bin/sh\nexit 101\n' }}"\r
190     dest: "{{ mountdir }}/usr/sbin/policy-rc.d"\r
191     mode: 0755\r
192   when: "target_os == 'Ubuntu'"\r
193 \r
194 - name: add chroot as host\r
195   add_host:\r
196     name: "{{ mountdir }}"\r
197     groups: chroot_image,image_builder\r
198     connection: chroot\r
199     ansible_python_interpreter: /usr/bin/python3\r
200     # set this host variable here\r
201     nameserver_ip: "{{ ansible_dns.nameservers[0] }}"\r
202     image_type: vm\r