Skip to content

Commit 67b712b

Browse files
authored
Merge pull request #213 from liulanzheng/main
add mkfs for extfs and overlaybd-apply
2 parents 4b19a8d + 048a86a commit 67b712b

File tree

7 files changed

+184
-5
lines changed

7 files changed

+184
-5
lines changed

.github/workflows/cmake.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,26 @@ jobs:
6767
sudo apt-get install -y lsscsi
6868
dev=`lsscsi | grep TCMU | awk '{print $7}'`
6969
echo $dev
70-
sudo mkdir obd_mp
70+
mkdir obd_mp
7171
sudo mount -o ro $dev obd_mp
7272
ls obd_mp
7373
sudo umount obd_mp
7474
ls -l /sys/kernel/config/target/loopback/naa.123456789abcdef/tpgt_1/lun/lun_0/vol1
7575
sudo unlink /sys/kernel/config/target/loopback/naa.123456789abcdef/tpgt_1/lun/lun_0/vol1
76+
wget https://overlaybd.blob.core.windows.net/overlaybd/e2etest.tar.gz
77+
gzip -k -d e2etest.tar.gz
78+
mkdir test_data
79+
tar -zxvf e2etest.tar.gz -C test_data/
80+
truncate -s 2g img1
81+
truncate -s 2g img2
82+
/opt/overlaybd/bin/overlaybd-apply --mkfs --raw e2etest.tar img1
83+
sleep 2
84+
/opt/overlaybd/bin/overlaybd-apply --mkfs --raw e2etest.tar img2
85+
diff img1 img2
86+
sudo mount -o ro img1 obd_mp
87+
ls obd_mp
88+
sudo diff -r --exclude "lost+found" test_data obd_mp
89+
sudo umount obd_mp
7690
- name: E2E Test FastOCI
7791
working-directory: ${{github.workspace}}/build
7892
shell: bash

CMake/Findext2fs.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set(FETCHCONTENT_QUIET false)
44
FetchContent_Declare(
55
e2fsprogs
66
GIT_REPOSITORY https://github.com/data-accelerator/e2fsprogs.git
7-
GIT_TAG b4d19a0ec5cb535f13009d910833c62aa70d69a5
7+
GIT_TAG 5c1a47716d56c2f543058c1499fdd7742e3dd2b1
88
)
99

1010
FetchContent_MakeAvailable(e2fsprogs)

src/overlaybd/extfs/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
file(GLOB SOURCE_TAR "*.cpp")
2-
add_library(extfs_lib STATIC ${SOURCE_TAR})
1+
file(GLOB SOURCE_EXTFS "*.cpp")
2+
add_library(extfs_lib STATIC ${SOURCE_EXTFS})
33

44
if(NOT ORIGIN_EXT2FS)
55
find_package(ext2fs REQUIRED)

src/overlaybd/extfs/extfs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ class IOManager {
2828

2929
IOManager *new_io_manager(photon::fs::IFile *file);
3030
photon::fs::IFileSystem *new_extfs(photon::fs::IFile *file, bool buffer = true);
31+
32+
// make extfs on an prezeroed IFile,
33+
// should be truncated to specified size in advance
34+
int make_extfs(photon::fs::IFile *file);

src/overlaybd/extfs/mkfs.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
Copyright The Overlaybd Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
#include "extfs.h"
17+
#include <limits.h>
18+
#include <ext2fs/ext2_fs.h>
19+
#include <ext2fs/ext2fs.h>
20+
#include <photon/photon.h>
21+
#include <photon/fs/filesystem.h>
22+
#include <photon/fs/localfs.h>
23+
#include <photon/common/alog.h>
24+
#include <photon/common/uuid4.h>
25+
#include <string>
26+
#include <fcntl.h>
27+
28+
int mkdir_lost_found(ext2_filsys fs) {
29+
std::string name = "lost+found";
30+
fs->umask = 077;
31+
ext2_ino_t ret = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name.c_str());
32+
if (ret) {
33+
LOG_ERRNO_RETURN(0, -1, "error mkdir for /lost+found ", VALUE(ret));
34+
}
35+
ext2_ino_t ino;
36+
ret = ext2fs_lookup(fs, EXT2_ROOT_INO, name.c_str(), name.length(), 0, &ino);
37+
if (ret) {
38+
LOG_ERRNO_RETURN(0, -1, "error looking up for /lost+found ", VALUE(ret));
39+
}
40+
// expand twice ensure at least 2 blocks
41+
for (int i = 0; i < 2; i++) {
42+
ret = ext2fs_expand_dir(fs, ino);
43+
if (ret) {
44+
LOG_ERRNO_RETURN(0, -1, "error expanding dir for /lost+found ", VALUE(ret));
45+
}
46+
}
47+
return 0;
48+
}
49+
50+
int do_mkfs(io_manager manager, size_t size) {
51+
int blocksize = 4096;
52+
blk64_t blocks_count = size / blocksize;
53+
int inode_ratio = 16384;
54+
int inode_size = 256;
55+
double reserved_ratio = 0.05;
56+
std::string uuid = "bdf7bb2e-c231-43ce-87c2-overlaybddev";
57+
58+
struct ext2_super_block fs_param;
59+
memset(&fs_param, 0, sizeof(struct ext2_super_block));
60+
fs_param.s_rev_level = 1;
61+
62+
ext2fs_set_feature_64bit(&fs_param);
63+
ext2fs_set_feature_sparse_super(&fs_param);
64+
ext2fs_set_feature_sparse_super2(&fs_param);
65+
ext2fs_set_feature_filetype(&fs_param);
66+
ext2fs_set_feature_resize_inode(&fs_param);
67+
ext2fs_set_feature_dir_index(&fs_param);
68+
ext2fs_set_feature_xattr(&fs_param);
69+
ext2fs_set_feature_dir_nlink(&fs_param);
70+
ext2fs_set_feature_large_file(&fs_param);
71+
ext2fs_set_feature_huge_file(&fs_param);
72+
ext2fs_set_feature_flex_bg(&fs_param);
73+
ext2fs_set_feature_extents(&fs_param);
74+
ext2fs_set_feature_extra_isize(&fs_param);
75+
76+
fs_param.s_log_cluster_size = fs_param.s_log_block_size = 2;
77+
fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
78+
fs_param.s_log_groups_per_flex = 0;
79+
fs_param.s_inode_size = inode_size;
80+
81+
ext2fs_blocks_count_set(&fs_param, blocks_count);
82+
unsigned long long n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
83+
fs_param.s_inodes_count = (n > UINT_MAX) ? UINT_MAX : n;
84+
85+
ext2fs_r_blocks_count_set(&fs_param, reserved_ratio * ext2fs_blocks_count(&fs_param));
86+
fs_param.s_backup_bgs[0] = 1;
87+
fs_param.s_backup_bgs[1] = ~0;
88+
89+
ext2_filsys fs;
90+
// init superblock
91+
errcode_t ret = ext2fs_initialize("virtual-dev", 0, &fs_param, manager, &fs);
92+
if (ret) {
93+
LOG_ERRNO_RETURN(0, -1, "error while setting up superblock ", VALUE(ret));
94+
}
95+
96+
uuid4_parse((char*)uuid.c_str(), (char*)(fs->super->s_uuid));
97+
uuid4_parse((char*)uuid.c_str(), (char*)(fs_param.s_hash_seed));
98+
fs->super->s_kbytes_written = 1;
99+
fs->super->s_def_hash_version = EXT2_HASH_HALF_MD4;
100+
fs->super->s_max_mnt_count = -1;
101+
fs->stride = fs->super->s_raid_stride;
102+
// alloc tables
103+
ret = ext2fs_allocate_tables(fs);
104+
if (ret) {
105+
LOG_ERRNO_RETURN(0, -1, "error while allocating tables ", VALUE(ret));
106+
}
107+
// create root dir
108+
ret = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
109+
if (ret) {
110+
LOG_ERRNO_RETURN(0, -1, "error make root dir ", VALUE(ret));
111+
}
112+
// mkdir for lost+found
113+
ret = mkdir_lost_found(fs);
114+
if (ret) {
115+
return ret;
116+
}
117+
// reserve inodes
118+
for (ext2_ino_t i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
119+
ext2fs_inode_alloc_stats2(fs, i, +1, 0);
120+
ext2fs_mark_ib_dirty(fs);
121+
// create resize inode
122+
ret = ext2fs_create_resize_inode(fs);
123+
if (ret) {
124+
LOG_ERRNO_RETURN(0, -1, "error creating resize inode ", VALUE(ret));
125+
}
126+
127+
ret = ext2fs_close_free(&fs);
128+
if (ret) {
129+
LOG_ERRNO_RETURN(0, -1, "error closing fs ", VALUE(ret));
130+
}
131+
return 0;
132+
}
133+
134+
int make_extfs(photon::fs::IFile *file) {
135+
struct stat st;
136+
auto ret = file->fstat(&st);
137+
if (ret) return ret;
138+
auto manager = new_io_manager(file);
139+
DEFER(delete manager);
140+
ret = do_mkfs(manager->get_io_manager(), st.st_size);
141+
return ret;
142+
}

src/tools/overlaybd-apply.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ int main(int argc, char **argv) {
5454
string commit_msg;
5555
string parent_uuid;
5656
std::string image_config_path, input_path, gz_index_path, config_path;
57-
bool raw = false, verbose = false;
57+
bool raw = false, mkfs = false, verbose = false;
5858

5959
CLI::App app{"this is overlaybd-apply, apply OCIv1 tar layer to overlaybd format"};
6060
app.add_flag("--raw", raw, "apply to raw image")->default_val(false);
61+
app.add_flag("--mkfs", mkfs, "mkfs before apply")->default_val(false);
6162
app.add_flag("--verbose", verbose, "output debug info")->default_val(false);
6263
app.add_option("--service_config_path", config_path, "overlaybd image service config path")->type_name("FILEPATH")->check(CLI::ExistingFile);
6364
app.add_option("--gz_index_path", gz_index_path, "build gzip index if layer is gzip, only used with fastoci")->type_name("FILEPATH");
@@ -89,6 +90,13 @@ int main(int argc, char **argv) {
8990
fprintf(stderr, "failed to create image file\n");
9091
exit(-1);
9192
}
93+
94+
if (mkfs) {
95+
if (make_extfs(imgfile) < 0) {
96+
fprintf(stderr, "mkfs failed, %s\n", strerror(errno));
97+
exit(-1);
98+
}
99+
}
92100
// for now, buffer_file can't be used with fastoci
93101
auto extfs = new_extfs(imgfile, gz_index_path == "");
94102
if (!extfs) {

src/tools/overlaybd-create.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <sys/stat.h>
2525
#include <fcntl.h>
2626
#include "../overlaybd/lsmt/file.h"
27+
#include "../overlaybd/extfs/extfs.h"
2728
#include <photon/common/alog.h>
2829
#include <photon/fs/localfs.h>
2930
#include <photon/common/uuid.h>
@@ -50,11 +51,13 @@ int main(int argc, char **argv) {
5051
bool sparse = false;
5152
std::string data_file_path, index_file_path, warp_index_path;
5253
bool build_fastoci = false;
54+
bool mkfs = false;
5355

5456
CLI::App app{"this is overlaybd-create"};
5557
app.add_option("-u", parent_uuid, "parent uuid");
5658
app.add_flag("-s", sparse, "create sparse RW layer")->default_val(false);
5759
app.add_flag("--fastoci", build_fastoci, "commit using fastoci format")->default_val(false);
60+
app.add_flag("--mkfs", mkfs, "mkfs after create")->default_val(false);
5861
app.add_option("data_file", data_file_path, "data file path")->type_name("FILEPATH")->required();
5962
app.add_option("index_file", index_file_path, "index file path")->type_name("FILEPATH")->required();
6063
app.add_option("vsize", vsize, "virtual size(GB)")->type_name("INT")->check(CLI::PositiveNumber)->required();
@@ -86,6 +89,14 @@ int main(int argc, char **argv) {
8689
fprintf(stderr, "failed to create lsmt file object, possibly I/O error!\n");
8790
exit(-1);
8891
}
92+
93+
if (mkfs) {
94+
if (make_extfs(file) < 0) {
95+
fprintf(stderr, "mkfs failed, %s\n", strerror(errno));
96+
exit(-1);
97+
}
98+
}
99+
89100
delete file;
90101
delete fdata;
91102
delete findex;

0 commit comments

Comments
 (0)