#include #include #include #include #include #include constexpr size_t BUFFER_SIZE = 100 * 1024 * 1024; // 100MB buffer constexpr size_t PATTERN_SIZE = 7; // 7-byte repeating pattern constexpr int BAR_WIDTH = 50; // Progress bar width // Function to get total size of block device size_t get_device_size(int fd) { size_t size = 0; if (ioctl(fd, BLKGETSIZE64, &size) < 0) { perror("Failed to get device size"); exit(EXIT_FAILURE); } return size; } // Function to print progress bar void print_progress(size_t total_written, size_t total_size) { float percent = static_cast(total_written) / total_size; if (percent > 1.0) percent = 1.0; int filled = static_cast(percent * BAR_WIDTH); std::cout << "\r["; for (int i = 0; i < BAR_WIDTH; i++) { std::cout << (i < filled ? '#' : ' '); } std::cout << "] " << (total_written / (1024 * 1024)) << " MB / " << (total_size / (1024 * 1024)) << " MB" << std::flush; } void fill_device(const std::string &device) { int fd = open(device.c_str(), O_WRONLY); if (fd < 0) { perror("Failed to open device"); exit(EXIT_FAILURE); } // Get total device size size_t total_size = get_device_size(fd); // Allocate 100MB buffer std::vector buffer(BUFFER_SIZE); // Define the 7-byte repeating pattern unsigned char pattern[PATTERN_SIZE] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; // Fill the buffer with the repeating pattern for (size_t i = 0; i < BUFFER_SIZE; i++) { buffer[i] = pattern[i % PATTERN_SIZE]; } std::cout << "Writing to " << device << " with a 7-byte repeating pattern in 100MB chunks...\n"; std::cout << "Total device size: " << (total_size / (1024 * 1024)) << " MB\n"; size_t total_written = 0; while (total_written < total_size) { ssize_t written = write(fd, buffer.data(), BUFFER_SIZE); if (written < 0) { perror("\nWrite failed"); break; } total_written += written; print_progress(total_written, total_size); } std::cout << "\nCompleted. Total written: " << (total_written / (1024 * 1024)) << " MB\n"; close(fd); } int main(int argc, char *argv[]) { if (argc != 2) { std::cerr << "Usage: " << argv[0] << " \n"; return EXIT_FAILURE; } fill_device(argv[1]); return EXIT_SUCCESS; }