#!/bin/bash get_guest_os_info () { local osimg=$1 # there is a mess with virt-inspector in different libguestfs builds if type virt-inspector2 >/dev/null 2>&1 ; then vicmd="virt-inspector2" elif type virt-inspector >/dev/null 2>&1 ; then vicmd="virt-inspector" else return 1 fi $vicmd -a $osimg > osinfo.xml [ -z "$VM_OS_NAME" ] && VM_OS_NAME=$( cat osinfo.xml | $vicmd --xpath 'string(//product_name)' ) [ -z "$VM_OS_TYPE" ] && VM_OS_TYPE=$( cat osinfo.xml | $vicmd --xpath 'string(//name)' ) [ -z "$VM_OS_ARCH" ] && VM_OS_ARCH=$( cat osinfo.xml | $vicmd --xpath 'string(//arch)' ) [ "$VM_OS_TYPE" = "windows" ] && VM_OS_WINDOWS_SYSROOT=$( cat osinfo.xml | $vicmd --xpath 'string(//windows_systemroot)' ) [ "$VM_OS_TYPE" = "windows" ] && VM_ACCESS_METHOD="RDP" || VM_ACCESS_METHOD="SSH" } change_guest_password () { local osimg=$1 local osuser=$2 local ospasswd=$3 if [ "$VM_OS_TYPE" = "windows" ]; then local regdir="winguest-config" mkdir -p $regdir reg_path="${VM_OS_WINDOWS_SYSROOT}" reg_path="${reg_path}/$( virt-ls -a "${osimg}" "${reg_path}" | grep -ie '^system32$' )" reg_path="${reg_path}/$( virt-ls -a "${osimg}" "${reg_path}" | grep -ie '^config$' )" sam_file="$( virt-ls -a "${osimg}" "${reg_path}" | grep -ie '^sam$' )" system_file="$( virt-ls -a "${osimg}" "${reg_path}" | grep -ie '^system$' )" virt-copy-out -a "${osimg}" "${reg_path}/${sam_file}" "${reg_path}/${system_file}" ${regdir} echo -e "n\n2\n${ospasswd}\ny" | chntpw -u ${osuser} "${regdir}/${sam_file}" "${regdir}/${system_file}" 1>&2 # *WALL* on successfull password change - exit code is 2! On fail exit code is 0! [ $? -ne 2 ] && return 1 virt-copy-in -a "${osimg}" "${regdir}/${sam_file}" "${regdir}/${system_file}" "${reg_path}/" elif [ "$VM_OS_TYPE" = "linux" ]; then # Possible TODO: use the same hashing algorithm/salt as in guest image newpass=$( perl -e "print crypt(\"${ospasswd}\",'\$1\$12345') . \"\n\"" | sed 's/\//\\\//g' | sed 's/\$/\\$/g' ) #virt-copy-out -a "${osinmg}" /etc/shadow guest-shadow #cat guest-shadow/shadow | grep -e "^root" | sed 's/root:\(\$.*\$.*\$\).*/\1/' #newpass=$( perl -e 'print crypt("password", "\$6\$9D9CWgMk\$") . "\n"' | sed 's/\//\\\//g' | sed 's/\$/\\$/g' ) virt-edit -a "${osimg}" /etc/shadow -e "s/^${osuser}:[^:]*:/${osuser}:${newpass}:/" fi }