/
stressdrive.c
273 lines (232 loc) · 8.17 KB
/
stressdrive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// stressdrive.c 1.2
// Copyright (c) 2011-2018 Jonathan 'Wolf' Rentzsch: http://rentzsch.com
// Some rights reserved: http://opensource.org/licenses/mit
// https://github.com/rentzsch/stressdrive
#define _BSD_SOURCE
#include <fcntl.h>
#include <inttypes.h>
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#ifdef __APPLE__
#include <sys/disk.h>
#import <IOKit/pwr_mgt/IOPMLib.h>
#endif
#ifdef __linux__
#include <linux/fs.h>
#endif
#define EXIT_CALL_FAILED 2
#define MAX(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})
#define MIN(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a < _b ? _a : _b; \
})
typedef struct {
uint64_t total;
const char *name;
struct timeval start, last_display;
} PROGRESS_CTX;
void PROGRESS_Init(PROGRESS_CTX *ctx, uint64_t total, const char *name) {
ctx->total = total;
ctx->name = name;
gettimeofday(&ctx->start, NULL);
ctx->last_display = (struct timeval){0};
}
void _PROGRESS_Print(PROGRESS_CTX *ctx, struct timeval *now, uint64_t current) {
double complete = (double)current / (double)ctx->total;
printf("\r%s %.1f%% (%" PRIu64 " of %" PRIu64 ")", ctx->name,
complete * 100.0, current, ctx->total);
uint64_t elapsed = now->tv_sec - ctx->start.tv_sec;
printf(" %02" PRIu64 ":%02" PRIu64 ":%02" PRIu64 "", elapsed / 3600,
(elapsed / 60) % 60, elapsed % 60);
if (current != ctx->total && elapsed > 10 && complete > 0.001) {
uint64_t eta = (1 / complete - 1) * elapsed;
printf(" (ETA: %02" PRIu64 ":%02" PRIu64 ":%02" PRIu64 ")", eta / 3600,
(eta / 60) % 60, eta % 60);
}
printf("\e[K");
fflush(stdout);
}
void PROGRESS_Update(PROGRESS_CTX *ctx, uint64_t current) {
struct timeval now, delta;
gettimeofday(&now, NULL);
timersub(&now, &ctx->last_display, &delta);
if (delta.tv_sec < 1)
return;
ctx->last_display = now;
_PROGRESS_Print(ctx, &now, current);
}
void PROGRESS_Finish(PROGRESS_CTX *ctx) {
struct timeval now;
gettimeofday(&now, NULL);
_PROGRESS_Print(ctx, &now, ctx->total);
printf("\n");
}
void SHA1_Finish(unsigned char *digest, SHA_CTX *ctx, const char *name) {
SHA1_Final(digest, ctx);
for (size_t i = 0; i < SHA_DIGEST_LENGTH; i++) {
printf("%02x", digest[i]);
}
printf(" <= SHA-1 of %s data\n", name);
}
int main(int argc, const char *argv[]) {
if (argc != 2) {
fprintf(stderr, "stressdrive v1.2\n");
#ifdef __APPLE__
fprintf(stderr, "Usage: sudo %s /dev/rdiskN\n", argv[0]);
#else
fprintf(stderr, "Usage: sudo %s /dev/sdX\n", argv[0]);
#endif
exit(EXIT_FAILURE);
}
const char *drivePath = argv[1];
int fd = open(drivePath, O_RDWR);
if (fd == -1) {
perror("open() failed");
exit(EXIT_CALL_FAILED);
}
uint32_t blockSize;
#ifdef DKIOCGETBLOCKSIZE
if (ioctl(fd, DKIOCGETBLOCKSIZE, &blockSize) == -1) {
#else
if (ioctl(fd, BLKSSZGET, &blockSize) == -1) {
#endif
perror("getting block size using ioctl failed");
exit(EXIT_CALL_FAILED);
}
printf("disk block size: %u\n", blockSize);
uint64_t blockCount;
#ifdef DKIOCGETBLOCKCOUNT
if (ioctl(fd, DKIOCGETBLOCKCOUNT, &blockCount) == -1) {
#else
if (ioctl(fd, BLKGETSIZE64, &blockCount) != -1) {
blockCount /= blockSize;
} else {
#endif
perror("getting block count using ioctl failed");
exit(EXIT_CALL_FAILED);
}
printf("disk block count: %" PRIu64 "\n", blockCount);
uint32_t bufferSize = MAX(blockSize, 8 * 1024 * 1024);
printf("buffer size: %u\n", bufferSize);
uint8_t *buffer = malloc(bufferSize);
if (buffer == NULL) {
perror("malloc() failed");
exit(EXIT_CALL_FAILED);
}
uint16_t bufferBlocks = bufferSize / blockSize;
#ifdef __APPLE__
IOPMAssertionID noIdleAssertionID;
IOReturn noIdleAssertionCreated = IOPMAssertionCreateWithName(
kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn,
CFSTR("stressdrive running"), &noIdleAssertionID);
if (kIOReturnSuccess == noIdleAssertionCreated) {
printf("succesfully created no idle assertion\n");
} else {
printf("failed to create no idle assertion\n");
}
#endif
SHA_CTX shaContext;
PROGRESS_CTX progress;
int aesKeylength = 128;
unsigned char aesKey[aesKeylength / 8];
if (!RAND_bytes(aesKey, aesKeylength / 8)) {
fprintf(stderr, "RAND_bytes() failed\n");
exit(EXIT_CALL_FAILED);
}
unsigned char aesIv[AES_BLOCK_SIZE];
if (!RAND_bytes(aesIv, AES_BLOCK_SIZE)) {
fprintf(stderr, "RAND_bytes() failed\n");
exit(EXIT_CALL_FAILED);
}
EVP_CIPHER_CTX aes;
if (!EVP_EncryptInit(&aes, EVP_aes_128_cbc(), aesKey, aesIv)) {
fprintf(stderr, "EVP_EncryptInit() failed\n");
exit(EXIT_CALL_FAILED);
}
unsigned char *aesInput = malloc(bufferSize);
memset(aesInput, 0, bufferSize);
printf("writing random data to %s\n", drivePath);
SHA1_Init(&shaContext);
PROGRESS_Init(&progress, blockCount, "writing");
for (uint64_t blockIndex = 0; blockIndex < blockCount;
blockIndex += bufferBlocks) {
uint32_t size = MIN(bufferBlocks, blockCount - blockIndex) * blockSize;
int outSize;
if (!EVP_EncryptUpdate(&aes, buffer, &outSize, aesInput, size)) {
fprintf(stderr, "EVP_EncryptUpdate() failed\n");
exit(EXIT_CALL_FAILED);
}
if (outSize != size) {
fprintf(stderr,
"EVP_EncryptUpdate() returned %d instead of %u bytes\n",
outSize, size);
exit(EXIT_CALL_FAILED);
}
if (write(fd, buffer, size) != size) {
perror("write() failed");
exit(EXIT_CALL_FAILED);
}
SHA1_Update(&shaContext, buffer, size);
PROGRESS_Update(&progress, blockIndex);
}
PROGRESS_Finish(&progress);
uint8_t writtenShaDigest[SHA_DIGEST_LENGTH];
SHA1_Finish(writtenShaDigest, &shaContext, "written");
if (lseek(fd, 0LL, SEEK_SET) != 0LL) {
perror("lseek() failed");
exit(EXIT_CALL_FAILED);
}
printf("verifying written data\n");
SHA1_Init(&shaContext);
PROGRESS_Init(&progress, blockCount, "reading");
for (uint64_t blockIndex = 0; blockIndex < blockCount;
blockIndex += bufferBlocks) {
uint32_t size = MIN(bufferBlocks, blockCount - blockIndex) * blockSize;
if (read(fd, buffer, size) == -1) {
perror("read() failed");
exit(EXIT_CALL_FAILED);
}
SHA1_Update(&shaContext, buffer, size);
PROGRESS_Update(&progress, blockIndex);
}
PROGRESS_Finish(&progress);
uint8_t readShaDigest[SHA_DIGEST_LENGTH];
SHA1_Finish(readShaDigest, &shaContext, "read");
int exitCode;
if (bcmp(writtenShaDigest, readShaDigest, SHA_DIGEST_LENGTH) == 0) {
printf("SUCCESS\n");
exitCode = EXIT_SUCCESS;
} else {
printf("FAILURE\n");
exitCode = EXIT_FAILURE;
}
#ifdef __APPLE__
if (kIOReturnSuccess == noIdleAssertionCreated) {
if (kIOReturnSuccess == IOPMAssertionRelease(noIdleAssertionID)) {
printf("succesfully released no idle assertion\n");
} else {
printf("failed to release no idle assertion\n");
}
}
#endif
free(buffer);
close(fd);
return exitCode;
}