|
| 1 | +# linux_run_iso_in_qemu |
| 2 | + |
| 3 | +[Ansible Galaxy](https://galaxy.ansible.com/click0/linux_run_iso_in_qemu/) |
| 4 | + |
| 5 | +Linux. Running QEMU with (or without) ISO and connecting block devices (HDD/SSD) of the host machine. |
| 6 | +Feel free to [share your feedback and report issues](https://github.com/click0/ansible-linux-run-iso-in-qemu/issues). |
| 7 | +[Contributions are welcome](https://github.com/firstcontributions/first-contributions). |
| 8 | + |
| 9 | +## Synopsis |
| 10 | + |
| 11 | +Many Datacenters and Hosters have removed the OS installation from their media (ISO). |
| 12 | +And, most generally offer very little choice - Debian, Ubuntu, CentOS and nothing else. |
| 13 | +This role will allow you to run the QEMU program with the Rescue server mode. |
| 14 | +[QEMU](https://www.qemu.org/) allows us to emulate a virtual machine, to which we will connect the ISO as a CD-ROM and connecting block devices (HDD/SSD) of the host machine. |
| 15 | +The role uses QEMU of two types - from the package base of the system and universal binary for Linux with statically compiled libraries. |
| 16 | +The ISO can be used as your favorite OS install disk, diagnostic disk, or other live operating system ([Live-CD](https://en.wikipedia.org/wiki/Live_CD)) |
| 17 | +Sources for obtaining ISO and QEMU universal binary - local system running Ansible, Rescue server mode itself and ftp/http(s). |
| 18 | + |
| 19 | +There is no Internet inside the system running inside QEMU yet, but we will fix this in the next release. |
| 20 | +To increase security for access to the system in QEMU, you can specify a whitelist of IP/networks through the `iptables` firewall. |
| 21 | + |
| 22 | +## Requirements |
| 23 | + |
| 24 | +Linux system Debian or CentOS. |
| 25 | +Installed packages: |
| 26 | + - python3 |
| 27 | + - python3-apt |
| 28 | + |
| 29 | +## Variables |
| 30 | + |
| 31 | +See the `defaults/main.yml` and examples in vars: |
| 32 | + |
| 33 | + lisoq_qemu_enable: false |
| 34 | +Do I need to use and run QEMU. Without this option, the role can download the ISO and configure the firewall. |
| 35 | + |
| 36 | + lisoq_qemu_static_custom_enable: false |
| 37 | +The option is responsible for using (or not) universal binary for Linux with statically compiled libraries. |
| 38 | +If this variable is selected, then the other variable `lisoq_qemu_install` will be disabled by the role itself. |
| 39 | + |
| 40 | + lisoq_qemu_static_custom_local: '' |
| 41 | +The local path on the Ansible host to the statically compiled QEMU archive(tar.gz). |
| 42 | + |
| 43 | + lisoq_qemu_static_custom_url: 'https://support.org.ua/Soft/vKVM/orig/vkvm.tar.gz' |
| 44 | +URL location with the statically compiled QEMU archive(tar.gz). |
| 45 | + |
| 46 | + lisoq_qemu_static_custom_relative_dir: '/share/qemu/' |
| 47 | +Relative path inside the statically compiled QEMU archive to auxiliary files (BIOS, keyboard layout etc). |
| 48 | + |
| 49 | + lisoq_qemu_static_custom_uefi_url: 'https://support.org.ua/Soft/vKVM/orig/uefi.tar.gz' |
| 50 | +Auxiliary UEFI BIOS archive URL to support block devices larger than 2 TiB. |
| 51 | + |
| 52 | + lisoq_qemu_args_port_ssh: '1022' |
| 53 | +External port for ssh forwarding to QEMU internal port `22`. |
| 54 | + |
| 55 | + lisoq_qemu_args_port_rdp: '3389' |
| 56 | +External port for RDP forwarding to internal QEMU port `3389` |
| 57 | + |
| 58 | + lisoq_qemu_args_port_vnc: '5901' |
| 59 | +External port for forwarding VNC to internal QEMU port `5901` |
| 60 | + |
| 61 | + lisoq_qemu_vnc_type: 'local' |
| 62 | +The variable controls how QEMU will "listen" for VNC connections. |
| 63 | +The value of the `local` variable is to listen only on `localhost`. |
| 64 | +The value of the `share` variable is to listen on all IPs. |
| 65 | + |
| 66 | + lisoq_qemu_install: false |
| 67 | +Install QEMU from the package repository. |
| 68 | + |
| 69 | + lisoq_qemu_ram: '1024' |
| 70 | +How much RAM (in MiB) can you use inside QEMU. |
| 71 | + |
| 72 | + lisoq_qemu_cpu: '' |
| 73 | +How much CPU core can you use inside QEMU. By default `''` and role allocates all CPU cores for QEMU use. |
| 74 | + |
| 75 | + lisoq_qemu_disk: '' |
| 76 | +List of block disk devices to connect to QEMU. By default, the role mounts all found block devices from the host machine. |
| 77 | +You can specify your own list of block devices: |
| 78 | + |
| 79 | + lisoq_qemu_disk: |
| 80 | + - 'sda' |
| 81 | + - 'sdb' |
| 82 | +<br> |
| 83 | + |
| 84 | + lisoq_qemu_exclude_disk: |
| 85 | + - 'fd0' |
| 86 | + - 'sr0' |
| 87 | + |
| 88 | +List of block disk devices to be excluded from the `lisoq_qemu_disk` list. The exclusion list usually contains FDD and CD-ROM devices. |
| 89 | + |
| 90 | + lisoq_qemu_boot_cd: true |
| 91 | +Whether to boot QEMU from CD-ROM (from our downloaded ISO file `lisoq_iso_file_...` ). |
| 92 | + |
| 93 | + lisoq_qemu_boot_once_cd: true |
| 94 | +Whether to download _**once**_ from CD-ROM (from our downloaded ISO file `lisoq_iso_file_...` ). |
| 95 | +Inside QEMU, you can choose to `reboot` the virtual machine and then the system will try to boot from the HDDs, _not_ from the CD-ROM. |
| 96 | +To use service CDs where there is a large set of applications, and the need to reboot the virtual machine frequently, set the value to `false`. |
| 97 | + |
| 98 | + lisoq_iso_file_local: '' |
| 99 | +Full path to the ISO file on the host machine from which the Ansible role is run. There is support for symlinks and share partitions mounted on the host machine file system. |
| 100 | + |
| 101 | + lisoq_iso_file_remote: '' |
| 102 | +ISO file location path on a remote host. |
| 103 | + |
| 104 | + lisoq_iso_file_url: 'https://mfsbsd.vx.sk/files/iso/12/amd64/mfsbsd-12.2-RELEASE-amd64.iso' |
| 105 | +URL location with ISO file. |
| 106 | + |
| 107 | + lisoq_iso_file_ssh_port: '22' |
| 108 | +Sshd port that accepts connections _inside_ ISO. |
| 109 | + |
| 110 | + lisoq_ramdisk_enable: false |
| 111 | +Use (and create) RAM-disk partitions on the target system (before running QEMU). |
| 112 | + |
| 113 | + lisoq_ramdisk_location: '/mnt' |
| 114 | +The preferred path for the RAM-disk partition. |
| 115 | + |
| 116 | + lisoq_ramdisk_another_location: '/tmp' |
| 117 | +The alternative path for a RAM-disk partition if it is already in use internally. Subsequently, we will expand it to the desired size. |
| 118 | + |
| 119 | + lisoq_ramdisk_existed: false |
| 120 | +Detect flag if RAM-disk partition is already in use. Service (local) variable. |
| 121 | + |
| 122 | + lisoq_ramdisk_mounted: false |
| 123 | +Detect flag if RAM-disk partition is already in mounted. Service (local) variable. |
| 124 | + |
| 125 | + lisoq_ramdisk_size: '300' |
| 126 | +The size of the RAM-disk partition in MiB (mebibytes). |
| 127 | + |
| 128 | + lisoq_total_need_ram: "( {{ lisoq_ramdisk_size | int + lisoq_qemu_ram | int }} | default('300') )" |
| 129 | +The minimum amount of RAM on the target system in MiB (mebibytes). The sum of two components - `lisoq_ramdisk_size` and `lisoq_qemu_ram`. |
| 130 | + |
| 131 | + lisoq_firewall_acl_enable: false |
| 132 | +Allow ACLs to whitelist IP's/net's and some listening ports (for example, `{{ lisoq_qemu_args_port_ssh }}` and `{{ lisoq_qemu_args_port_vnc }}`). Connections from other IPs to these ports are dropped. Whitelists are separate for IPv4 and IPv6 networks. |
| 133 | + |
| 134 | + lisoq_firewall_acl_ipv4_white: |
| 135 | + - '127.0.0.0/8' |
| 136 | +Default white list for IPv4 networks. |
| 137 | + |
| 138 | + lisoq_firewall_acl_ipv6_white: |
| 139 | + - '::1/128' |
| 140 | +Default white list for IPv6 networks. |
| 141 | + |
| 142 | + lisoq_firewall_acl_ports: |
| 143 | + - '{{ lisoq_qemu_args_port_ssh | default(omit) }}' |
| 144 | + - '{{ lisoq_qemu_args_port_rdp | default(omit) }}' |
| 145 | + - '{{ lisoq_qemu_args_port_vnc | default(omit) }}' |
| 146 | +Default port ACL for a firewall. |
| 147 | + |
| 148 | + lisoq_qemu_args: ' |
| 149 | + -net nic |
| 150 | + -rtc base=localtime |
| 151 | + -M pc |
| 152 | + -vga std |
| 153 | + -daemonize |
| 154 | + ' |
| 155 | +List of required command line arguments to run QEMU. |
| 156 | + |
| 157 | +## Workflow |
| 158 | + |
| 159 | +1) Install the role |
| 160 | + |
| 161 | +``` |
| 162 | +shell> ansible-galaxy role install click0.linux_run_iso_in_qemu |
| 163 | +``` |
| 164 | + |
| 165 | +2) Look variables, e.g. in `defaults/main.yml` |
| 166 | + |
| 167 | +You can override them in the playbook and inventory. |
| 168 | + |
| 169 | +## Example Playbooks |
| 170 | + |
| 171 | +### Example #1 |
| 172 | + |
| 173 | + - hosts: rescue_servers |
| 174 | + vars_files: |
| 175 | + - vars/main.yml |
| 176 | + roles: |
| 177 | + - click0.linux_run_iso_in_qemu |
| 178 | +*Inside `vars/main.yml`*: |
| 179 | + |
| 180 | + lisoq_qemu_enable: true |
| 181 | + lisoq_qemu_static_custom_enable: true |
| 182 | + lisoq_iso_file_url: 'https://mfsbsd.vx.sk/files/iso/12/amd64/mfsbsd-12.2-RELEASE-amd64.iso' |
| 183 | + lisoq_firewall_acl_ipv4_white: |
| 184 | + - '127.0.0.0/8' |
| 185 | + - '10.0.0.0/8' |
| 186 | + - '192.168.0.0/16' |
| 187 | + lisoq_firewall_acl_ipv6_white: [] |
| 188 | + lisoq_firewall_acl_enable: true |
| 189 | + |
| 190 | +### Example #2 |
| 191 | + |
| 192 | + - hosts: rescue_servers |
| 193 | + vars_files: |
| 194 | + - vars/main.yml |
| 195 | + roles: |
| 196 | + - click0.linux_run_iso_in_qemu |
| 197 | +*Inside `vars/main.yml`*: |
| 198 | + |
| 199 | + lisoq_qemu_enable: true |
| 200 | + lisoq_qemu_install: true |
| 201 | + lisoq_qemu_ram: '1000' |
| 202 | + lisoq_qemu_cpu: '2' |
| 203 | + lisoq_iso_file_local: '../../files/ISO images/WinPE10_8_Strelec_2022.01.04.iso' |
| 204 | + lisoq_qemu_vnc_type: 'share' |
| 205 | + lisoq_ramdisk_enable: true |
| 206 | + lisoq_ramdisk_size: '4100' |
| 207 | + lisoq_firewall_acl_ipv4_white: |
| 208 | + - '127.0.0.0/8' |
| 209 | + - '10.0.0.0/8' |
| 210 | + - '192.168.0.0/16' |
| 211 | + lisoq_firewall_acl_enable: true |
| 212 | + |
| 213 | +## TODO |
| 214 | + |
| 215 | +- [ ] Test on a Linux LiveCD based: |
| 216 | + - Debian |
| 217 | + - CentOS |
| 218 | + - Rocky Linux |
| 219 | + - Alpine |
| 220 | + - ArchLinux |
| 221 | + - OpenWRT |
| 222 | +- [ ] Set up Internet access inside QEMU |
| 223 | + |
| 224 | +## Tested |
| 225 | + |
| 226 | +- [x] Freshly installed on HDD a Debian "bullseye" 11 |
| 227 | + |
| 228 | +## Dependencies |
| 229 | + |
| 230 | +None. |
| 231 | + |
| 232 | +## Further use |
| 233 | + |
| 234 | +### License |
| 235 | + |
| 236 | +BSD 3-Clause |
| 237 | + |
| 238 | +### Author: |
| 239 | + |
| 240 | +- Vladislav V. Prodan `<github.com/click0>` |
| 241 | + |
| 242 | +### 🤝 Contributing |
| 243 | + |
| 244 | +Contributions, issues and feature requests are welcome!<br> |
| 245 | +Feel free to check [issues page](https://github.com/click0/ansible-linux-run-iso-in-qemu/issues). |
| 246 | + |
| 247 | +### Show your support |
| 248 | + |
| 249 | +Give a ⭐ if this project helped you! |
| 250 | + |
| 251 | +<a href="https://www.buymeacoffee.com/click0" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-orange.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a> |
0 commit comments