1

I'm trying to write a char array to a vhd disk using libvhd (c++). But it failes with 'error number -22' ( -EINVAL /* Invalid argument */). There is no problem in opening and reading vhd disks using libvhd but I don't know what is the problem in writing to it. Here is part of my code for writing:

vhd_context_t *ctx = NULL;
int size,err;
int z;

char *buf =(char *)malloc(VHD_SECTOR_SIZE);
z=vhd_unix_open("/home/zahra/Desktop/zahra/macemu/sys.vhd", &size, false, &ctx);
printf("z: %d \n",z);
/*reading a sector from a vhd disk*/
if ((err = vhd_io_read(ctx,  buf, (uint64_t)0 ,(uint32_t) 1))){
        printf("vhd read error %d \n", err);

}
BIO_dump_fp (stdout, (const char *)buf, size_t (VHD_SECTOR_SIZE));
/*reading is OK*/
/*writing check*/
  char * plaintext= (char *)"This is a writing test";  
if ((err = vhd_io_write(ctx,plaintext ,(uint64_t)0 ,(uint32_t)1))) {
            printf("vhd write error %d\n", err);
            printf("vhd footer current size %lld\n", ctx->footer.curr_size);}

Writing fails with 'vhd write error -22'.

  • I have zero idea what libvhd is but it returns EINVAL in bunch of places: https://github.com/xapi-project/blktap/blob/master/vhd/lib/libvhd.c – mostruash Jan 18 '16 at 23:02

1 Answers1

0

I fixed the problem by using 'posix_memalign' for memory allocations.