block.c (df2dbb4a508ebea7bcacff3f07caecbae969d528) block.c (f88e1a4201e31cac0438df395dfcdd45ac35c17d)
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 2744 unchanged lines hidden (view full) ---

2753{
2754 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2755}
2756
2757int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2758{
2759 return bs->dirty_count;
2760}
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 2744 unchanged lines hidden (view full) ---

2753{
2754 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2755}
2756
2757int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2758{
2759 return bs->dirty_count;
2760}
2761
2762int bdrv_img_create(const char *filename, const char *fmt,
2763 const char *base_filename, const char *base_fmt,
2764 char *options, uint64_t img_size, int flags)
2765{
2766 QEMUOptionParameter *param = NULL, *create_options = NULL;
2767 QEMUOptionParameter *backing_fmt;
2768 BlockDriverState *bs = NULL;
2769 BlockDriver *drv, *proto_drv;
2770 int ret = 0;
2771
2772 /* Find driver and parse its options */
2773 drv = bdrv_find_format(fmt);
2774 if (!drv) {
2775 error_report("Unknown file format '%s'", fmt);
2776 ret = -1;
2777 goto out;
2778 }
2779
2780 proto_drv = bdrv_find_protocol(filename);
2781 if (!proto_drv) {
2782 error_report("Unknown protocol '%s'", filename);
2783 ret = -1;
2784 goto out;
2785 }
2786
2787 create_options = append_option_parameters(create_options,
2788 drv->create_options);
2789 create_options = append_option_parameters(create_options,
2790 proto_drv->create_options);
2791
2792 /* Create parameter list with default values */
2793 param = parse_option_parameters("", create_options, param);
2794
2795 set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
2796
2797 /* Parse -o options */
2798 if (options) {
2799 param = parse_option_parameters(options, create_options, param);
2800 if (param == NULL) {
2801 error_report("Invalid options for file format '%s'.", fmt);
2802 ret = -1;
2803 goto out;
2804 }
2805 }
2806
2807 if (base_filename) {
2808 if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
2809 base_filename)) {
2810 error_report("Backing file not supported for file format '%s'",
2811 fmt);
2812 ret = -1;
2813 goto out;
2814 }
2815 }
2816
2817 if (base_fmt) {
2818 if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
2819 error_report("Backing file format not supported for file "
2820 "format '%s'", fmt);
2821 ret = -1;
2822 goto out;
2823 }
2824 }
2825
2826 backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
2827 if (backing_fmt && backing_fmt->value.s) {
2828 if (!bdrv_find_format(backing_fmt->value.s)) {
2829 error_report("Unknown backing file format '%s'",
2830 backing_fmt->value.s);
2831 ret = -1;
2832 goto out;
2833 }
2834 }
2835
2836 // The size for the image must always be specified, with one exception:
2837 // If we are using a backing file, we can obtain the size from there
2838 if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) {
2839 QEMUOptionParameter *backing_file =
2840 get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
2841
2842 if (backing_file && backing_file->value.s) {
2843 uint64_t size;
2844 const char *fmt = NULL;
2845 char buf[32];
2846
2847 if (backing_fmt && backing_fmt->value.s) {
2848 fmt = backing_fmt->value.s;
2849 }
2850
2851 bs = bdrv_new("");
2852
2853 ret = bdrv_open(bs, backing_file->value.s, flags, drv);
2854 if (ret < 0) {
2855 error_report("Could not open '%s'", filename);
2856 ret = -1;
2857 goto out;
2858 }
2859 bdrv_get_geometry(bs, &size);
2860 size *= 512;
2861
2862 snprintf(buf, sizeof(buf), "%" PRId64, size);
2863 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
2864 } else {
2865 error_report("Image creation needs a size parameter");
2866 ret = -1;
2867 goto out;
2868 }
2869 }
2870
2871 printf("Formatting '%s', fmt=%s ", filename, fmt);
2872 print_option_parameters(param);
2873 puts("");
2874
2875 ret = bdrv_create(drv, filename, param);
2876
2877 if (ret < 0) {
2878 if (ret == -ENOTSUP) {
2879 error_report("Formatting or formatting option not supported for "
2880 "file format '%s'", fmt);
2881 } else if (ret == -EFBIG) {
2882 error_report("The image size is too large for file format '%s'",
2883 fmt);
2884 } else {
2885 error_report("%s: error while creating %s: %s", filename, fmt,
2886 strerror(-ret));
2887 }
2888 }
2889
2890out:
2891 free_option_parameters(create_options);
2892 free_option_parameters(param);
2893
2894 if (bs) {
2895 bdrv_delete(bs);
2896 }
2897 if (ret) {
2898 return 1;
2899 }
2900 return 0;
2901}