| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | |||
| 5 | package Rex::Commands::Virtualization; | ||
| 6 | |||
| 7 | 1 | ▶ | use v5.14.4; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | our $VERSION = '1.16.1'; # VERSION | |
| 11 | |||
| 12 | 1 | require Rex::Exporter; | |
| 13 | 1 | ▶ | use base qw(Rex::Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use vars qw(@EXPORT); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | |||
| 16 | 1 | ▶ | use Rex::Logger; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 1 | ▶ | use Rex::Virtualization; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | |||
| 19 | 1 | @EXPORT = qw(vm); | |
| 20 | |||
| 21 | 0 | ▶ | sub vm { |
|
Subroutine
vm: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 22 | 0 | my ( $action, $vmname, @opt ) = @_; | |
| 23 | |||
| 24 | 0 | my $vm_obj = Rex::Virtualization->create(); | |
| 25 | 0 | return $vm_obj->execute( $action, $vmname, @opt ); | |
| 26 | } | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Rex::Commands::Virtualization - Virtualization module | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | With this module you can manage your virtualization. | ||
| 35 | |||
| 36 | Version <= 1.0: All these functions will not be reported. | ||
| 37 | |||
| 38 | All these functions are not idempotent. | ||
| 39 | |||
| 40 | =head1 SYNOPSIS | ||
| 41 | |||
| 42 | use Rex::Commands::Virtualization; | ||
| 43 | |||
| 44 | set virtualization => "LibVirt"; | ||
| 45 | set virtualization => "VBox"; | ||
| 46 | |||
| 47 | use Data::Dumper; | ||
| 48 | |||
| 49 | print Dumper vm list => "all"; | ||
| 50 | print Dumper vm list => "running"; | ||
| 51 | |||
| 52 | vm destroy => "vm01"; | ||
| 53 | |||
| 54 | vm delete => "vm01"; | ||
| 55 | |||
| 56 | vm start => "vm01"; | ||
| 57 | |||
| 58 | vm shutdown => "vm01"; | ||
| 59 | |||
| 60 | vm reboot => "vm01"; | ||
| 61 | |||
| 62 | vm option => "vm01", | ||
| 63 | max_memory => 1024*1024, | ||
| 64 | memory => 512*1024; | ||
| 65 | |||
| 66 | print Dumper vm info => "vm01"; | ||
| 67 | |||
| 68 | # creating a vm on a kvm host | ||
| 69 | vm create => "vm01", | ||
| 70 | storage => [ | ||
| 71 | { | ||
| 72 | file => "/mnt/data/libvirt/images/vm01.img", | ||
| 73 | dev => "vda", | ||
| 74 | } | ||
| 75 | ]; | ||
| 76 | |||
| 77 | print Dumper vm hypervisor => "capabilities"; | ||
| 78 | |||
| 79 | =head1 EXPORTED FUNCTIONS | ||
| 80 | |||
| 81 | =head2 vm($action => $name, %option) | ||
| 82 | |||
| 83 | This module only exports the I<vm> function. You can manage everything with this function. | ||
| 84 | |||
| 85 | =head1 EXAMPLES | ||
| 86 | |||
| 87 | =head2 Creating a Virtual Machine | ||
| 88 | |||
| 89 | Create a (VirtualBox) VM named "vm01" with 512 MB ram and 1 cpu. One harddrive, 10 GB in size being a file on disk. | ||
| 90 | With a cdrom as an iso image and a natted network. The bootorder is set to "dvd". | ||
| 91 | |||
| 92 | vm create => "vm01", | ||
| 93 | storage => [ | ||
| 94 | { | ||
| 95 | file => "/mnt/data/vbox/vm01.img", | ||
| 96 | size => "10G", | ||
| 97 | }, | ||
| 98 | { | ||
| 99 | file => "/mnt/iso/debian6.iso", | ||
| 100 | } | ||
| 101 | ], | ||
| 102 | memory => 512, | ||
| 103 | type => "Linux26", | ||
| 104 | cpus => 1, | ||
| 105 | boot => "dvd"; | ||
| 106 | |||
| 107 | |||
| 108 | Create a (KVM) VM named "vm01" with 512 MB ram and 1 cpu. One harddrive, 10 GB in size being a file on disk. | ||
| 109 | With a cdrom as an iso image and a bridged network on the bridge virbr0. The Bootorder is set to "cdrom". | ||
| 110 | |||
| 111 | vm create => "vm01", | ||
| 112 | boot => "cdrom", | ||
| 113 | storage => [ | ||
| 114 | { | ||
| 115 | size => "10G", | ||
| 116 | file => "/mnt/data/libvirt/images/vm01.img", | ||
| 117 | }, | ||
| 118 | |||
| 119 | { | ||
| 120 | file => "/mnt/data/iso/debian-6.0.2.1-amd64-netinst.iso", | ||
| 121 | }, | ||
| 122 | ]; | ||
| 123 | |||
| 124 | This is the same as above, but with all options in use. | ||
| 125 | |||
| 126 | vm create => "vm01", | ||
| 127 | memory => 512*1024, | ||
| 128 | cpus => 1, | ||
| 129 | arch => "x86_64", | ||
| 130 | boot => "cdrom", | ||
| 131 | clock => "utc", | ||
| 132 | emulator => "/usr/bin/qemu-system-x86_64", | ||
| 133 | on_poweroff => "destroy", | ||
| 134 | on_reboot => "restart", | ||
| 135 | on_crash => "restart", | ||
| 136 | storage => [ | ||
| 137 | { type => "file", | ||
| 138 | size => "10G", | ||
| 139 | device => "disk", | ||
| 140 | driver_type => "qcow2", # supports all formats qemu-img supports. | ||
| 141 | file => "/mnt/data/libvirt/images/vm01.img", | ||
| 142 | dev => "vda", | ||
| 143 | bus => "virtio", | ||
| 144 | address => { | ||
| 145 | type => "pci", | ||
| 146 | domain => "0x0000", | ||
| 147 | bus => "0x00", | ||
| 148 | slot => "0x05", | ||
| 149 | function => "0x0", | ||
| 150 | }, | ||
| 151 | }, | ||
| 152 | { type => "file", | ||
| 153 | device => "cdrom", | ||
| 154 | file => "/mnt/data/iso/debian-6.0.2.1-amd64-netinst.iso", | ||
| 155 | dev => "hdc", | ||
| 156 | bus => "ide", | ||
| 157 | readonly => 1, | ||
| 158 | address => { | ||
| 159 | type => "drive", | ||
| 160 | controller => 0, | ||
| 161 | bus => 1, | ||
| 162 | unit => 0, | ||
| 163 | }, | ||
| 164 | }, | ||
| 165 | ], | ||
| 166 | network => [ | ||
| 167 | { type => "bridge", | ||
| 168 | bridge => "virbr0", | ||
| 169 | model => "virtio", | ||
| 170 | address => { | ||
| 171 | type => "pci", | ||
| 172 | domain => "0x0000", | ||
| 173 | bus => "0x00", | ||
| 174 | slot => "0x03", | ||
| 175 | function => "0x0", | ||
| 176 | }, | ||
| 177 | }, | ||
| 178 | ], | ||
| 179 | serial_devices => [ | ||
| 180 | { | ||
| 181 | type => 'tcp', | ||
| 182 | host => '127.0.0.1', | ||
| 183 | port => 12345, | ||
| 184 | }, | ||
| 185 | ]; | ||
| 186 | |||
| 187 | Create a (Xen/HVM) VM named "vm01" with 512 MB ram and 1 cpu. One harddrive, cloned from an existing one. | ||
| 188 | |||
| 189 | vm create => "vm01", | ||
| 190 | type => "hvm", | ||
| 191 | storage => [ | ||
| 192 | { | ||
| 193 | file => "/mnt/data/libvirt/images/vm01.img", | ||
| 194 | template => "/mnt/data/libvirt/images/svn01.img", | ||
| 195 | }, | ||
| 196 | ]; | ||
| 197 | |||
| 198 | This is the same as above, but with all options in use. | ||
| 199 | |||
| 200 | vm create => "vm01", | ||
| 201 | memory => 512*1024, | ||
| 202 | cpus => 1, | ||
| 203 | boot => "hd", | ||
| 204 | clock => "utc", | ||
| 205 | on_poweroff => "destroy", | ||
| 206 | on_reboot => "restart", | ||
| 207 | on_crash => "restart", | ||
| 208 | storage => [ | ||
| 209 | { type => "file", | ||
| 210 | size => "10G", | ||
| 211 | device => "disk", | ||
| 212 | file => "/mnt/data/libvirt/images/vm01.img", | ||
| 213 | dev => "hda", | ||
| 214 | bus => "ide", | ||
| 215 | template => "/mnt/data/libvirt/images/svn01.img", | ||
| 216 | }, | ||
| 217 | { type => "file", | ||
| 218 | device => "cdrom", | ||
| 219 | dev => "hdc", | ||
| 220 | bus => "ide", | ||
| 221 | readonly => 1, | ||
| 222 | }, | ||
| 223 | ], | ||
| 224 | network => [ | ||
| 225 | { type => "bridge", | ||
| 226 | bridge => "virbr0", | ||
| 227 | }, | ||
| 228 | ], | ||
| 229 | type => "hvm"; | ||
| 230 | |||
| 231 | Create a (Xen/PVM) VM named "vm01" with 512 MB ram and 1 cpu. With one root partition (10GB in size) and one swap parition (1GB in size). | ||
| 232 | |||
| 233 | vm create => "vm01", | ||
| 234 | type => "pvm", | ||
| 235 | storage => [ | ||
| 236 | { | ||
| 237 | file => "/mnt/data/libvirt/images/domains/vm01/disk.img", | ||
| 238 | dev => "xvda2", | ||
| 239 | is_root => 1, | ||
| 240 | }, | ||
| 241 | { | ||
| 242 | file => "/mnt/data/libvirt/images/domains/vm01/swap.img", | ||
| 243 | dev => "xvda1", | ||
| 244 | }, | ||
| 245 | ]; | ||
| 246 | |||
| 247 | This is the same as above, but with all options in use. | ||
| 248 | |||
| 249 | vm create => "vm01", | ||
| 250 | type => "pvm", | ||
| 251 | memory => 512*1024, | ||
| 252 | cpus => 1, | ||
| 253 | clock => "utc", | ||
| 254 | on_poweroff => "destroy", | ||
| 255 | on_reboot => "restart", | ||
| 256 | on_crash => "restart", | ||
| 257 | os => { | ||
| 258 | type => "linux", | ||
| 259 | kernel => "/boot/vmlinuz-2.6.32-5-xen-amd64", | ||
| 260 | initrd => "/boot/initrd.img-2.6.32-5-xen-amd64", | ||
| 261 | cmdline => "root=/dev/xvda2 ro", | ||
| 262 | }, | ||
| 263 | storage => [ | ||
| 264 | { type => "file", | ||
| 265 | size => "10G", | ||
| 266 | device => "disk", | ||
| 267 | file => "/mnt/data/libvirt/images/domains/vm01/disk.img", | ||
| 268 | dev => "xvda2", | ||
| 269 | bus => "xen", | ||
| 270 | aio => 1, # if you want to use aio | ||
| 271 | }, | ||
| 272 | { type => "file", | ||
| 273 | size => "4G", | ||
| 274 | device => "disk", | ||
| 275 | file => "/mnt/data/libvirt/images/domains/vm01/swap.img", | ||
| 276 | dev => "xvda1", | ||
| 277 | bus => "xen", | ||
| 278 | aio => 1, # if you want to use aio | ||
| 279 | }, | ||
| 280 | ], | ||
| 281 | network => [ | ||
| 282 | { type => "bridge", | ||
| 283 | bridge => "virbr0", | ||
| 284 | }, | ||
| 285 | ]; | ||
| 286 | |||
| 287 | =head2 Start/Stop/Destroy | ||
| 288 | |||
| 289 | Start a stopped vm | ||
| 290 | |||
| 291 | vm start => "name"; | ||
| 292 | |||
| 293 | Stop a running vm (send shutdown signal) | ||
| 294 | |||
| 295 | vm shutdown => "name"; | ||
| 296 | |||
| 297 | Hard Stop a running vm | ||
| 298 | |||
| 299 | vm destroy => "name"; | ||
| 300 | |||
| 301 | |||
| 302 | =head2 Delete | ||
| 303 | |||
| 304 | vm delete => "name"; | ||
| 305 | |||
| 306 | =head2 Modifying a VM | ||
| 307 | |||
| 308 | Currently you can only modify the memory. | ||
| 309 | |||
| 310 | vm option => "name", | ||
| 311 | max_memory => 1024*1024, # in bytes | ||
| 312 | memory => 512*1024; | ||
| 313 | |||
| 314 | =head2 Request information of a vm | ||
| 315 | |||
| 316 | vm info => "name"; | ||
| 317 | |||
| 318 | =head2 Request info from the underlying hypervisor | ||
| 319 | |||
| 320 | vm hypervisor => "capabilities"; | ||
| 321 | |||
| 322 | =cut | ||
| 323 | |||
| 324 | 1 | 1; | |