来源:chatGPT
#include <iostream>
#include <string>
#include <sys/mount.h>
bool mountTempfs(const std::string& mountPoint, const std::string& size) {
std::string options = "size=" + size;
if (mount("none", mountPoint.c_str(), "tmpfs", 0, options.c_str()) == 0) {
std::cout << "Tempfs mounted successfully at: " << mountPoint << std::endl;
return true;
} else {
std::cerr << "Failed to mount tempfs." << std::endl;
return false;
}
}
int main() {
std::string mountPoint = "/path/to/mount/point";
std::string diskSize = "1G"; // 设置磁盘大小,例如1GB
if (mountTempfs(mountPoint, diskSize)) {
// Tempfs mounted successfully, perform further operations if needed
} else {
// Failed to mount tempfs, handle error
}
return 0;
}
编译
g++ -o mount_example mount_example.cpp -lmount