2023-01-22 18:11:10 +00:00
|
|
|
# Copyright 2023 Oliver Smith
|
2020-02-20 20:07:28 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2018-08-27 21:35:05 +00:00
|
|
|
# PYTHON_ARGCOMPLETE_OK
|
2017-09-02 19:30:40 +00:00
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
from . import config
|
|
|
|
from . import parse
|
2017-10-30 19:56:38 +00:00
|
|
|
from .config import init as config_init
|
2017-09-02 19:30:40 +00:00
|
|
|
from .helpers import frontend
|
|
|
|
from .helpers import logging as pmb_logging
|
2018-07-09 20:56:06 +00:00
|
|
|
from .helpers import mount
|
2017-09-02 19:30:40 +00:00
|
|
|
from .helpers import other
|
|
|
|
|
2023-04-07 23:30:25 +00:00
|
|
|
# pmbootstrap version
|
2023-05-17 18:45:59 +00:00
|
|
|
__version__ = "1.53.0"
|
2023-04-07 23:30:25 +00:00
|
|
|
|
|
|
|
# Python version check
|
2023-04-07 23:30:24 +00:00
|
|
|
version = sys.version_info
|
|
|
|
if version < (3, 7):
|
|
|
|
print("You need at least Python 3.7 to run pmbootstrap")
|
|
|
|
print("(You are running it with Python " + str(version.major) +
|
|
|
|
"." + str(version.minor) + ")")
|
|
|
|
sys.exit()
|
|
|
|
|
2017-09-02 19:30:40 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
# Wrap everything to display nice error messages
|
2018-09-05 05:57:38 +00:00
|
|
|
args = None
|
2017-09-02 19:30:40 +00:00
|
|
|
try:
|
2018-09-05 05:57:38 +00:00
|
|
|
# Parse arguments, set up logging
|
|
|
|
args = parse.arguments()
|
|
|
|
os.umask(0o22)
|
|
|
|
|
2023-03-25 20:53:38 +00:00
|
|
|
# Store script invocation command
|
|
|
|
os.environ["PMBOOTSTRAP_CMD"] = sys.argv[0]
|
|
|
|
|
2018-01-14 08:13:35 +00:00
|
|
|
# Sanity checks
|
2021-10-16 16:15:54 +00:00
|
|
|
other.check_grsec()
|
2018-01-14 08:13:35 +00:00
|
|
|
if not args.as_root and os.geteuid() == 0:
|
|
|
|
raise RuntimeError("Do not run pmbootstrap as root!")
|
2017-09-02 19:30:40 +00:00
|
|
|
|
|
|
|
# Initialize or require config
|
|
|
|
if args.action == "init":
|
2017-10-30 19:56:38 +00:00
|
|
|
return config_init.frontend(args)
|
2017-09-02 19:30:40 +00:00
|
|
|
elif not os.path.exists(args.config):
|
2017-12-02 16:40:55 +00:00
|
|
|
raise RuntimeError("Please specify a config file, or run"
|
|
|
|
" 'pmbootstrap init' to generate one.")
|
|
|
|
elif not os.path.exists(args.work):
|
|
|
|
raise RuntimeError("Work path not found, please run 'pmbootstrap"
|
|
|
|
" init' to create it.")
|
2017-09-02 19:30:40 +00:00
|
|
|
|
2020-02-26 13:48:48 +00:00
|
|
|
other.check_old_devices(args)
|
|
|
|
|
2017-10-12 20:08:10 +00:00
|
|
|
# Migrate work folder if necessary
|
|
|
|
if args.action not in ["shutdown", "zap", "log"]:
|
|
|
|
other.migrate_work_folder(args)
|
|
|
|
|
2017-09-02 19:30:40 +00:00
|
|
|
# Run the function with the action's name (in pmb/helpers/frontend.py)
|
|
|
|
if args.action:
|
|
|
|
getattr(frontend, args.action)(args)
|
|
|
|
else:
|
|
|
|
logging.info("Run pmbootstrap -h for usage information.")
|
|
|
|
|
2018-07-09 20:56:06 +00:00
|
|
|
# Still active notice
|
|
|
|
if mount.ismount(args.work + "/chroot_native/dev"):
|
|
|
|
logging.info("NOTE: chroot is still active (use 'pmbootstrap"
|
|
|
|
" shutdown' as necessary)")
|
2021-08-12 15:03:26 +00:00
|
|
|
logging.info("DONE!")
|
2017-09-02 19:30:40 +00:00
|
|
|
|
2023-04-07 23:30:24 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("\nCaught KeyboardInterrupt, exiting …")
|
|
|
|
sys.exit(130) # SIGINT(2) + 128
|
|
|
|
|
2017-09-02 19:30:40 +00:00
|
|
|
except Exception as e:
|
2019-02-13 08:55:26 +00:00
|
|
|
# Dump log to stdout when args (and therefore logging) init failed
|
|
|
|
if not args:
|
|
|
|
logging.getLogger().setLevel(logging.DEBUG)
|
|
|
|
|
2017-09-02 19:30:40 +00:00
|
|
|
logging.info("ERROR: " + str(e))
|
2018-07-14 01:13:28 +00:00
|
|
|
logging.info("See also: <https://postmarketos.org/troubleshooting>")
|
|
|
|
logging.debug(traceback.format_exc())
|
|
|
|
|
|
|
|
# Hints about the log file (print to stdout only)
|
2018-09-05 05:57:38 +00:00
|
|
|
log_hint = "Run 'pmbootstrap log' for details."
|
|
|
|
if not args or not os.path.exists(args.log):
|
|
|
|
log_hint += (" Alternatively you can use '--details-to-stdout' to"
|
2021-05-19 18:44:28 +00:00
|
|
|
" get more output, e.g. 'pmbootstrap"
|
|
|
|
" --details-to-stdout init'.")
|
2023-05-21 07:01:48 +00:00
|
|
|
print()
|
2018-09-05 05:57:38 +00:00
|
|
|
print(log_hint)
|
2023-05-21 07:01:48 +00:00
|
|
|
print()
|
|
|
|
print("Before you report this error, ensure that pmbootstrap is "
|
|
|
|
"up to date.")
|
|
|
|
print("Find the latest version here:"
|
|
|
|
" https://git.sr.ht/~postmarketos/pmbootstrap/refs")
|
|
|
|
print(f"Your version: {__version__}")
|
2017-09-02 19:30:40 +00:00
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|