92 lines
2.5 KiB
C
92 lines
2.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <sys/ioctl.h>
|
|
#include <linux/fs.h>
|
|
|
|
#define BLKSZ (100 * 1024 * 1024) // 100MB buffer
|
|
#define PATTERN_SIZE 7 // 7-byte repeating pattern
|
|
#define 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 = (float)total_written / total_size;
|
|
if (percent > 1.0) percent = 1.0;
|
|
|
|
int filled = (int)(percent * BAR_WIDTH);
|
|
printf("\r[");
|
|
for (int i = 0; i < BAR_WIDTH; i++) {
|
|
printf(i < filled ? "#" : " ");
|
|
}
|
|
printf("] %lu MB / %lu MB", total_written / (1024 * 1024), total_size / (1024 * 1024));
|
|
fflush(stdout);
|
|
}
|
|
|
|
void fill_device(const char *device) {
|
|
int fd = open(device, 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
|
|
unsigned char *buffer = (unsigned char *)malloc(BLKSZ);
|
|
if (!buffer) {
|
|
perror("Failed to allocate buffer");
|
|
close(fd);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// 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 < BLKSZ; i++) {
|
|
buffer[i] = pattern[i % PATTERN_SIZE];
|
|
}
|
|
|
|
printf("Writing to %s with a 7-byte repeating pattern in 100MB chunks...\n", device);
|
|
printf("Total device size: %lu MB\n", total_size / (1024 * 1024));
|
|
|
|
size_t total_written = 0;
|
|
while (total_written < total_size) {
|
|
ssize_t written = write(fd, buffer, BLKSZ);
|
|
if (written < 0) {
|
|
perror("\nWrite failed");
|
|
break;
|
|
}
|
|
total_written += written;
|
|
print_progress(total_written, total_size);
|
|
}
|
|
|
|
printf("\nCompleted. Total written: %lu MB\n", total_written / (1024 * 1024));
|
|
|
|
free(buffer);
|
|
close(fd);
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc != 2) {
|
|
fprintf(stderr, "Usage: %s <device>\n", argv[0]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
fill_device(argv[1]);
|
|
return EXIT_SUCCESS;
|
|
}
|