better stow script
This commit is contained in:
+45
-17
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import shutil
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -65,6 +66,12 @@ PKGS = {
|
|||||||
"niri": NIRI_PKGS,
|
"niri": NIRI_PKGS,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SESSION_NAME = {
|
||||||
|
"hyprland": "Hyprland",
|
||||||
|
"niri": "niri",
|
||||||
|
"default": "default",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
PKGS_PATH = Path(__file__).resolve().parent.resolve() / "config"
|
PKGS_PATH = Path(__file__).resolve().parent.resolve() / "config"
|
||||||
DEST_PATH = Path.home().expanduser()
|
DEST_PATH = Path.home().expanduser()
|
||||||
@@ -78,12 +85,27 @@ def _log(level: str, message: str):
|
|||||||
print(f"{color}[{level}] {message}{reset}")
|
print(f"{color}[{level}] {message}{reset}")
|
||||||
|
|
||||||
|
|
||||||
|
def check_deps() -> bool:
|
||||||
|
required_deps = ["stow"]
|
||||||
|
for dep in required_deps:
|
||||||
|
if not shutil.which(dep):
|
||||||
|
_log("ERROR", f"Required dependency '{dep}' is not installed.")
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def stow(pkg: str):
|
def stow(pkg: str):
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
["stow", "-v", "-d", str(PKGS_PATH), "-t", str(DEST_PATH), pkg], check=True
|
["stow", "-v", "-d", str(PKGS_PATH), "-t", str(DEST_PATH), pkg], check=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def unstow(pkg: str):
|
||||||
|
subprocess.run(
|
||||||
|
["stow", "-v", "-d", str(PKGS_PATH), "-t", str(DEST_PATH), "-D", pkg], check=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def switch(session: str):
|
def switch(session: str):
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
[str(Path("~/.local/scripts/config-switch").expanduser()), session], check=True
|
[str(Path("~/.local/scripts/config-switch").expanduser()), session], check=True
|
||||||
@@ -91,43 +113,49 @@ def switch(session: str):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
if not check_deps():
|
||||||
|
exit(1)
|
||||||
|
|
||||||
parser = ArgumentParser(
|
parser = ArgumentParser(
|
||||||
description="Stow configuration packages in this repository."
|
description="Stow configuration packages in this repository."
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"package", choices=PKGS.keys(), help="The configuration package to stow."
|
"package", choices=PKGS.keys(), help="The configuration package group to stow."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--unstow",
|
||||||
|
action="store_true",
|
||||||
|
help="Unstow the specified package group instead of stowing it.",
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
selected_pkgs = PKGS[args.package]
|
selected_pkgs = PKGS[args.package]
|
||||||
|
is_unstow = args.unstow
|
||||||
|
|
||||||
for pkg in selected_pkgs:
|
for pkg in selected_pkgs:
|
||||||
try:
|
try:
|
||||||
|
if is_unstow:
|
||||||
|
unstow(pkg)
|
||||||
|
_log("INFO", f"Successfully unstowed package '{pkg}'.")
|
||||||
|
else:
|
||||||
stow(pkg)
|
stow(pkg)
|
||||||
_log("INFO", f"Successfully stowed package '{pkg}'.")
|
_log("INFO", f"Successfully stowed package '{pkg}'.")
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
_log("ERROR", f"Failed to stow package '{pkg}': {e}")
|
_log("ERROR", f"Failed to stow package '{pkg}': {e}")
|
||||||
|
|
||||||
if args.package == "hyprland":
|
if is_unstow:
|
||||||
try:
|
return # No need to switch session if we're just unstowing
|
||||||
switch("Hyprland") # "Hyprland" as defined by hyprland
|
|
||||||
_log("INFO", "Switched session to hyprland.")
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
_log("ERROR", f"Failed to switch session: {e}")
|
|
||||||
|
|
||||||
elif args.package == "niri":
|
|
||||||
try:
|
|
||||||
switch("niri") # "niri" as defined by niri
|
|
||||||
_log("INFO", "Switched session to niri.")
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
_log("ERROR", f"Failed to switch session: {e}")
|
|
||||||
|
|
||||||
|
if args.package in SESSION_NAME:
|
||||||
|
session = SESSION_NAME[args.package]
|
||||||
else:
|
else:
|
||||||
|
session = SESSION_NAME["default"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
switch("default") # "default" as defined by me :)
|
switch(session)
|
||||||
_log("INFO", "Switched session to default.")
|
_log("INFO", f"Switched to session profile '{session}'.")
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
_log("ERROR", f"Failed to switch session: {e}")
|
_log("ERROR", f"Failed to switch session profile '{session}': {e}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
-8161
File diff suppressed because it is too large
Load Diff
@@ -1,50 +0,0 @@
|
|||||||
在 btrfs 分区下使用 swapfile 创建虚拟内存 (复杂方法):
|
|
||||||
|
|
||||||
1. 创建 swap 子卷 (假定已经挂载到 /mnt):
|
|
||||||
|
|
||||||
```bash
|
|
||||||
btrfs subvolume create /mnt/@swap
|
|
||||||
```
|
|
||||||
|
|
||||||
2. 创建 swap 文件:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
touch /mnt/@swap/swapfile
|
|
||||||
```
|
|
||||||
|
|
||||||
3. 禁用 COW:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
chattr +C /mnt/@swap/swapfile
|
|
||||||
```
|
|
||||||
|
|
||||||
4. 设置 swap 文件大小(例如 16GB):
|
|
||||||
|
|
||||||
```bash
|
|
||||||
dd if=/dev/zero of=/mnt/@swap/swapfile bs=1M count=16384 oflag=direct
|
|
||||||
# 可检查属性,确保有 C:
|
|
||||||
lsattr /mnt/@swap/swapfile
|
|
||||||
```
|
|
||||||
|
|
||||||
5. 设置 swap 文件权限:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
chmod 600 /mnt/@swap/swapfile
|
|
||||||
```
|
|
||||||
|
|
||||||
6. 启用 swap 文件:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
mkswap /mnt/@swap/swapfile
|
|
||||||
swapon /mnt/@swap/swapfile
|
|
||||||
# 可检查 swap 状态
|
|
||||||
swapon --show
|
|
||||||
```
|
|
||||||
|
|
||||||
7. 修改 `/etc/fstab` 以自动挂载 swap 文件:
|
|
||||||
|
|
||||||
```conf
|
|
||||||
UUID={btrfs-uuid} /swap btrfs rw,noatime,ssd,discard=async,space_cache=v2,subvol=/@swap 0 0
|
|
||||||
|
|
||||||
/swap/swapfile none swap sw 0 0
|
|
||||||
```
|
|
||||||
Reference in New Issue
Block a user