diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index ad781d0c..8b181887 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -851,6 +851,24 @@ jobs: echo "" done + test_qemu_img_create: + runs-on: ubuntu-latest + needs: build + container: ${{env.TEST_IMAGE}}:${{github.sha}} + steps: + - name: Run test + id: test + timeout-minutes: 3 + run: /root/vitastor/tests/test_qemu_img_create.sh + - name: Print logs + if: always() && steps.test.outcome == 'failure' + run: | + for i in /root/vitastor/testdata/*.log /root/vitastor/testdata/*.txt; do + echo "-------- $i --------" + cat $i + echo "" + done + test_snapshot_chain: runs-on: ubuntu-latest needs: build diff --git a/docs/usage/qemu.en.md b/docs/usage/qemu.en.md index 20fed330..74d7c074 100644 --- a/docs/usage/qemu.en.md +++ b/docs/usage/qemu.en.md @@ -61,6 +61,9 @@ For example, to upload a VM image into Vitastor, run: qemu-img convert -f qcow2 debian10.qcow2 -p -O raw 'vitastor:image=debian10' ``` +The target image must already exist (create it with `vitastor-cli create`) and be at least +as large as the source: qemu-img doesn't create or resize Vitastor images. + You can also specify `:pool=:inode=:size=` instead of `:image=` if you don't want to use inode metadata. diff --git a/docs/usage/qemu.ru.md b/docs/usage/qemu.ru.md index c3b903e7..e26d8d63 100644 --- a/docs/usage/qemu.ru.md +++ b/docs/usage/qemu.ru.md @@ -63,6 +63,9 @@ qemu-system-x86_64 -enable-kvm -m 1024 \ qemu-img convert -f qcow2 debian10.qcow2 -p -O raw 'vitastor:image=testimg' ``` +Целевой образ должен уже существовать (создайте его через `vitastor-cli create`) и быть +не меньше исходного: qemu-img не создаёт образы Vitastor и не меняет их размер. + Если вы не хотите обращаться к образу по имени, вместо `:image=` можно указать номер пула, номер инода и размер: `:pool=:inode=:size=`. diff --git a/src/client/qemu_driver.c b/src/client/qemu_driver.c index 5a67260d..2db2628a 100644 --- a/src/client/qemu_driver.c +++ b/src/client/qemu_driver.c @@ -956,7 +956,9 @@ static int coroutine_fn vitastor_co_create_opts( const char *url, QemuOpts *opts, Error **errp) { QDict *options; - int ret; + BlockDriverState *bs; + uint64_t size, image_size; + int ret = 0; options = qdict_new(); vitastor_parse_filename(url, options, errp); @@ -966,9 +968,36 @@ static int coroutine_fn vitastor_co_create_opts( goto out; } - // inodes don't require creation in Vitastor. FIXME: They will when there will be some metadata - - ret = 0; + // Inodes don't require creation, but named images do, and QEMU can't create them: + // only check that the image exists and is large enough + if (!qdict_get_try_str(options, "image")) + { + goto out; + } +#if QEMU_VERSION_MAJOR >= 8 + bs = bdrv_co_open(url, NULL, NULL, BDRV_O_PROTOCOL, errp); +#else + bs = bdrv_open(url, NULL, NULL, BDRV_O_PROTOCOL, errp); +#endif + if (!bs) + { + error_prepend(errp, "Can't open the image (QEMU doesn't create Vitastor images, use vitastor-cli create): "); + ret = -ENOENT; + goto out; + } + size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0); + image_size = ((VitastorClient*)bs->opaque)->size; + if (image_size < size) + { + error_setg(errp, "Image is smaller than requested: %"PRIu64" < %"PRIu64" bytes" + " (QEMU doesn't resize Vitastor images, use vitastor-cli modify --resize)", image_size, size); + ret = -ENOTSUP; + } +#if QEMU_VERSION_MAJOR > 8 || QEMU_VERSION_MAJOR == 8 && QEMU_VERSION_MINOR >= 1 + bdrv_co_unref(bs); +#else + bdrv_unref(bs); +#endif out: qobject_unref(options); return ret; diff --git a/tests/run_tests.sh b/tests/run_tests.sh index caac41b7..f480f9d9 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -71,6 +71,8 @@ OLD=1 ./test_move_reappear.sh ./test_rm_degraded.sh ./test_describe.sh +./test_qemu_img_create.sh + ./test_snapshot_chain.sh SCHEME=ec ./test_snapshot_chain.sh ENCRYPTED=1 ./test_snapshot_chain.sh diff --git a/tests/test_qemu_img_create.sh b/tests/test_qemu_img_create.sh new file mode 100755 index 00000000..86a801b8 --- /dev/null +++ b/tests/test_qemu_img_create.sh @@ -0,0 +1,30 @@ +#!/bin/bash -ex + +# qemu-img convert doesn't flush with its default cache=unsafe +IMMEDIATE_COMMIT=1 +. `dirname $0`/run_3osds.sh +check_qemu + +# qemu-img create doesn't create Vitastor images, it only checks that they exist and are large enough + +if qemu-img create -f raw "vitastor:config_path=$VITASTOR_CFG:image=testimg" 32M; then + format_error "qemu-img create succeeded for a missing image" +fi + +$VITASTOR_CLI create -s 32M testimg + +if qemu-img create -f raw "vitastor:config_path=$VITASTOR_CFG:image=testimg" 64M; then + format_error "qemu-img create succeeded for an image smaller than requested" +fi +qemu-img create -f raw "vitastor:config_path=$VITASTOR_CFG:image=testimg" 32M + +# qemu-img convert without -n also calls create +dd if=/dev/urandom of=./testdata/bin/src.bin bs=1M count=16 +qemu-img convert -p -f raw ./testdata/bin/src.bin -O raw "vitastor:config_path=$VITASTOR_CFG:image=testimg" +qemu-img convert -p -f raw "vitastor:config_path=$VITASTOR_CFG:image=testimg" -O raw ./testdata/bin/check.bin +cmp -n $((16*1024*1024)) ./testdata/bin/src.bin ./testdata/bin/check.bin + +# Inodes addressed by number don't require creation +qemu-img create -f raw "vitastor:config_path=$VITASTOR_CFG:pool=1:inode=2:size=$((32*1024*1024))" 32M + +format_green OK