2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
22 * @brief Declaration of the class for managing one zone
26 #ifndef SERVER_ZONE_HPP
27 #define SERVER_ZONE_HPP
29 #include "zone-config.hpp"
30 #include "zone-provision.hpp"
32 #include "lxc/zone.hpp"
39 #include <boost/regex.hpp>
45 enum class SchedulerLevel {
53 typedef netdev::Attrs NetdevAttrs;
57 * @param zoneId zone id
58 * @param zonesPath directory where zones are defined (configs, rootfs etc)
59 * @param zoneTemplatePath path for zones config template
60 * @param dbPath path to dynamic config db file
61 * @param zoneTemplateDir directory where templates are stored
62 * @param baseRunMountPointPath base directory for run mount point
64 Zone(const std::string& zoneId,
65 const std::string& zonesPath,
66 const std::string& zoneTemplatePath,
67 const std::string& dbPath,
68 const std::string& zoneTemplateDir,
69 const std::string& baseRunMountPointPath);
70 Zone(const Zone&) = delete;
71 Zone& operator=(const Zone&) = delete;
74 typedef std::function<void(bool succeeded)> StartAsyncResultCallback;
79 const std::string& getId() const;
82 * Get the zone privilege
84 int getPrivilege() const;
87 * Restore zone to the previous state
92 * Boot the zone to the background.
97 * Try to shutdown the zone, if failed, destroy it.
98 * @param saveState save zone's state
100 void stop(bool saveState);
103 * Activate this zone's VT
105 * @return Was activation successful?
110 * Setup this zone to be put in the foreground.
111 * I.e. set appropriate scheduler level.
116 * Setup this zone to be put in the background.
117 * I.e. set appropriate scheduler level.
122 * Set if zone should be detached on exit.
124 void setDetachOnExit();
127 * Set if zone should be destroyed on exit.
129 void setDestroyOnExit();
132 * @return Is the zone running?
137 * Check if the zone is stopped. It's NOT equivalent to !isRunning,
138 * because it checks different internal zone states. There are other states,
139 * (e.g. paused) when the zone isn't running nor stopped.
141 * @return Is the zone stopped?
146 * Suspends an active zone, the process is frozen
147 * without further access to CPU resources and I/O,
148 * but the memory used by the zone
149 * at the hypervisor level will stay allocated
159 * @return Is the zone in a paused state?
164 * @return Is switching to default zone after timeout allowed?
166 bool isSwitchToDefaultAfterTimeoutAllowed() const;
174 * Create file inside zone, return its fd
176 * @param path Path where the file should be created
177 * @param flags Flags used when opening the file. See common/lxc/zone.hpp for more info.
178 * @param mode Permissions with which file is created
180 * @return Created files fd
182 int createFile(const std::string& path, const std::int32_t flags, const std::int32_t mode);
185 * Declare file, directory or pipe that will be created while zone startup
187 std::string declareFile(const int32_t& type,
188 const std::string& path,
189 const int32_t& flags,
190 const int32_t& mode);
192 * Declare mount that will be created while zone startup
194 std::string declareMount(const std::string& source,
195 const std::string& target,
196 const std::string& type,
197 const int64_t& flags,
198 const std::string& data);
200 * Declare link that will be created while zone startup
202 std::string declareLink(const std::string& source,
203 const std::string& target);
206 * Gets all declarations
208 std::vector<std::string> getDeclarations() const;
213 void removeDeclaration(const std::string& declarationId);
218 std::string getRootPath() const;
221 * Create veth network device
223 void createNetdevVeth(const std::string& zoneDev,
224 const std::string& hostDev);
227 * Create macvlan network device
229 void createNetdevMacvlan(const std::string& zoneDev,
230 const std::string& hostDev,
231 const uint32_t& mode);
234 * Move network device to zone
236 void moveNetdev(const std::string& devId);
239 * Destroy network device in zone
241 void destroyNetdev(const std::string& devId);
244 * Set network device attributes
246 void setNetdevAttrs(const std::string& netdev, const NetdevAttrs& attrs);
249 * Get network device attributes
251 NetdevAttrs getNetdevAttrs(const std::string& netdev);
254 * Get network device list
256 std::vector<std::string> getNetdevList();
259 * Remove ipv4/ipv6 address from network device
261 void deleteNetdevIpAddress(const std::string& netdev, const std::string& ip);
264 * Sets the zones scheduler CFS quota.
266 void setSchedulerLevel(SchedulerLevel sched);
269 * @return Scheduler CFS quota,
270 * TODO: this function is only for UNIT TESTS
272 std::int64_t getSchedulerQuota();
276 ZoneDynamicConfig mDynamicConfig;
277 std::unique_ptr<ZoneProvision> mProvision;
278 mutable std::recursive_mutex mReconnectMutex;
279 std::string mRunMountPoint;
280 std::string mRootPath;
283 const std::string mId;
287 void onNameLostCallback();
288 void saveDynamicConfig();
289 void updateRequestedState(const std::string& state);
290 void setSchedulerParams(std::uint64_t cpuShares, std::uint64_t vcpuPeriod, std::int64_t vcpuQuota);
296 #endif // SERVER_ZONE_HPP