Skip to content

Commit e62f5ea

Browse files
committed
Add --with-be to allow using pkg pieces from alternative BE
Fixes #506
1 parent 27eb112 commit e62f5ea

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

src/client.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,59 @@
4444
# PKG_SUCCESS_ON_NOP - when an operation completes with nothing to do, exit with
4545
# the success code (0) instead of the NOP one (4).
4646

47+
# Support re-execing using pieces from an alternate mounted boot environment to
48+
# aid recovery.
49+
#
50+
# For example:
51+
#
52+
# beadm mount 20250902
53+
# Mounted successfully on: '/tmp/tmp.miaO0W'
54+
# pkg --with-be /tmp/tmp.miaO0W update
55+
#
56+
# Deliberately use minimal dependencies as we may be in a somewhat broken
57+
# python environment. We hope at least that these core modules are intact.
58+
import os, sys, sysconfig
59+
60+
61+
def altbe():
62+
argv = sys.argv[1:]
63+
64+
be = None
65+
rest = None
66+
67+
if argv:
68+
if argv[0] == "--with-be" and len(argv) >= 2:
69+
be = argv[1]
70+
rest = argv[2:]
71+
elif argv[0].startswith("--with-be="):
72+
be = argv[0].split("=", 1)[1]
73+
rest = argv[1:]
74+
75+
if be is None:
76+
return
77+
78+
exe = sysconfig.get_config_var("EXENAME")
79+
pyver = sysconfig.get_config_var("py_version_short")
80+
libdir = sysconfig.get_config_var("LIBDIR")
81+
destlib = sysconfig.get_config_var("DESTLIB")
82+
pkglib = f"/usr/lib/pkg/python{pyver}"
83+
84+
env = os.environ.copy()
85+
env["LD_LIBRARY_PATH"] = f"{be}{libdir}"
86+
env["PYTHONPATH"] = f"{be}{pkglib}"
87+
env["PYTHONPATH"] += f":{be}{destlib}/vendor-packages"
88+
89+
try:
90+
os.execvpe(
91+
f"{be}/{exe}", [exe, f"{be}/usr/bin/pkg"] + (rest or []), env
92+
)
93+
except OSError as e:
94+
sys.stderr.write(f"error: failed to exec alt be pkg from '{be}': {e}\n")
95+
sys.exit(127)
96+
97+
98+
altbe()
99+
47100
try:
48101
import pkg.site_paths
49102
except ImportError:
@@ -72,10 +125,8 @@
72125
import itertools
73126
import locale
74127
import logging
75-
import os
76128
import re
77129
import socket
78-
import sys
79130
import tempfile
80131
import textwrap
81132
import time

src/man/pkg.1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.\" Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
22
.\" Copyright 2024 Oxide Computer Company
33
.\" Copyright 2025 OmniOS Community Edition (OmniOSce) Association.
4-
.Dd June 25, 2025
4+
.Dd September 8, 2025
55
.Dt PKG 1
66
.Os
77
.Sh NAME
@@ -650,6 +650,12 @@ is
650650
See the
651651
.Sx Environment Variables
652652
section for more information.
653+
.It Fl \&-with-be Ar path
654+
Run using python components from a boot environment mounted at
655+
.Ar path .
656+
This is useful to recover from a situation where
657+
.Nm
658+
does not run due to a problem with python modules.
653659
.El
654660
.\"
655661
.Sh SUB-COMMANDS
@@ -4297,6 +4303,12 @@ and display verbose result.
42974303
.Bd -literal
42984304
$ pkg verify -v -p /usr/bin/ls
42994305
.Ed
4306+
.It Sy Example 38 No Update using python pieces in a different BE
4307+
.Pp
4308+
.Bd -literal
4309+
$ beadm mount <old be> /be
4310+
$ pkg --with-be /be update
4311+
.Ed
43004312
.El
43014313
.Sh ENVIRONMENT VARIABLES
43024314
.Bl -tag -width Ds

0 commit comments

Comments
 (0)