qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] Re: Mounting qemu virtual disk images from host linux os


From: Heike C. Zimmerer
Subject: [Qemu-devel] Re: Mounting qemu virtual disk images from host linux os
Date: Tue, 18 Jan 2005 00:13:56 +0100
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

Tom Sandholm <address@hidden> writes:

[ mounting disk partitions ]

You might want to try the following script I'm using for mounting
entire disk images.  I wouldn't say it's really finished, but it works
for me so far so I hope it might be of some use.

==========================================================================
~/bin/mount-hd-image
==========================================================================
#!/bin/sh
#
# Time-stamp: <2005-01-10 10:50:16 hcz>

pname=${0##*/}
usage() {
    iconv -f utf-8 <<EOF
Syntax: $pname [-t type] raw_image mountpoint
Function:  mount all partitions of a complete hd image
           optionally create a new one
opts:
        -c        create image if it doesn't exist
        -f        run fdisk on the image
        -m        make a filesystem on it
        -v        debug level (may be given multiple times)
        -x        Script debug (det -x)
EOF
}


if [ -z "$nopt" ]; then
    black=$(tput oc)$(tput sgr0) # Reset color + attribs
    bold=$(tput bold)
    red=$(tput setaf 1)
    green=$(tput setaf 2)
    blue=$(tput setaf 4)
    cyan=$(tput setaf 6)
fi 2>/dev/null

d_echo(){
    # $1: minimum debug level req'd for display
    # $2 ..: message
    if [ "$vopt" -ge "$1" ]; then
        shift
        echo "$@"
    fi
}

muss(){
    res=0
    d_echo 1 -e "$cyan$*$black"
    "$@" || abort "$*"
}

kann(){
    d_echo 1 -e "$cyan$*$black"
    "$@"
}

kann0(){
    d_echo 1 -e "$cyan$*$black"
    "$@" >/dev/null 2>&1
}

abort(){
    [ $# -gt 0 ] && echo -n "$pname: "
    for i in "$@"; do echo >&2 "$i"; done
    echo "$pname: aborted."
    exit 2
}

###################################################

vopt=0
dopt=/home/hcz/qemu
while getopts xvh-:fmc: argv; do
    case $argv in
        c) eval ${argv}opt="\$OPTARG";;
        f|m) eval ${argv}opt=\$argv;;
        v) eval ': $(('$argv'opt++))';;
        x) set -x;;
        *) usage; exit 0;;
    esac
done
shift $((OPTIND-1))

if [ $# -lt 2 ]; then
    abort "Not enough arguments"
    exit 1
fi

src="$1"
dest="$2"
shift; shift
mkdir -p "${dest##*/}" || abort "Can't create $dest"
uid=$(id -u)
gid=$(id -g)
losetup=/sbin/losetup
#types="vfat captive-ntfs ntfs"

if [ -n "$copt" ]; then
    if [ -f "$src" ]; then
        read -p "$src already exists.  Overwrite (Y/N*): "
        echo $REPLY | egrep -i '^ *y' || abort
    fi
    muss qemu-img create -f raw "$src" $copt
fi

if [ -n "$copt$fopt" ]; then
    muss /sbin/fdisk -C $(( $(stat "$src" -c "%s") / 255 / 63 /512)) "$src"
fi

x=$(/sbin/fdisk -ul "$src" 2>/dev/null)
[ -z "$x" ] && abort "Can't 'fdisk -ul $src'"
 echo $x | grep -q "This doesn't look like a partition table" &&
  abort "You selected a partition, but there is no valid partition table"
echo "Mounting harddisk image '$src' ..."
# win-b1              63    20964824    10482381    7  HPFS/NTFS
#      ()1            ()2   (                       ){3}  ()3
echo "$x" | grep -v 'Extended$' |
 sed -ne "s#^${src}p\?\([0-9]\+\)[ *]*\([0-9]*\) \+\([0-9]\++\? 
\+\)\{3\}\(.\+\)#\1 \2 \3 \4#p" |
 while read partno start id system; do
    d_echo 2 "p:$partno start:$start id:$id sys:$system"
    case $system in
        Extended) continue;;
        FAT*) types="msdos vfat"; mkfs=(/sbin/mkfs.msdos -F32);;
        HPFS/NTFS) types="captive-ntfs ntfs";;
        *) types=auto;;
    esac
    echo -n "Patition $partno: trying "
    mkdir -p "$dest/$partno"
    for type in $types; do
        echo -n "$type "
        res=99
        loop_dev=$($losetup -f) 
        [ -z "$loop_dev" ] && abort "Can't find a loop device"
        muss $losetup -o $((start * 512)) $loop_dev "$src"
        if [ -n "$mopt" ]; then
            [ -n "$mkfs" ] ||
               echo "Don't know how to make a $system file system"
            muss "address@hidden" $loop_dev
            # muss $losetup -d $loop_dev
        fi
        if kann0 sudo mount -o uid=$uid,gid=$gid,users \
                  -t  $type "$loop_dev" "$dest/$partno"; then
            kann0 sudo chmod a+xrw "$2"
            echo "... $type: ok."
            res=0
            break
        fi
    done
    [ "$res" -eq 0 ] || abort "Error mounting partition $partno"
done






reply via email to

[Prev in Thread] Current Thread [Next in Thread]