#386 add support to usb mass storage devices on qemu inventory
Merged 3 years ago by astepano. Opened 3 years ago by bgoncalv.
bgoncalv/standard-test-roles fix-standard-inventory-qcow2  into  master

file modified
+16 -5
@@ -58,10 +58,14 @@ 

      m: 3G

      net_nic:

        model: e1000

-   drive:

-   - size: 11000000000

-     path: /home/vader

-   - size: 2000000000

+     drive:

+     - size: 11000000000

+       path: /home/vader

+     - size: 2000000000

+     usb_drive:

+     - path: /tmp

+     - size: 2000000000

+       path: /tmp

  

  standard-inventory-docker:

    dumb_option: dumb_parameter
@@ -77,8 +81,15 @@ 

  * `drive` - has to contain dict of additional drive(s)

  * `drive.size` - allows to specify additional drive with size in bytes

                   (default size: 2G)

- * `drive.path` - allows to specify additional drive with custom path to its backing file

+ * `drive.path` - allows to specify additional drive with custom directory path to its backing file

+                  Filename for image will be auto-generated.

                   (default path: /tmp)

+ * `usb_drive` - has to contain dict of additional usb drive(s)

+ * `usb_drive.size` - allows to specify additional usb drive with size in bytes

+                      (default size: 1G)

+ * `usb_drive.path` - allows to specify additional usb drive with custom directory path to its backing file

+                      Filename for image will be auto-generated.

+                      (default path: /tmp)

  

  You can open a RFE ticket to extend supported parameters according to

  https://qemu.weilnetz.de/doc/qemu-doc.html

@@ -16,7 +16,6 @@ 

  import shlex

  import signal

  import socket

- import atexit

  import shutil

  import random

  import logging
@@ -98,20 +97,39 @@ 

          list of str

                  qemu -drive options

          """

-         drives = fmf_get(['qemu', 'drive'], list())

          result = []

+         drives = fmf_get(['qemu', 'drive'], list())

          for drive in drives:

              # create temporary sparse file

              size = int(drive.get('size', 2 * 1024 ** 3))  # default size: 2G

              path = drive.get('path', None)

              path = str(path) if path is not None else None

-             drive_file = tempfile.NamedTemporaryFile(dir=path)

+             drive_file = tempfile.NamedTemporaryFile(dir=path, delete=False)

              drive_file.truncate(size)

              cls._tempfiles.append(drive_file)

              logger.info("Created temporary sparse file '%s'.", drive_file.name)

              # translate data into qemu command options

              result += ["-drive", "file=%s,media=disk,if=virtio" % drive_file.name]

-         atexit.register(cls.cleanup)

+ 

+         usb_drives = fmf_get(['qemu', 'usb_drive'], list())

+         if usb_drives:

+             # there is at least 1 usb device, enable usb hub

+             result += ["-usb"]

+         usb_id = 0

+         for usb_drive in usb_drives:

+             usb_id += 1

+             # create temporary sparse file

+             size = int(usb_drive.get('size', 1 * 1024 ** 3))  # default size: 1G

+             path = usb_drive.get('path', None)

+             path = str(path) if path is not None else None

+             usb_drive_file = tempfile.NamedTemporaryFile(dir=path, suffix='.img', prefix='usb_storage', delete=False)

+             usb_drive_file.truncate(size)

+             cls._tempfiles.append(usb_drive_file)

+             logger.info("Created temporary sparse file '%s'.", usb_drive_file.name)

+             # translate data into qemu command options

+             result += ["-drive", "id=str_usb_storage_%s,file=%s,if=none" % (usb_id, usb_drive_file.name),

+                        "-device", "usb-storage,drive=str_usb_storage_%s" % usb_id]

+ 

          return result

  

      @classmethod
@@ -569,6 +587,7 @@ 

      # Kill the qemu process

      try:

          os.kill(proc.pid, signal.SIGTERM)

+         AdditionalDrives.cleanup()

      except OSError:

          pass

      shutil.rmtree(directory)

add support to usb mass storage devices on qemu inventory

don't remove tempfile when process ends

It is possible to 1st provision the VM and leave it running to only after run the playbook like example below, so tempfile can't be removed until qemu process finishes

TEST_DEBUG=1 TEST_SUBJECTS=/test_subject.qcow2 ansible-inventory --inventory=/usr/share/ansible/inventory/standard-inventory-qcow2 --list --yaml | tee pipeline_inventory.yaml

ansible-playbook --inventory=pipeline_inventory.yaml --extra-vars ansible_python_interpreter=/usr/bin/python3 --tags classic tests.yml

if=none
Will it work with this? A few lines we use virtio .

I followed the information from https://www.linux-kvm.org/page/USB, I've tested and it seems to work for me.

On my test I was able to create a filesystem and mount it.

2 new commits added

  • add support to usb mass storage devices on qemu inventory
  • don't remove tempfile when process ends
3 years ago

Could you please add:
custom directory path.
+ Filename for image will be auto-generated.

2 new commits added

  • add support to usb mass storage devices on qemu inventory
  • don't remove tempfile when process ends
3 years ago

Sure I can squash it, but as the fix to not delete the tempfiles is unrelated to usb support I though having it separated was better.

2 new commits added

  • add support to usb mass storage devices on qemu inventory
  • don't remove tempfile when process ends
3 years ago

2 new commits added

  • add support to usb mass storage devices on qemu inventory
  • don't remove tempfile when process ends
3 years ago

Commit 6267038 fixes this pull-request

Pull-Request has been merged by astepano

3 years ago