| 851 | } |
| 852 | |
| 853 | Status FsManager::CreateInitialFileSystemLayout(optional<string> uuid, |
| 854 | optional<string> tenant_name, |
| 855 | optional<string> tenant_id, |
| 856 | optional<string> encryption_key, |
| 857 | optional<string> encryption_key_iv, |
| 858 | optional<string> encryption_key_version) { |
| 859 | CHECK(!opts_.read_only); |
| 860 | |
| 861 | RETURN_NOT_OK(Init()); |
| 862 | |
| 863 | // In the event of failure, delete everything we created. |
| 864 | vector<string> created_dirs; |
| 865 | vector<string> created_files; |
| 866 | auto deleter = MakeScopedCleanup([&]() { |
| 867 | // Delete files first so that the directories will be empty when deleted. |
| 868 | for (const auto& f : created_files) { |
| 869 | WARN_NOT_OK(GetEnv()->DeleteFile(f), "Could not delete file " + f); |
| 870 | } |
| 871 | // Delete directories in reverse order since parent directories will have |
| 872 | // been added before child directories. |
| 873 | for (auto it = created_dirs.rbegin(); it != created_dirs.rend(); it++) { |
| 874 | WARN_NOT_OK(GetEnv()->DeleteDir(*it), "Could not delete dir " + *it); |
| 875 | } |
| 876 | }); |
| 877 | |
| 878 | // Create the filesystem roots. |
| 879 | // |
| 880 | // Files/directories created will NOT be synchronized to disk. |
| 881 | InstanceMetadataPB metadata; |
| 882 | string tid = tenant_id ? *tenant_id : fs::kDefaultTenantID; |
| 883 | RETURN_NOT_OK_PREPEND(CreateInstanceMetadata(std::move(uuid), |
| 884 | std::move(tenant_name), |
| 885 | std::move(tenant_id), |
| 886 | std::move(encryption_key), |
| 887 | std::move(encryption_key_iv), |
| 888 | std::move(encryption_key_version), |
| 889 | &metadata), |
| 890 | "unable to create instance metadata"); |
| 891 | RETURN_NOT_OK_PREPEND(FsManager::CreateFileSystemRoots( |
| 892 | canonicalized_all_fs_roots_, metadata, &created_dirs, &created_files), |
| 893 | "unable to create file system roots"); |
| 894 | |
| 895 | // Create ancillary directories. |
| 896 | vector<string> ancillary_dirs = { GetWalsRootDir(), |
| 897 | GetTabletMetadataDir(), |
| 898 | GetConsensusMetadataDir() }; |
| 899 | for (const string& dir : ancillary_dirs) { |
| 900 | bool created; |
| 901 | RETURN_NOT_OK_PREPEND(env_util::CreateDirIfMissing(GetEnv(), dir, &created), |
| 902 | Substitute("Unable to create directory $0", dir)); |
| 903 | if (created) { |
| 904 | created_dirs.emplace_back(dir); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | // Create the directory manager. |
| 909 | // |
| 910 | // All files/directories created will be synchronized to disk. |