@@ -8,6 +8,7 @@ IMAGE3=image3
88IMAGES=" ${IMAGE1} ${IMAGE2} ${IMAGE3} "
99
1010cleanup () {
11+ kill_nbd_server
1112 cleanup_tempdir
1213 remove_images
1314}
@@ -68,6 +69,10 @@ remove_images() {
6869 done
6970}
7071
72+ kill_nbd_server () {
73+ pkill -9 qemu-nbd || true
74+ }
75+
7176show_diff ()
7277{
7378 local file1=$1
@@ -393,6 +398,177 @@ EOF
393398 remove_image " ${dest_image} "
394399}
395400
401+ test_import_nbd_stream_qcow2 () {
402+ local base_image=$1
403+ local dest_image=$2
404+
405+ qemu-nbd -f qcow2 --read-only --shared 10 --persistent --fork \
406+ ${TEMPDIR} /${base_image} .qcow2
407+
408+ cat > ${TEMPDIR} /spec.json << EOF
409+ {
410+ "type": "raw",
411+ "stream": {
412+ "type": "nbd",
413+ "uri": "nbd://localhost"
414+ }
415+ }
416+ EOF
417+ cat ${TEMPDIR} /spec.json
418+
419+ cat ${TEMPDIR} /spec.json | rbd migration prepare --import-only \
420+ --source-spec-path - ${dest_image}
421+ compare_images ${base_image} ${dest_image}
422+ rbd migration abort ${dest_image}
423+
424+ rbd migration prepare --import-only \
425+ --source-spec-path ${TEMPDIR} /spec.json ${dest_image}
426+ compare_images ${base_image} ${dest_image}
427+ rbd migration execute ${dest_image}
428+ compare_images ${base_image} ${dest_image}
429+ rbd migration commit ${dest_image}
430+ compare_images ${base_image} ${dest_image}
431+ remove_image " ${dest_image} "
432+
433+ # shortest possible URI
434+ rbd migration prepare --import-only \
435+ --source-spec ' {"type": "raw", "stream": {"type": "nbd", "uri": "nbd://"}}' \
436+ ${dest_image}
437+ rbd migration abort ${dest_image}
438+
439+ # non-existing export name
440+ expect_false rbd migration prepare --import-only \
441+ --source-spec ' {"type": "raw", "stream": {"type": "nbd", "uri": "nbd:///myexport"}}' \
442+ ${dest_image}
443+ expect_false rbd migration prepare --import-only \
444+ --source-spec ' {"type": "raw", "stream": {"type": "nbd", "uri": "nbd://localhost/myexport"}}' \
445+ ${dest_image}
446+
447+ kill_nbd_server
448+ qemu-nbd --export-name myexport -f qcow2 --read-only --shared 10 --persistent --fork \
449+ ${TEMPDIR} /${base_image} .qcow2
450+
451+ rbd migration prepare --import-only \
452+ --source-spec ' {"type": "raw", "stream": {"type": "nbd", "uri": "nbd:///myexport"}}' \
453+ ${dest_image}
454+ rbd migration abort ${dest_image}
455+
456+ rbd migration prepare --import-only \
457+ --source-spec ' {"type": "raw", "stream": {"type": "nbd", "uri": "nbd://localhost/myexport"}}' \
458+ ${dest_image}
459+ rbd migration abort ${dest_image}
460+
461+ kill_nbd_server
462+
463+ # server not running
464+ expect_false rbd migration prepare --import-only \
465+ --source-spec-path ${TEMPDIR} /spec.json ${dest_image}
466+ expect_false rbd migration prepare --import-only \
467+ --source-spec ' {"type": "raw", "stream": {"type": "nbd", "uri": "nbd://"}}' \
468+ ${dest_image}
469+
470+ # no URI
471+ expect_false rbd migration prepare --import-only \
472+ --source-spec ' {"type": "raw", "stream": {"type": "nbd"}}' \
473+ ${dest_image}
474+
475+ # invalid URI
476+ expect_false rbd migration prepare --import-only \
477+ --source-spec ' {"type": "raw", "stream": {"type": "nbd", "uri": 123456}}' \
478+ ${dest_image}
479+
480+ # libnbd - nbd_get_errno() returns an error
481+ # nbd_connect_uri: unknown URI scheme: NULL: Invalid argument (errno = 22)
482+ expect_false rbd migration prepare --import-only \
483+ --source-spec ' {"type": "raw", "stream": {"type": "nbd", "uri": ""}}' \
484+ ${dest_image}
485+ expect_false rbd migration prepare --import-only \
486+ --source-spec ' {"type": "raw", "stream": {"type": "nbd", "uri": "foo.example.com"}}' \
487+ ${dest_image}
488+
489+ # libnbd - nbd_get_errno() returns 0, EIO fallback
490+ # nbd_connect_uri: getaddrinfo: foo.example.com:10809: Name or service not known (errno = 0)
491+ expect_false rbd migration prepare --import-only \
492+ --source-spec ' {"type": "raw", "stream": {"type": "nbd", "uri": "nbd://foo.example.com"}}' \
493+ ${dest_image}
494+ }
495+
496+ test_import_nbd_stream_raw () {
497+ local base_image=$1
498+ local dest_image=$2
499+
500+ qemu-nbd -f raw --read-only --shared 10 --persistent --fork \
501+ --socket ${TEMPDIR} /qemu-nbd-${base_image} ${TEMPDIR} /${base_image}
502+ qemu-nbd -f raw --read-only --shared 10 --persistent --fork \
503+ --socket ${TEMPDIR} /qemu-nbd-${base_image} @1 ${TEMPDIR} /${base_image} @1
504+ qemu-nbd -f raw --read-only --shared 10 --persistent --fork \
505+ --socket ${TEMPDIR} /qemu-nbd-${base_image} @2 ${TEMPDIR} /${base_image} @2
506+
507+ cat > ${TEMPDIR} /spec.json << EOF
508+ {
509+ "type": "raw",
510+ "stream": {
511+ "type": "nbd",
512+ "uri": "nbd+unix:///?socket=${TEMPDIR} /qemu-nbd-${base_image} "
513+ },
514+ "snapshots": [{
515+ "type": "raw",
516+ "name": "snap1",
517+ "stream": {
518+ "type": "nbd",
519+ "uri": "nbd+unix:///?socket=${TEMPDIR} /qemu-nbd-${base_image} @1"
520+ }
521+ }, {
522+ "type": "raw",
523+ "name": "snap2",
524+ "stream": {
525+ "type": "nbd",
526+ "uri": "nbd+unix:///?socket=${TEMPDIR} /qemu-nbd-${base_image} @2"
527+ }
528+ }]
529+ }
530+ EOF
531+ cat ${TEMPDIR} /spec.json
532+
533+ rbd migration prepare --import-only \
534+ --source-spec-path ${TEMPDIR} /spec.json ${dest_image}
535+
536+ rbd snap create ${dest_image} @head
537+ rbd bench --io-type write --io-pattern rand --io-size 32K --io-total 4M ${dest_image}
538+
539+ compare_images " ${base_image} @1" " ${dest_image} @snap1"
540+ compare_images " ${base_image} @2" " ${dest_image} @snap2"
541+ compare_images " ${base_image} " " ${dest_image} @head"
542+
543+ rbd migration abort ${dest_image}
544+
545+ cat ${TEMPDIR} /spec.json | rbd migration prepare --import-only \
546+ --source-spec-path - ${dest_image}
547+
548+ rbd snap create ${dest_image} @head
549+ rbd bench --io-type write --io-pattern rand --io-size 64K --io-total 8M ${dest_image}
550+
551+ compare_images " ${base_image} @1" " ${dest_image} @snap1"
552+ compare_images " ${base_image} @2" " ${dest_image} @snap2"
553+ compare_images " ${base_image} " " ${dest_image} @head"
554+
555+ rbd migration execute ${dest_image}
556+
557+ compare_images " ${base_image} @1" " ${dest_image} @snap1"
558+ compare_images " ${base_image} @2" " ${dest_image} @snap2"
559+ compare_images " ${base_image} " " ${dest_image} @head"
560+
561+ rbd migration commit ${dest_image}
562+
563+ compare_images " ${base_image} @1" " ${dest_image} @snap1"
564+ compare_images " ${base_image} @2" " ${dest_image} @snap2"
565+ compare_images " ${base_image} " " ${dest_image} @head"
566+
567+ remove_image " ${dest_image} "
568+
569+ kill_nbd_server
570+ }
571+
396572# make sure rbd pool is EMPTY.. this is a test script!!
397573rbd ls 2>&1 | wc -l | grep -v ' ^0$' && echo " nonempty rbd pool, aborting! run this script on an empty test cluster only." && exit 1
398574
@@ -404,7 +580,11 @@ export_base_image ${IMAGE1}
404580
405581test_import_native_format ${IMAGE1} ${IMAGE2}
406582test_import_qcow_format ${IMAGE1} ${IMAGE2}
583+
407584test_import_qcow2_format ${IMAGE2} ${IMAGE3}
585+ test_import_nbd_stream_qcow2 ${IMAGE2} ${IMAGE3}
586+
408587test_import_raw_format ${IMAGE1} ${IMAGE2}
588+ test_import_nbd_stream_raw ${IMAGE1} ${IMAGE2}
409589
410590echo OK
0 commit comments