基本上,我正在尝试创建自己的工具来快速格式化 ext4 分区。
据我了解,其背后的想法是用空ext2_inode
结构查找和覆盖现有的 inode。我认为我可以通过简单地计算和之间的差异来做到这一点,sb.s_inodes_count
所以sb.s_free_inodes_count
我会知道我必须覆盖多少个 inode。
但实际上它不起作用(毕竟只是无法安装),我很好奇为什么。如果是这样,那么我如何找到正确的 inode 位置(如果我发现它不正确)以及如何正确编写空结构?
#include <iostream>
#include <ext2fs/ext2_fs.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include "Disk.h"
using namespace std;
Disk::Disk(string path){
try{
this->fd = open(path.c_str(), O_RDWR);
if(this->fd == -1) throw new CantOpenDevice;
} catch (CantOpenDevice& openError){
logger->error(openError.what());
}
}
ext2_super_block Disk::readSuperBlock(){
lseek(this->fd, 1024, SEEK_SET); // 1024 - Group 0 Padding
read(this->fd, &this->sb, sizeof(ext2_super_block));
this->logger->debug("Succesfully read superblock.");
return this->sb;
}
ext4_group_desc Disk::readFirstBGD(){
int block_size_offset = this->getBlockSizeOffset();
lseek(this->fd, block_size_offset, SEEK_SET);
read(this->fd, &this->bg, sizeof(ext4_group_desc));
this->logger->debug("Succesfully read a group descriptor.");
return this->bg;
}
void Disk::writeEmptyInodes(){
ext2_inode inode = {0};
int block_size_offset = this->getBlockSizeOffset();
int inodes_to_clean = this->sb.s_inodes_count - this->sb.s_free_inodes_count;
for(int i=1; i < inodes_to_clean; i++){
write(this->fd, &inode, sizeof(inode));
}
}
int Disk::getBlockSizeOffset(){
return 1024 << this->sb.s_log_block_size;
}
Disk::~Disk(){
close(this->fd);
delete logger;
}