mkswap/swapon C语言源码实现
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <linux/hdreg.h>
#include <sys/swap.h>
#include <stdint.h>
#include <sys/stat.h>
#define SWAP_HEADER_MAGIC "SWAPSPACE2"
#define PAGE_SIZE 4096
#define HEADER_SIZE 0x1000
#define SWAP_MAGIC_OFFSET 1000
#define MAX_LABEL_LEN 15
#define UUID_SIZE 16
void xgetrandom(void *buf, size_t size) {
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
perror("open /dev/urandom");
exit(EXIT_FAILURE);
}
ssize_t bytes_read = read(fd, buf, size);
if (bytes_read != (ssize_t)size) {
perror("read /dev/urandom");
close(fd);
exit(EXIT_FAILURE);
}
close(fd);
}
void create_uuid(char *uuid) {
xgetrandom(uuid, 16);
uuid[6] = (uuid[6] & 0x0F) | 0x40;
uuid[8] = (uuid[8] & 0x3F) | 0x80;
}
char *show_uuid(const char *uuid) {
static char formatted_uuid[37];
snprintf(formatted_uuid, sizeof(formatted_uuid),
"%.8s-%.4s-%.4s-%.4s-%.12s",
uuid, uuid + 8, uuid + 12, uuid + 16, uuid + 20);
return formatted_uuid;
}
void make_swap(const char *swapfile, const char *label) {
int fd = open(swapfile, O_RDWR);
if (fd < 0) {
perror("open");
exit(EXIT_FAILURE);
}
int pagesize = sysconf(_SC_PAGE_SIZE);
off_t len = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
unsigned int pages = (len / pagesize) - 1;
unsigned int swap[129] = {0};
char uuid[UUID_SIZE + 1] = {0};
char label_pos[MAX_LABEL_LEN + 1] = {0};
swap[0] = 1;
swap[1] = pages;
create_uuid(uuid);
if (label) strncpy(label_pos, label, MAX_LABEL_LEN);
lseek(fd, 1024, SEEK_SET);
write(fd, swap, sizeof(swap));
lseek(fd, pagesize - 10, SEEK_SET);
write(fd, SWAP_HEADER_MAGIC, 10);
fsync(fd);
close(fd);
printf("Swapspace size: %luk, UUID=%s\n",
pages * (unsigned long)(pagesize / 1024),
show_uuid(uuid));
}
void dump_swap_header(const char *device) {
int fd = open(device, O_RDONLY);
if (fd < 0) {
perror("Failed to open device");
return;
}
unsigned char buffer[HEADER_SIZE];
if (lseek(fd, SWAP_MAGIC_OFFSET, SEEK_SET) < 0) {
perror("Failed to seek");
close(fd);
return;
}
if (read(fd, buffer, sizeof(buffer)) != sizeof(buffer)) {
perror("Failed to read swap header");
close(fd);
return;
}
for (int i = 0; i < HEADER_SIZE; i += 16) {
printf("%08x ", i);
for (int j = 0; j < 16; j++) {
if (i + j < HEADER_SIZE)
printf("%02x ", buffer[i + j]);
else
printf(" ");
}
printf(" |");
for (int j = 0; j < 16; j++) {
if (i + j < HEADER_SIZE)
printf("%c", buffer[i + j] >= 32 && buffer[i + j] <= 126 ? buffer[i + j] : '.');
}
printf("|\n");
}
close(fd);
}
int enable_swap(const char *device) {
if (swapon(device, 0) != 0) {
perror("Failed to enable swap");
return -1;
}
return 0;
}
int main(int argc, char *argv[]) {
const char *device = "/dev/block/mmcblk0p35";
if (argc < 2) {
fprintf(stderr, "Usage: %s <make|on> [device_path]\n", argv[0]);
return EXIT_FAILURE;
}
if (strcmp(argv[1], "make") == 0) {
make_swap(device,NULL);
} else if (strcmp(argv[1], "on") == 0) {
enable_swap(device);
} else if(strcmp(argv[1], "read")==0){
dump_swap_header(device);
} else {
fprintf(stderr, "Unknown argv[1]: %s\n", argv[1]);
return EXIT_FAILURE;
}
}
修改device变成你需要的路径即可
可以绕开一些selinux的限制,执行制作swap分区,有用的话点个赞吧!