master
cpp 133 lines 4.34 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 WslDistributionConfig.h
8
9 Abstract:
10
11 This file contains the WslDistributionConfig class implementation.
12
13 --*/
14
15 #include "WslDistributionConfig.h"
16 #include "configfile.h"
17 #include "util.h"
18
19 using wsl::linux::WslDistributionConfig;
20 using namespace wsl::linux;
21
22 WslDistributionConfig::WslDistributionConfig(const char* configFilePath)
23 {
24
25 std::vector<ConfigKey> keys = {
26 ConfigKey(c_ConfigAutoMountOption, AutoMount),
27 ConfigKey(c_ConfigAutoMountRoot, DrvFsPrefix),
28 ConfigKey("automount.options", DrvFsOptions),
29 ConfigKey(c_ConfigMountFsTabOption, MountFsTab),
30 ConfigKey(c_ConfigLinkOsLibsOption, LinkOsLibs),
31 ConfigKey("automount.cgroups", {{"v1", CGroupVersion::v1}, {"v2", CGroupVersion::v2}}, CGroup, nullptr),
32
33 ConfigKey("filesystem.umask", Umask),
34
35 ConfigKey(c_ConfigInteropAppendWindowsPathOption, InteropAppendWindowsPath),
36 ConfigKey(c_ConfigInteropEnabledOption, InteropEnabled),
37
38 ConfigKey(c_ConfigGenerateHostsOption, GenerateHosts),
39 ConfigKey(c_ConfigGenerateResolvConfOption, GenerateResolvConf),
40 ConfigKey("network.hostname", HostName),
41
42 ConfigKey(c_ConfigAutoUpdateTimezoneOption, AutoUpdateTimezone),
43
44 ConfigKey(c_ConfigPlan9EnabledOption, Plan9Enabled),
45 ConfigKey("fileServer.logFile", Plan9LogFile),
46 ConfigKey("fileServer.logLevel", Plan9LogLevel),
47 ConfigKey("fileServer.logTruncate", Plan9LogTruncate),
48
49 ConfigKey(c_ConfigGpuEnabledOption, GpuEnabled),
50 ConfigKey(c_ConfigAppendGpuLibPathOption, AppendGpuLibPath),
51
52 ConfigKey("user.default", DefaultUser),
53
54 ConfigKey(c_ConfigBootCommandOption, BootCommand),
55 ConfigKey(c_ConfigBootSystemdOption, BootInit),
56 ConfigKey("boot.initTimeout", BootInitTimeout),
57 ConfigKey(c_ConfigBootProtectBinfmtOption, BootProtectBinfmt),
58
59 ConfigKey(c_ConfigEnableGuiAppsOption, GuiAppsEnabled),
60 };
61
62 //
63 // If the config file does not exist, then ParseConfigFile sets all the configuration
64 // values to their defaults.
65 //
66
67 wil::unique_file File{fopen(configFilePath, "r")};
68 ParseConfigFile(keys, File.get(), CFG_SKIP_UNKNOWN_VALUES, STRING_TO_WSTRING(CONFIG_FILE));
69
70 //
71 // Ensure the DrvFs prefix is well-formed (not empty and ends with a path separator).
72 //
73
74 std::string Prefix;
75 if (!DrvFsPrefix.empty())
76 {
77 Prefix = DrvFsPrefix;
78 }
79
80 if (Prefix.empty() || Prefix.back() != '/')
81 {
82 Prefix += '/';
83 DrvFsPrefix = Prefix;
84 }
85
86 //
87 // Using boot.systemd is only supported on WSL2.
88 //
89
90 BootInit &= UtilIsUtilityVm();
91 if (BootInit && (access(INIT_PATH, X_OK) < 0))
92 {
93 LOG_WARNING("access({}) failed {} - {} disabled", INIT_PATH, errno, c_ConfigBootSystemdOption);
94 BootInit = false;
95 }
96
97 //
98 // Apply safe mode overrides.
99 //
100
101 const char* Value = getenv(LX_WSL2_SAFE_MODE);
102 if (Value && (strcmp(Value, "1") == 0))
103 {
104 auto DisableBoolOption = [&](const char* Option, bool& ConfigValue) {
105 if (ConfigValue)
106 {
107 LOG_WARNING("{} - {} disabled", WSL_SAFE_MODE_WARNING, Option);
108 ConfigValue = false;
109 }
110 };
111
112 bool BootCommandEnabled = (BootCommand.has_value());
113 for (const auto& ConfigOption : std::vector<std::pair<const char*, bool&>>{
114 {c_ConfigAutoMountOption, AutoMount},
115 {c_ConfigLinkOsLibsOption, LinkOsLibs},
116 {c_ConfigMountFsTabOption, MountFsTab},
117 {c_ConfigBootCommandOption, BootCommandEnabled},
118 {c_ConfigBootSystemdOption, BootInit},
119 {c_ConfigGenerateHostsOption, GenerateHosts},
120 {c_ConfigGenerateResolvConfOption, GenerateResolvConf},
121 {c_ConfigPlan9EnabledOption, Plan9Enabled},
122 {c_ConfigAppendGpuLibPathOption, AppendGpuLibPath},
123 {c_ConfigGpuEnabledOption, GpuEnabled},
124 {c_ConfigInteropAppendWindowsPathOption, InteropAppendWindowsPath},
125 {c_ConfigInteropEnabledOption, InteropEnabled},
126 {c_ConfigAutoUpdateTimezoneOption, AutoUpdateTimezone}})
127 {
128 DisableBoolOption(ConfigOption.first, ConfigOption.second);
129 }
130
131 BootCommand = {};
132 }
133 }