public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <levinsasha928@gmail.com>
To: Prasad Joshi <prasadjoshi124@gmail.com>
Cc: mingo@elte.hu, kvm@vger.kernel.org, penberg@kernel.org,
	asias.hejun@gmail.com, gorcunov@gmail.com,
	chaitanyakulkarni15@gmail.com, ashwini.kulkarni@gmail.com
Subject: Re: [PATCH v2 1/4] kvm tools: Add a wrapper function to open disk images
Date: Fri, 20 May 2011 18:28:05 +0300	[thread overview]
Message-ID: <1305905285.3205.18.camel@lappy> (raw)
In-Reply-To: <1305903760-7804-1-git-send-email-prasadjoshi124@gmail.com>

On Fri, 2011-05-20 at 16:02 +0100, Prasad Joshi wrote:
> The patch was suggested by Ingo to move the disk image subsystem code
> from the kvm-run.c file. The code to open all of the specified disk
> images is now moved to a wrapper function in disk/core.c.
> 
> Suggested-by: Ingo Molnar <mingo@elte.hu>
> Signed-off-by: Prasad Joshi <prasadjoshi124@gmail.com>
> ---
>  tools/kvm/disk/core.c              |   30 ++++++++++++++++++++++++++++++
>  tools/kvm/include/kvm/disk-image.h |    2 ++
>  tools/kvm/include/kvm/kvm.h        |    1 +
>  tools/kvm/kvm-run.c                |   15 ++++++---------
>  4 files changed, 39 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/kvm/disk/core.c b/tools/kvm/disk/core.c
> index 34852a2..c53944f 100644
> --- a/tools/kvm/disk/core.c
> +++ b/tools/kvm/disk/core.c
> @@ -59,6 +59,36 @@ struct disk_image *disk_image__open(const char *filename, bool readonly)
>  	return NULL;
>  }
>  
> +struct disk_image **disk_image__open_all(const char **filenames, bool *readonly, int count)
> +{
> +	struct disk_image **disks;
> +	int i;
> +
> +	if (count > MAX_DISK_IMAGES)
> +		return NULL;
> +
> +	disks = malloc(sizeof(struct disk_image *) * count);
			sizeof(*disks).

> +	if (!disks)
> +		return NULL;
> +
> +	for (i = 0; i < count; i++) {
> +		if (!filenames[i]) {
> +			disks[i] = NULL;
> +			continue;
> +		}
> +
> +		disks[i] = disk_image__open(filenames[i], readonly[i]);
> +		if (!disks[i])
> +			goto error;
> +	}
> +	return disks;
> +error:
> +	while (--i)
> +		disk_image__close(disks[i]);

Bad things happen here if the first image failed to open (i = -1).

> +
> +	return NULL;
> +}
> +
>  int disk_image__flush(struct disk_image *disk)
>  {
>  	if (disk->ops->flush)
> diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h
> index ac394e8..bf2bfa9 100644
> --- a/tools/kvm/include/kvm/disk-image.h
> +++ b/tools/kvm/include/kvm/disk-image.h
> @@ -23,6 +23,7 @@
>  
>  #define DISK_IMAGE_MMAP		0
>  #define DISK_IMAGE_NOMMAP	1
> +#define MAX_DISK_IMAGES         4
>  
>  struct disk_image;
>  
> @@ -51,6 +52,7 @@ struct disk_image {
>  };
>  
>  struct disk_image *disk_image__open(const char *filename, bool readonly);
> +struct disk_image **disk_image__open_all(const char **filenames, bool *readonly, int count);
>  struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops, int mmap);
>  int disk_image__close(struct disk_image *disk);
>  int disk_image__flush(struct disk_image *disk);
> diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h
> index 3cf6e6c..f419634 100644
> --- a/tools/kvm/include/kvm/kvm.h
> +++ b/tools/kvm/include/kvm/kvm.h
> @@ -32,6 +32,7 @@ struct kvm {
>  	struct interrupt_table	interrupt_table;
>  
>  	const char		*vmlinux;
> +	struct disk_image       **disks;
>  };
>  
>  struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size);
> diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c
> index f7de0fb..e16caac 100644
> --- a/tools/kvm/kvm-run.c
> +++ b/tools/kvm/kvm-run.c
> @@ -44,7 +44,6 @@
>  #define MB_SHIFT		(20)
>  #define MIN_RAM_SIZE_MB		(64ULL)
>  #define MIN_RAM_SIZE_BYTE	(MIN_RAM_SIZE_MB << MB_SHIFT)
> -#define MAX_DISK_IMAGES		4
>  
>  static struct kvm *kvm;
>  static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
> @@ -530,17 +529,15 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
>  	if (!strstr(real_cmdline, "root="))
>  		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
>  
> -	for (i = 0; i < image_count; i++) {
> -		if (image_filename[i]) {
> -			struct disk_image *disk = disk_image__open(image_filename[i], readonly_image[i]);
> -			if (!disk)
> -				die("unable to load disk image %s", image_filename[i]);
> +	kvm->disks  = disk_image__open_all(image_filename, readonly_image, image_count);
> +	if (!kvm->disks)
> +		die("unable to load disk image");

Maybe it's worth printing also which image failed to load, If the user
passed 4 images he'll need to guess which one of the 4 failed.

>  
> -			virtio_blk__init(kvm, disk);
> -		}
> -	}
>  	free(hi);
>  
> +	for (i = 0; i < image_count; i++)
> +		virtio_blk__init(kvm, kvm->disks[i]);
> +
>  	printf("  # kvm run -k %s -m %Lu -c %d\n", kernel_filename, ram_size / 1024 / 1024, nrcpus);
>  
>  	if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename,

-- 

Sasha.


      parent reply	other threads:[~2011-05-20 15:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-20 15:02 [PATCH v2 1/4] kvm tools: Add a wrapper function to open disk images Prasad Joshi
2011-05-20 15:02 ` [PATCH v2 2/4] kvm tools: Close the disk images after the guest shuts down Prasad Joshi
2011-05-20 15:29   ` Sasha Levin
2011-05-20 15:02 ` [PATCH v2 3/4] kvm tools: Add a wrapper function to initialize all virtio block devices Prasad Joshi
2011-05-20 15:02 ` [PATCH v2 4/4] kvm tools: Release memory allocated during virtio block initialization Prasad Joshi
2011-05-20 15:28 ` Sasha Levin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1305905285.3205.18.camel@lappy \
    --to=levinsasha928@gmail.com \
    --cc=ashwini.kulkarni@gmail.com \
    --cc=asias.hejun@gmail.com \
    --cc=chaitanyakulkarni15@gmail.com \
    --cc=gorcunov@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=penberg@kernel.org \
    --cc=prasadjoshi124@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox