-
Notifications
You must be signed in to change notification settings - Fork 97
/
qemu_img
executable file
·121 lines (108 loc) · 3.89 KB
/
qemu_img
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2013, Jeroen Hoekx <jeroen.hoekx@dsquare.be>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
DOCUMENTATION = '''
---
author: Jeroen Hoekx
module: qemu_img
short_description: Create qemu images
description:
- "This module creates images for qemu."
version_added: "1.2"
options:
dest:
description:
- The image file to create or remove
required: true
format:
description:
- The image format - default qcow2
required: false
size:
description:
- The size of the image in megabytes.
required: false
opt:
description:
- Comma separated list of format specific options in a name=value format.
required: false
state:
choices: [ "absent", "present" ]
description:
- If the image should be present or absent - default present
required: false
examples:
- description: Create a raw image of 5M.
code: qemu_img dest=/tmp/testimg size=5 format=raw
- description: Enlarge the image to 6M.
code: qemu_img dest=/tmp/testimg size=6 format=raw
- description: Remove the image
code: qemu_img dest=/tmp/testimg state=absent
notes:
- This module does not change the type of the image.
'''
import os
from ansible.module_utils.basic import AnsibleModule
def main():
module = AnsibleModule(
argument_spec = dict(
dest = dict(type='str', required=True),
opt = dict(type='str'),
format = dict(type='str', default='qcow2'),
size = dict(type='int'),
state = dict(type='str', choices=['absent', 'present'], default='present'),
),
supports_check_mode = True,
)
changed = False
qemu_img = module.get_bin_path('qemu-img', True)
dest = module.params['dest']
img_format = module.params['format']
opt = module.params['opt']
if module.params['state'] == 'present':
if not module.params['size']:
module.fail_json(msg="Parameter 'size' required")
size = module.params['size'] * 1024 * 1024
if not os.path.exists(dest):
if not module.check_mode:
if not opt:
module.run_command('%s create -f %s "%s" %s'%(qemu_img, img_format, dest, size), check_rc=True)
else:
module.run_command('%s create -f %s -o %s "%s" %s'%(qemu_img, img_format, opt, dest, size), check_rc=True)
changed = True
else:
rc, stdout, _ = module.run_command('%s info "%s"'%(qemu_img, dest), check_rc=True)
current_size = None
for line in stdout.splitlines():
if 'virtual size' in line:
### virtual size: 5.0M (5242880 bytes)
current_size = int(line.split('(')[1].split()[0])
if not current_size:
module.fail_json(msg='Unable to read virtual disk size of %s'%(dest))
if current_size != size:
if not module.check_mode:
module.run_command('%s resize "%s" %s'%(qemu_img, dest, size), check_rc=True)
changed = True
if module.params['state'] == 'absent':
if os.path.exists(dest):
if not module.check_mode:
os.remove(dest)
changed = True
module.exit_json(changed=changed)
if __name__ == '__main__':
main()