VM: Mk Kid Disk: Common Steps

These instructions are part of a guide. These guides may assume some specific variables are set to appropriate values, including:

Let's define a new variable that points to a disk image that the “virtual machine” uses. (This will point to the disk image for the virtual machine's “second” disk.)

export VMDskKid=${VMDskDir}/${VMLILNAM}${VMCrDsNm}.qc2

The eventual goal is to create a child image at that location. However, there might be a file there now. Move it out of the way.

Rationale behind moving the file

We're about to create a new file named ${VMDskKid}. To make sure that no desirable data is erased, make sure that there is not currently a file using that name. At this point, there probably is a file using that name, so that file will get moved out of the way. (The other option, as an alternative, would be to delete the file. That probably should be done eventually, after verifying that the newer file, which is about to get created, works as intended.)

ls -l ${VMDskDir}
ls -l ${VMDskKid}
[ -f ${VMDskKid} ] && sudo mv -i ${VMDskKid} ${VMDskDir}/${VMLILNAM}${VMCrDsNm}-parent-old-uncompressed.qc2
ls -l ${VMDskDir}

We'll use a variable named after the purpose of keeping track of the parent. (Even if the desired information is already in another existing variable, using a variable with a sensible name is good. Even if this requires the creation of a new variable, having clear names for the variables is worthwhile. Also, these instructions may be re-used at some points where other variables might have different values at the start.) This variable will be named VMDskMom, and will be used to point to the file that is going to be the “parent” disk image.

Setting VMDskMom

Depending on what prior activities have been performed, and which disk is currently being childed, the desired value of this variable might already exist in another variable. If so, you may be able to see that value from one of these commands:

echo ${VMDskEnd}
echo ${VMDskDir}/${VMDskSml}

However, in some cases, these exact same steps may be useful at a time when that variable does not have the desired value. So, these instructions provide details on how to set the variable to the desired value.

echo ${VMDskMom}

If that variable currently has an desired value, then leave that desired value alone. However, if that variable currently has an undesired value, then it may be necessary to use “ unset ” on the “VMDskMom” variable. (The upcoming command is designed to allow pre-customization, and so it doesn't override any already-set value.)

(Hopefully this will work verbatim, without any need for customization).

[ "X${VMDskMom}" = "X" ] && export VMDskMom=${VMDskDir}/${VMLILNAM}${VMCrDsNm}-parent-compressed.qc2
  • (If some prior instructions had this variable be set to a custom value, that was likely intentional. Don't worry about the prior line using the pre-existing value instead of a newly updated value; that is also intentional.)

Now that we have that variable set, we can view it:

echo ${VMDskMom}

We can also see whether that file path was the same as the value in another specific variable.

if [ "X${VMDskMom}" = "X${VMDskEnd}" ] ; then echo is same ; else echo is different ; fi if [ "X${VMDskMom}" = "X${VMDskDir}/${VMDskSml}" ] ; then echo is same ; else echo is different ; fi

Let's review that:

echo VMDskKid=${VMDskKid} VMDskMom=${VMDskMom} VMDskDir=${VMDskDir}

Here are some more checks to perform:

  • ls -l ${VMDskDir}

    Although this might sound tedious, check that each of those values makes sense. A fair amount is at stake, because a mistake could result in losing a hard drive image. (If the hard drive image is notably important, and you're unsure what the values of these variables should be, consider making a backup copy of the entire ${VMDskDir} directory.)

  • This file absolutely must exist for the remainder of the process to work well:

    ls -l ${VMDskMom}

    (The permissions for the file can be read-only.)

  • This file should NOT exist, because a new file is about to be written to this filename:

    ls -l ${VMDskKid}

The parent will need to stop being updated. May as well set permissions in a way that starts protecting the parent, now.

sudo chmod a-w ${VMDskMom}

Now, the childing gets performed:

( sudo qemu-img create -f qcow2 -o backing_file=${VMDskMom} ${VMDskKid} ; echo Result=${?} ) 2>&1 | sudo -n tee -a ${VMDskKid}-creation-log.txt
echo ${?}
cat ${VMDskKid}-creation-log.txt
ls -Fltr ${VMDskDir}

(The actual creation of the “child image” file is a quick process. Although compressing an image may take time, this act of child creation should feel approximately instantaneous.)

You can see that the new image requires the old image.

qemu-img info ${VMDskKid} | grep ^"backing file: "

This guide is intentionally leaving some of the environment variables set. (Their values may be useful at a later point of the process.) So, the lack of instructions here, to “clean up” the variable space (by removing values), is actually quite intentional.