ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    QEMU Convert Script

    IT Discussion
    kvm linux bash script qemu qcow2 preallocation
    1
    1
    2.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • stacksofplatesS
      stacksofplates
      last edited by stacksofplates

      QEMU doesn't preallocate QCOW2 images by default. If you want to preallocate you have to either convert an existing disk or preallocate at creation. I'm lazy and get tired of typing

      qemu-img convert -f qcow2 -O qcow2 -o preallocation=metadata disk1.qcow2 disk2.qcow2
      

      So I wrote a script where I can just type

      convert disk1.qcow2 disk2.qcow2
      

      It does some basic error handling, like if the disk you are converting doesn't exist it lets you know, and if you are trying to convert to a file that already exists it lets you know.

      I also gave it an interactive mode with -i so it will ask you what the disks are (just for fun).

      #!/bin/bash
      
      #Script to convert qemu-images with preallocation
      
      #Script functions
      function script_help () {
        echo "
            Usage: $(basename $0) [options] original-file new-file
      
                -i   Ineractive mode
      
                -h   this help text
      
                original-file  File to convert
      
                new-file       File to create
      
            Example:
              $(basename $0) file.raw new-file.qcow2"
      
        exit ${1:-0}
      }
      
      function interactive_convert_file () {
      
        echo "Original file"
        read originalFile
      
        if [[ ! -f $originalFile ]]; then
          echo "File $originalFile not found"
          exit 1
        fi
      
        echo "File to convert to"
        read newFile
      
        if [[ -e $newFile ]]; then
          echo "File already exists!"
          exit 1
        fi
      
      
        qemu-img convert -f qcow2 -O qcow2 -o preallocation=metadata $originalFile $newFile
      
        exit ${1:-0}
      
      }
      
      function argument_convert_file () {
      
        if [[ ! -f $origFile ]]; then
          echo "File $origFile not found"
          exit 1
        fi
      
        if [[ -e $newFile ]]; then
          echo "File $newFile already exists!"
          exit 1
        fi
      
        qemu-img convert -f qcow2 -O qcow2 -o preallocation=metadata $origFile $newFile
      
        exit ${1:-0}
      
      }
      
      #Show help if no arguments or options are passed
      [[ ! "$*" ]] && script_help 1
      OPTIND=1
      
      
      #Read command line options
      while getopts "ih" opt; do
          case "$opt" in
            i) interactive_convert_file ;;
            h) script_help ;;
            \?) script_help 1 ;;
          esac
      done
      shift $(($OPTIND-1));
      
      #Run argument function
      origFile=$1
      newFile=$2
      argument_convert_file
      
      1 Reply Last reply Reply Quote 4
      • 1 / 1
      • First post
        Last post