-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: properly handling swapfile during persist file handling (#275)
This PR implements the proper way to create swapfile. For a quick workaround fix, a hook is added in persist file handling to handle swapfile creating as special case.
- Loading branch information
1 parent
1aa06ed
commit dfd1237
Showing
3 changed files
with
109 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright 2022 TIER IV, INC. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from __future__ import annotations | ||
from pathlib import Path | ||
from subprocess import check_call | ||
|
||
|
||
def create_swapfile( | ||
swapfile_fpath: str | Path, size_in_MiB: int, *, timeout=900 | ||
) -> Path: | ||
"""Create swapfile at <swapfile_fpath> with <size_in_MiB>MiB. | ||
Reference: https://wiki.archlinux.org/title/swap#Swap_file_creation | ||
Args: | ||
swapfile_fpath(StrOrPath): the path to place the created swapfile. | ||
size_in_MiB(int): the size of to-be-created swapfile. | ||
timeout: timeout of swapfile creating, default is 15mins. | ||
Returns: | ||
The Path object to the newly created swapfile. | ||
Raises: | ||
ValueError on file already exists at <swapfile_fpath>, SubprocessCallFailed | ||
on failed swapfile creation. | ||
""" | ||
swapfile_fpath = Path(swapfile_fpath) | ||
if swapfile_fpath.exists(): | ||
raise ValueError(f"{swapfile_fpath=} exists, skip") | ||
|
||
# create a new file with <size_in_MiB>MiB size | ||
# executes: | ||
# dd if=/dev/zero of=/swapfile bs=1M count=8k | ||
# chmod 0600 /swapfile | ||
check_call( | ||
[ | ||
"dd", | ||
"if=/dev/zero", | ||
f"of={str(swapfile_fpath)}", | ||
"bs=1M", | ||
f"count={size_in_MiB}", | ||
], | ||
timeout=timeout, | ||
) | ||
swapfile_fpath.chmod(0o600) | ||
|
||
# prepare the created file as swapfile | ||
# executes: | ||
# mkswap /swapfile | ||
check_call(["mkswap", str(swapfile_fpath)], timeout=timeout) | ||
|
||
return swapfile_fpath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters