Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc8e78da

Browse files
committed
Initial use of 1. chroot_in and chroot_out 2. rmrf
1 parent98b5550 commitc8e78da

File tree

3 files changed

+67
-33
lines changed

3 files changed

+67
-33
lines changed

‎src/ashpk_core.py‎

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
fromconfigparserimportConfigParser,NoOptionError,NoSectionError
1414
fromfilecmpimportcmp
1515
fromreimportsub
16-
fromshutilimportcopy
16+
fromshutilimportcopy,rmtree
1717
fromtempfileimportTemporaryDirectory
1818
fromurllib.errorimportURLError,HTTPError
1919
fromurllib.requestimporturlopen
@@ -285,6 +285,7 @@ def clone_under(snap, branch):
285285
# Delete tree or branch
286286
defdelete_node(snaps,quiet,nuke):
287287
forsnapinsnaps:
288+
run=False
288289
ifsnap==0:
289290
print("F: Changing base snapshot is not allowed.")
290291
ifnotnuke:
@@ -296,7 +297,9 @@ def delete_node(snaps, quiet, nuke):
296297
print("F: Cannot delete deployed snapshot.")
297298
elifnotquietandnotyes_no(f"Are you sure you want to delete snapshot{snap}?"):
298299
sys.exit("Aborted")
299-
else:
300+
else:
301+
run=True
302+
ifnukeorrun:
300303
children=return_children(fstree,snap)
301304
write_desc("",snap)# Clear description # Why have this? REVIEW 2023
302305
os.system(f"btrfs sub del /.snapshots/boot/boot-{snap}{DEBUG}")
@@ -354,7 +357,8 @@ def deploy(snap, secondary=False):
354357
###2023 os.system(f"btrfs sub create /.snapshots/var/var-{tmp} >/dev/null 2>&1") # REVIEW pretty sure not needed
355358
os.system(f"mkdir -p /.snapshots/rootfs/snapshot-{tmp}/boot{DEBUG}")
356359
os.system(f"mkdir -p /.snapshots/rootfs/snapshot-{tmp}/etc{DEBUG}")
357-
os.system(f"rm -rf /.snapshots/rootfs/snapshot-{tmp}/var{DEBUG}")
360+
###TODEL os.system(f"rm -rf /.snapshots/rootfs/snapshot-{tmp}/var{DEBUG}")
361+
rmrf(f"/.snapshots/rootfs/snapshot-{tmp}/var")
358362
os.system(f"cp -r --reflink=auto /.snapshots/boot/boot-{snap}/. /.snapshots/rootfs/snapshot-{tmp}/boot{DEBUG}")
359363
os.system(f"cp -r --reflink=auto /.snapshots/etc/etc-{snap}/. /.snapshots/rootfs/snapshot-{tmp}/etc{DEBUG}")
360364
# os.system(f"cp --reflink=auto -r /.snapshots/var/var-{etc}/* /.snapshots/var/var-{tmp} >/dev/null 2>&1") # REVIEW 2023 pretty sure not needed
@@ -708,9 +712,9 @@ def post_transactions(snap):
708712
# Some operations were moved below to fix hollow functionality ### IMPORTANT REVIEW 2023
709713
# File operations in snapshot-chr
710714
# os.system(f"btrfs sub del /.snapshots/rootfs/snapshot-{snap}{DEBUG}") ### REVIEW # Moved to a few lines below ### GOOD_TO_DELETE_2023
711-
os.system(f"rm -rf/.snapshots/boot/boot-chr{snap}/*{DEBUG}")
715+
rmrf(f"/.snapshots/boot/boot-chr{snap}","/*")
712716
os.system(f"cp -r --reflink=auto /.snapshots/rootfs/snapshot-chr{snap}/boot/. /.snapshots/boot/boot-chr{snap}{DEBUG}")
713-
os.system(f"rm -rf/.snapshots/etc/etc-chr{snap}/*{DEBUG}")
717+
rmrf(f"/.snapshots/etc/etc-chr{snap}","/*")
714718
os.system(f"cp -r --reflink=auto /.snapshots/rootfs/snapshot-chr{snap}/etc/. /.snapshots/etc/etc-chr{snap}{DEBUG}")
715719
# Keep package manager's cache after installing packages. This prevents unnecessary downloads for each snapshot when upgrading multiple snapshots
716720
cache_copy(snap,"post_transactions")
@@ -894,6 +898,20 @@ def return_children(tree, id):
894898
children.remove(id)
895899
returnchildren
896900

901+
# Pythonic rm -rf (for both deleting just contents and everything)
902+
defrmrf(a_path,contents=""):
903+
ifcontents=="/*":# just delete a_path/*
904+
forroot,dirs,filesinos.walk(a_path):
905+
forfinfiles:
906+
os.unlink(os.path.join(root,f))
907+
fordindirs:
908+
rmtree(os.path.join(root,d))
909+
else:# delete everything
910+
ifos.path.isfile(a_path):
911+
os.unlink(a_path)
912+
elifos.path.isdir(a_path):
913+
rmtree(a_path)
914+
897915
# Rollback last booted deployment
898916
defrollback():
899917
tmp=get_tmp()
@@ -1158,18 +1176,18 @@ def tree_sync(tree, tname, force_offline, live):
11581176
# Sync tree helper function ### REVIEW might need to put it in distribution-specific ashpk.py
11591177
deftree_sync_helper(CHR,s_f,s_t):
11601178
os.system("mkdir -p /.snapshots/tmp-db/local/")### REVIEW Still resembling Arch pacman folder structure!
1161-
os.system("rm -rf/.snapshots/tmp-db/local/*")### REVIEW
1179+
rmrf("/.snapshots/tmp-db/local","/*")### REVIEW
11621180
pkg_list_from=pkg_list(s_f,"")
11631181
pkg_list_to=pkg_list(s_t,CHR)
11641182
# Get packages to be inherited
11651183
pkg_list_from= [jforjinpkg_list_fromifjnotinpkg_list_to]
11661184
os.system(f"cp -r /.snapshots/rootfs/snapshot-{CHR}{s_t}/usr/share/ash/db/local/. /.snapshots/tmp-db/local/")### REVIEW
11671185
os.system(f"cp -n -r --reflink=auto /.snapshots/rootfs/snapshot-{s_f}/. /.snapshots/rootfs/snapshot-{CHR}{s_t}/{DEBUG}")
1168-
os.system(f"rm -rf/.snapshots/rootfs/snapshot-{CHR}{s_t}/usr/share/ash/db/local/*")### REVIEW
1186+
rmrf(f"/.snapshots/rootfs/snapshot-{CHR}{s_t}/usr/share/ash/db/local","/*")### REVIEW
11691187
os.system(f"cp -r /.snapshots/tmp-db/local/. /.snapshots/rootfs/snapshot-{CHR}{s_t}/usr/share/ash/db/local/")### REVIEW
11701188
forentryinpkg_list_from:
11711189
os.system(f"bash -c 'cp -r /.snapshots/rootfs/snapshot-{s_f}/usr/share/ash/db/local/{entry}-[0-9]* /.snapshots/rootfs/snapshot-{CHR}{s_t}/usr/share/ash/db/local/'")### REVIEW
1172-
os.system("rm -rf/.snapshots/tmp-db/local/*")
1190+
rmrf("/.snapshots/tmp-db/local","/*")
11731191

11741192
# Recursively run an update in tree
11751193
deftree_upgrade(tree,tname):
@@ -1317,7 +1335,7 @@ def main():
13171335
elifsys.platform.startswith("cosmo")andos.getuid()!=0:### TODO use geteuid when fixed
13181336
exit("F: Run ash with super user privileges!")
13191337
elifsys.platform.startswith("windows"):
1320-
fromctypesimportwindll# type: ignore
1338+
fromctypesimportwindll# type: ignore ### REVIEW 2023
13211339
ifwindll.shell32.IsUserAnAdmin()!=0:
13221340
exit("F: Run ash with admin privileges!")
13231341
elifsys.platform.startswith("darwin"):
@@ -1333,7 +1351,9 @@ def main():
13331351
# print("Please don't use ash inside a chroot!") ### TODO
13341352
# Recognize argument and call appropriate function
13351353
parser=ArgumentParser(prog='ash',description='Any Snapshot Hierarchical OS')
1336-
subparsers=parser.add_subparsers(dest='command',required=True,help='Different commands for ash')
1354+
#subparsers = parser.add_subparsers(dest='command', required=True, help='Different commands for ash')
1355+
subparsers=parser.add_subparsers(dest='command',help='Different commands for ash')
1356+
subparsers.required=True# backward compatibility with python <= 3.6
13371357
# Ash update
13381358
upme_par=subparsers.add_parser("upme",aliases=['ash-update'],allow_abbrev=True,help="Update ash itself")
13391359
upme_par.add_argument('--dbg','--debug','--test','-d','-t',action='store_true',required=False,help='Enable live install for snapshot')
@@ -1523,11 +1543,11 @@ def main():
15231543
whichtmp_par=subparsers.add_parser("whichtmp",aliases=["whichdep","which"],allow_abbrev=True,help="Show which deployment snapshot is in use")
15241544
whichtmp_par.set_defaults(func=lambda:get_tmp(console=True))# print to console REVIEW 2023 NEWER: test print(get_tmp)
15251545
# Which snapshot(s) contain a package
1526-
which_snap_par=subparsers.add_parser("whichsnap",aliases=["ws"],allow_abbrev=True,help='Get list of installed packages in a snapshot')
1527-
which_snap_par.add_argument('pkg',nargs='+',help='a package')
1546+
which_snap_par=subparsers.add_parser("whichsnap",aliases=["ws"],allow_abbrev=True,help='Which snapshot has a package installed')
1547+
which_snap_par.add_argument('--pkg','--package','-p',nargs='+',required=True,help='a package')
1548+
#which_snap_par.add_argument('pkg', nargs='+', help='a package')
15281549
which_snap_par.set_defaults(func=which_snapshot_has)
15291550
# Call relevant functions
1530-
#args_1 = parser.parse_args()
15311551
args_1=parser.parse_args(args=Noneifsys.argv[1:]else ['--help'])# Show help if no command used
15321552
args_2=vars(args_1).copy()
15331553
args_2.pop('command',None)

‎src/distros/arch/ashpk.py‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,42 @@ def cache_copy(snap, FROM):
9999

100100
# Fix signature invalid error
101101
deffix_package_db(snap=0):
102+
run_chroot=False
102103
ifnotos.path.exists(f"/.snapshots/rootfs/snapshot-{snap}"):
103104
print(f"F: Cannot fix package manager database as snapshot{snap} doesn't exist.")
104105
return
105106
elifos.path.exists(f"/.snapshots/rootfs/snapshot-chr{snap}"):
106107
print(f"F: Snapshot{snap} appears to be in use. If you're certain it's not in use, clear lock with 'ash unlock -s{snap}'.")
107108
return
108109
elifsnap==0:
109-
P=""### I think this is wrong. It should be check if snapshot = current-deployed-snapshot, then this.
110+
run_chroot=False# Before:P = "" ### I think this is wrong. It should be check if snapshot = current-deployed-snapshot, then this.
110111
else:
111-
P=f"chroot /.snapshots/rootfs/snapshot-chr{snap} "
112+
run_chroot=True
113+
P=f"/.snapshots/rootfs/snapshot-chr{snap}"# before: P = f"chroot /.snapshots/rootfs/snapshot-chr{snap} "
112114
try:
113115
ifcheck_mutability(snap):
114116
flip=False# Snapshot is mutable so do not make it immutable after fixdb is done
115117
else:
116118
immutability_disable(snap)
117119
flip=True
118120
prepare(snap)
119-
os.system(f"{P}rm -rf /etc/pacman.d/gnupg $HOME/.gnupg")### $HOME vs /root NEEDS fixing # If folder not present and subprocess.run is used, throws error and stops
120-
os.system(f"{P}rm -r /var/lib/pacman/db.lck")
121-
os.system(f"{P}pacman -Syy")
122-
os.system(f"{P}gpg --refresh-keys")
123-
os.system(f"{P}killall gpg-agent")
124-
os.system(f"{P}pacman-key --init")
125-
os.system(f"{P}pacman-key --populate archlinux")
126-
os.system(f"{P}pacman -Syvv --noconfirm archlinux-keyring")### REVIEW NEEDED? (maybe)
121+
# chroot in if needed
122+
rr=None
123+
ifrun_chroot:
124+
rr=chroot_in(P)
125+
# Fix package database
126+
# before all these were like: os.system(f"{P}XYZ")
127+
os.system("rm -rf /etc/pacman.d/gnupg $HOME/.gnupg")### $HOME vs /root NEEDS fixing # If folder not present and subprocess.run is used, throws error and stops
128+
os.system("rm -r /var/lib/pacman/db.lck")
129+
os.system("pacman -Syy")
130+
os.system("gpg --refresh-keys")
131+
os.system("killall gpg-agent")
132+
os.system("pacman-key --init")
133+
os.system("pacman-key --populate archlinux")
134+
os.system("pacman -Syvv --noconfirm archlinux-keyring")### REVIEW NEEDED? (maybe)
135+
# chroot out if needed
136+
ifrun_chroot:
137+
chroot_out(rr)
127138
post_transactions(snap)
128139
ifflip:
129140
immutability_enable(snap)
@@ -135,15 +146,15 @@ def fix_package_db(snap = 0):
135146
# Delete init system files (Systemd, OpenRC, etc.)
136147
definit_system_clean(snap,FROM):
137148
ifFROM=="prepare":
138-
os.system(f"rm -rf/.snapshots/rootfs/snapshot-chr{snap}/var/lib/systemd/*{DEBUG}")
149+
rmrf(f"/.snapshots/rootfs/snapshot-chr{snap}/var/lib/systemd","/*")
139150
elifFROM=="deploy":
140-
os.system(f"rm -rf/var/lib/systemd/*{DEBUG}")
141-
os.system(f"rm -rf/.snapshots/rootfs/snapshot-{snap}/var/lib/systemd/*{DEBUG}")
151+
rmrf("/var/lib/systemd","/*")
152+
rmrf(f"/.snapshots/rootfs/snapshot-{snap}/var/lib/systemd/","/*")
142153

143154
# Copy init system files (Systemd, OpenRC, etc.) to shared
144155
definit_system_copy(snap,FROM):
145156
ifFROM=="post_transactions":
146-
os.system(f"rm -rf/var/lib/systemd/*{DEBUG}")
157+
rmrf("/var/lib/systemd","/*")
147158
os.system(f"cp -r --reflink=auto /.snapshots/rootfs/snapshot-{snap}/var/lib/systemd/. /var/lib/systemd/{DEBUG}")
148159

149160
# Install atomic-operation

‎src/installer_core.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
fromreimportsearch
77
fromsetupimportargs,distro,distro_name
88
fromshutilimportcopy,which
9+
fromashpk_coreimportrmrf
910

1011
SUDO="sudo"### Test and emove if sudo not needed in any distro
1112

@@ -50,8 +51,10 @@ def deploy_base_snapshot():
5051
os.system(f"{SUDO} chroot /mnt{SUDO} btrfs sub set-default /.snapshots/rootfs/snapshot-deploy")
5152
os.system(f"{SUDO} cp -r /mnt/root/. /mnt/.snapshots/root/")
5253
os.system(f"{SUDO} cp -r /mnt/tmp/. /mnt/.snapshots/tmp/")
53-
os.system(f"{SUDO} rm -rf /mnt/root/*")
54-
os.system(f"{SUDO} rm -rf /mnt/tmp/*")
54+
#os.system(f"{SUDO} rm -rf /mnt/root/*")
55+
#os.system(f"{SUDO} rm -rf /mnt/tmp/*")
56+
rmrf("/mnt/root","/*")
57+
rmrf("/mnt/tmp","/*")
5558

5659
# Copy boot and etc: deployed snapshot <---> common
5760
defdeploy_to_common():
@@ -121,20 +124,20 @@ def get_ip():
121124
returnIP
122125

123126
# Generic function to choose something from a directory
124-
defget_item_from_path(thing,apath):
127+
defget_item_from_path(thing,a_path):
125128
clear()
126129
whileTrue:
127130
print(f"Select a{thing} (type list to list):")
128131
ch=input("> ")
129132
ifch=="list":
130133
ch= []
131-
forroot,_dirs,filesinos.walk(apath,followlinks=True):
134+
forroot,_dirs,filesinos.walk(a_path,followlinks=True):
132135
forfileinfiles:
133-
ch.append(os.path.join(root,file).replace(f"{apath}/",""))
136+
ch.append(os.path.join(root,file).replace(f"{a_path}/",""))
134137
ch="\n".join(sorted(ch))
135138
os.system(f"echo '{ch}' | less")
136139
else:
137-
temp=str(f"{apath}/{ch}")
140+
temp=str(f"{a_path}/{ch}")
138141
ifnot (os.path.isfile(temp)oros.path.isdir(temp) ):
139142
print(f"Invalid{thing}!")
140143
continue

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp