diff --git a/src/disk_tool/disk_tool.h b/src/disk_tool/disk_tool.h index b0f3a427..1f910d5f 100644 --- a/src/disk_tool/disk_tool.h +++ b/src/disk_tool/disk_tool.h @@ -36,6 +36,7 @@ struct resizer_data_moving_t; struct vitastor_dev_info_t { std::string path; + std::string orig_path; // as given on the command line, for the "skipped" JSON bool is_hdd; json11::Json pt; // pt = partition table int osd_part_count; @@ -53,6 +54,8 @@ struct disk_tool_t bool dump_with_blocks = false, dump_with_data = false; bool dump_as_old = false; int log_level = 1; + // prepare: disks given but not used, for the JSON output + json11::Json::array skipped_devices; double meta_reserve_multiple = 2; uint64_t meta_reserve_min_size = (uint64_t)1024*1024*1024; blockstore_disk_t dsk; diff --git a/src/disk_tool/disk_tool_prepare.cpp b/src/disk_tool/disk_tool_prepare.cpp index c6621567..269b47ac 100644 --- a/src/disk_tool/disk_tool_prepare.cpp +++ b/src/disk_tool/disk_tool_prepare.cpp @@ -375,6 +375,7 @@ std::vector disk_tool_t::collect_devices(const std::vector< if (dev.substr(0, 5) != "/dev/") { fprintf(stderr, "%s does not start with /dev/, ignoring\n", dev.c_str()); + skipped_devices.push_back(json11::Json::object{ { "device_path", orig_dev }, { "reason", "not_dev_path" } }); continue; } struct stat sys_st; @@ -386,6 +387,7 @@ std::vector disk_tool_t::collect_devices(const std::vector< else if (!dev_size) { fprintf(stderr, "%s does not exist, skipping\n", dev.c_str()); + skipped_devices.push_back(json11::Json::object{ { "device_path", orig_dev }, { "reason", "not_found" } }); continue; } if (stat(("/sys/block/"+dev.substr(5)).c_str(), &sys_st) < 0) @@ -393,6 +395,7 @@ std::vector disk_tool_t::collect_devices(const std::vector< if (errno == ENOENT) { fprintf(stderr, "%s is probably a partition (no entry in /sys/block/), ignoring\n", dev.c_str()); + skipped_devices.push_back(json11::Json::object{ { "device_path", orig_dev }, { "reason", "partition" } }); continue; } fprintf(stderr, "Error checking /sys/block/%s: %s\n", dev.c_str()+5, strerror(errno)); @@ -415,6 +418,7 @@ std::vector disk_tool_t::collect_devices(const std::vector< if (r != 0 || out != "") { fprintf(stderr, "%s contains data, skipping:\n %s\n", dev.c_str(), str_replace(trim(out), "\n", "\n ").c_str()); + skipped_devices.push_back(json11::Json::object{ { "device_path", orig_dev }, { "reason", "contains_data" } }); continue; } } @@ -424,6 +428,7 @@ std::vector disk_tool_t::collect_devices(const std::vector< osds++; devinfo.push_back((vitastor_dev_info_t){ .path = dev, + .orig_path = orig_dev, .is_hdd = is_hdd, .pt = pt, .osd_part_count = osds, @@ -590,6 +595,7 @@ std::vector disk_tool_t::get_new_data_parts(vitastor_dev_info_t & d if (osds_exist >= osd_per_disk || (dev.free+osds_size) < dev.size*(100-max_other_percent)/100) { fprintf(stderr, "%s is already partitioned, skipping\n", dev.path.c_str()); + skipped_devices.push_back(json11::Json::object{ { "device_path", dev.orig_path }, { "reason", "already_partitioned" } }); use_parts.clear(); } else @@ -713,6 +719,11 @@ int disk_tool_t::prepare(std::vector devices) if (!devinfo.size()) { fprintf(stderr, "No suitable devices found\n"); + if (json && options.find("dry_run") != options.end()) + printf("%s\n", json11::Json(json11::Json::object{ { "devices", json11::Json::array() } }).dump().c_str()); + else if (json) + printf("%s\n", json11::Json(json11::Json::object{ { "osds", json11::Json::array() }, + { "errors", json11::Json::array() }, { "skipped", skipped_devices } }).dump().c_str()); return 1; } bool explicit_fast = options.find("fast_devices") != options.end(); @@ -819,6 +830,8 @@ int disk_tool_t::prepare(std::vector devices) printf("%s\n", json11::Json(json11::Json::object{{ "devices", results }}).dump().c_str()); return 0; } + // An OSD that fails doesn't stop the others, but the command fails + bool prepare_failed = false; for (auto & dev: devinfo) { // Select new partitions and create an OSD on each of them @@ -839,6 +852,8 @@ int disk_tool_t::prepare(std::vector devices) // Treat all disks as SSDs if not in the hybrid mode json11::Json::object result; int r = prepare_one(options, dev.is_hdd ? 1 : 0, result); + if (r != 0) + prepare_failed = true; if (json) { if (!r) @@ -856,7 +871,7 @@ int disk_tool_t::prepare(std::vector devices) } if (json) { - printf("%s\n", json11::Json(json11::Json::object{ { "osds", all_results }, { "errors", errors } }).dump().c_str()); + printf("%s\n", json11::Json(json11::Json::object{ { "osds", all_results }, { "errors", errors }, { "skipped", skipped_devices } }).dump().c_str()); } - return 0; + return prepare_failed ? 1 : 0; } diff --git a/tests/test_resize_auto.sh b/tests/test_resize_auto.sh index b0c3eaa4..82546106 100755 --- a/tests/test_resize_auto.sh +++ b/tests/test_resize_auto.sh @@ -12,9 +12,13 @@ dd if=/dev/zero of=./testdata/bin/test_meta.bin bs=1 count=1 seek=$((1024*1024*1 LOOP2=$(sudo losetup --show -f ./testdata/bin/test_meta.bin) trap "kill -9 $(jobs -p) || true; sudo losetup -d $LOOP1 $LOOP2"' || true' EXIT -# prepare should refuse a disk given both in the device list and in --fast-devices -sudo -E build/src/disk_tool/vitastor-disk-test prepare --dry-run --hybrid --fast-devices $LOOP1 $LOOP1 2>&1 | - grep 'both in the device list and in --fast-devices' +# prepare should refuse a disk given both in the device list and in --fast-devices, also through a symlink +ln -s $LOOP1 ./testdata/bin/test_osd1_fast +if sudo -E build/src/disk_tool/vitastor-disk-test prepare --dry-run --hybrid --fast-devices ./testdata/bin/test_osd1_fast $LOOP1 \ + 2>./testdata/prepare_err.txt; then + format_error "prepare accepted a disk given both in the device list and in --fast-devices" +fi +grep 'both in the device list and in --fast-devices' ./testdata/prepare_err.txt # also test prepare --hybrid :) # non-vitastor random type UUID to prevent udev activation @@ -147,4 +151,32 @@ sudo -E build/src/disk_tool/vitastor-disk-test resize --data-size max ${LOOP1}p1 sudo -E build/src/disk_tool/vitastor-disk dump-meta ${LOOP1}p1 | jq -S '. + {"entries": (.entries | sort_by(.stripe)) }' > ./testdata/2.json diff ./testdata/1.json ./testdata/2.json +# prepare should exit with an error when it fails to initialize an OSD +dd if=/dev/zero of=./testdata/bin/test_bad.bin bs=1 count=1 seek=$((1024*1024*1024-1)) +LOOP3=$(sudo losetup --show -f ./testdata/bin/test_bad.bin) +trap "kill -9 $(jobs -p) || true; sudo losetup -d $LOOP1 $LOOP2 $LOOP3"' || true' EXIT +if sudo -E build/src/disk_tool/vitastor-disk-test prepare --no_init 1 --osd_num 987654 \ + --block_size 4M --data_csum_type crc32c --csum_block_size 4k $LOOP3; then + format_error "prepare exited with 0 after failing to initialize an OSD" +fi + +# prepare should exit with 0 when it skips every disk, and list the skipped disks in JSON by the path given +sudo -E build/src/disk_tool/vitastor-disk-test prepare --json --no_init 1 --osd_num 987655 ./testdata/bin/test_osd1_link1 > ./testdata/prepare.json || \ + format_error "prepare failed when it had nothing to do" +[[ "$(jq -c '[(.osds | length), .skipped]' ./testdata/prepare.json)" == '[0,[{"device_path":"./testdata/bin/test_osd1_link1","reason":"already_partitioned"}]]' ]] || \ + format_error "prepare didn't list the skipped disk by the path given" +dd if=/dev/zero of=./testdata/bin/test_osd4.bin bs=1 count=1 seek=$((10*1024*1024*1024-1)) +LOOP4=$(sudo losetup --show -f ./testdata/bin/test_osd4.bin) +trap "kill -9 $(jobs -p) || true; sudo losetup -d $LOOP1 $LOOP2 $LOOP3 $LOOP4"' || true' EXIT +sudo -E build/src/disk_tool/vitastor-disk-test prepare --json --no_init 1 --osd_num 987655 $LOOP1 $LOOP4 > ./testdata/prepare.json +[[ "$(jq -c '[(.osds | length), .skipped[0].device_path]' ./testdata/prepare.json)" == '[1,"'$LOOP1'"]' ]] || \ + format_error "prepare didn't create an OSD on one disk and skip the other" +if sudo -E build/src/disk_tool/vitastor-disk-test prepare --json /dev/nonexistent > ./testdata/prepare.json; then + format_error "prepare exited with 0 without a suitable disk" +fi +[[ "$(jq -c .skipped ./testdata/prepare.json)" == '[{"device_path":"/dev/nonexistent","reason":"not_found"}]' ]] || \ + format_error "prepare didn't list a missing disk" +[[ "$(sudo -E build/src/disk_tool/vitastor-disk-test prepare --dry-run --json /dev/nonexistent | jq -c .devices)" == '[]' ]] || \ + format_error "prepare --dry-run printed the wrong JSON without a suitable disk" + format_green OK