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