Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 22:38:16 +00:00
|
|
|
/*
|
2023-07-15 22:12:35 +00:00
|
|
|
* Copyright (c) 2016, 2018, 2020, 2021, 2022, 2023 Jonas 'Sortie' Termansen.
|
Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 22:38:16 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* sysmerge.c
|
|
|
|
* Upgrade current operating system from a sysroot.
|
|
|
|
*/
|
2016-02-18 21:16:02 +00:00
|
|
|
|
2018-07-14 20:34:48 +00:00
|
|
|
#include <sys/stat.h>
|
2016-02-18 21:16:02 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2021-01-15 20:12:56 +00:00
|
|
|
#include <ctype.h>
|
2016-02-18 21:16:02 +00:00
|
|
|
#include <err.h>
|
2016-04-08 21:09:38 +00:00
|
|
|
#include <errno.h>
|
2021-01-18 23:04:56 +00:00
|
|
|
#include <fcntl.h>
|
2023-07-15 22:12:35 +00:00
|
|
|
#include <getopt.h>
|
2016-02-18 21:16:02 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2021-01-15 20:12:56 +00:00
|
|
|
#include <termios.h>
|
2016-02-18 21:16:02 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "conf.h"
|
|
|
|
#include "execute.h"
|
2016-08-22 22:28:49 +00:00
|
|
|
#include "hooks.h"
|
2016-02-18 21:16:02 +00:00
|
|
|
#include "fileops.h"
|
|
|
|
#include "manifest.h"
|
|
|
|
#include "release.h"
|
|
|
|
|
2021-01-15 20:12:56 +00:00
|
|
|
static char* atcgetblob(int fd, const char* name, size_t* size_ptr)
|
|
|
|
{
|
|
|
|
ssize_t size = tcgetblob(fd, name, NULL, 0);
|
|
|
|
if ( size < 0 )
|
|
|
|
return NULL;
|
|
|
|
char* result = (char*) malloc((size_t) size + 1);
|
|
|
|
if ( !result )
|
|
|
|
return NULL;
|
|
|
|
ssize_t second_size = tcgetblob(fd, name, result, (size_t) size);
|
|
|
|
if ( second_size != size )
|
|
|
|
return free(result), (char*) NULL;
|
|
|
|
result[(size_t) size] = '\0';
|
|
|
|
if ( size_ptr )
|
|
|
|
*size_ptr = (size_t) size;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_partition_name(const char* path)
|
|
|
|
{
|
|
|
|
const char* name = path;
|
|
|
|
for ( size_t i = 0; path[i]; i++ )
|
|
|
|
if ( path[i] == '/' )
|
|
|
|
name = path + i + 1;
|
|
|
|
if ( !isalpha((unsigned char) *name) )
|
|
|
|
return false;
|
|
|
|
name++;
|
|
|
|
while ( isalpha((unsigned char) *name) )
|
|
|
|
name++;
|
|
|
|
if ( !isdigit((unsigned char) *name) )
|
|
|
|
return false;
|
|
|
|
name++;
|
|
|
|
while ( isdigit((unsigned char) *name) )
|
|
|
|
name++;
|
|
|
|
if ( *name != 'p' )
|
|
|
|
return false;
|
|
|
|
name++;
|
|
|
|
if ( !isdigit((unsigned char) *name) )
|
|
|
|
return false;
|
|
|
|
name++;
|
|
|
|
while ( isdigit((unsigned char) *name) )
|
|
|
|
name++;
|
|
|
|
return *name == '\0';
|
|
|
|
}
|
|
|
|
|
2023-07-15 22:12:35 +00:00
|
|
|
static bool has_pending_upgrade(const char* target)
|
2016-02-18 21:16:02 +00:00
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
char* sysmerge = join_paths(target, "sysmerge");
|
2023-08-07 19:31:59 +00:00
|
|
|
char* boot_sysmerge = join_paths(target, "boot/sysmerge");
|
|
|
|
if ( !sysmerge || !boot_sysmerge )
|
2023-07-15 22:12:35 +00:00
|
|
|
err(2, "malloc");
|
2023-08-07 19:31:59 +00:00
|
|
|
bool result = access_or_die(sysmerge, F_OK) == 0 ||
|
|
|
|
access_or_die(boot_sysmerge, F_OK) == 0;
|
2023-07-15 22:12:35 +00:00
|
|
|
free(sysmerge);
|
2023-08-07 19:31:59 +00:00
|
|
|
free(boot_sysmerge);
|
2023-07-15 22:12:35 +00:00
|
|
|
return result;
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 19:31:59 +00:00
|
|
|
static void update_grub(struct conf* conf, const char* target)
|
|
|
|
{
|
|
|
|
if ( conf->grub )
|
|
|
|
{
|
|
|
|
printf(" - Configuring bootloader...\n");
|
|
|
|
execute((const char*[]) { "update-grub", NULL }, "ceqQ", target);
|
|
|
|
}
|
|
|
|
else if ( access_or_die("/etc/default/grub.d/10_sortix", F_OK) == 0 )
|
|
|
|
{
|
|
|
|
printf(" - Creating bootloader fragment...\n");
|
|
|
|
execute((const char*[]) { "/etc/default/grub.d/10_sortix", NULL },
|
|
|
|
"ceq", target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:16:02 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
setvbuf(stdout, NULL, _IOLBF, 0); // Pipes.
|
|
|
|
|
|
|
|
bool booting = false;
|
2016-08-22 22:28:49 +00:00
|
|
|
bool cancel = false;
|
2021-01-18 23:04:56 +00:00
|
|
|
bool full = false;
|
2016-08-22 22:28:49 +00:00
|
|
|
bool hook_finalize = false;
|
|
|
|
bool hook_prepare = false;
|
2023-07-07 22:28:33 +00:00
|
|
|
bool ports = false;
|
|
|
|
bool system = false;
|
2023-07-15 22:12:35 +00:00
|
|
|
const char* target = "/";
|
2016-02-18 21:16:02 +00:00
|
|
|
bool wait = false;
|
|
|
|
|
2023-07-15 22:12:35 +00:00
|
|
|
enum longopt
|
2016-02-18 21:16:02 +00:00
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
OPT_BOOTING = 128,
|
|
|
|
OPT_HOOK_FINALIZE,
|
|
|
|
OPT_HOOK_PREPARE,
|
|
|
|
};
|
|
|
|
const struct option longopts[] =
|
|
|
|
{
|
|
|
|
{"booting", no_argument, NULL, OPT_BOOTING},
|
|
|
|
{"cancel", no_argument, NULL, 'c'},
|
|
|
|
{"full", no_argument, NULL, 'f'},
|
|
|
|
{"hook-finalize", no_argument, NULL, OPT_HOOK_FINALIZE},
|
|
|
|
{"hook-prepare", no_argument, NULL, OPT_HOOK_PREPARE},
|
|
|
|
{"ports", no_argument, NULL, 'p'},
|
|
|
|
{"system", no_argument, NULL, 's'},
|
|
|
|
{"target", required_argument, NULL, 't'},
|
|
|
|
{"wait", no_argument, NULL, 'w'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
const char* opts = "cfpst:w";
|
|
|
|
int opt;
|
|
|
|
while ( (opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1 )
|
|
|
|
{
|
|
|
|
switch (opt)
|
2016-02-18 21:16:02 +00:00
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
case OPT_BOOTING: booting = true; break;
|
|
|
|
case 'c': cancel = true; break;
|
|
|
|
case 'f': full = true; break;
|
|
|
|
case OPT_HOOK_FINALIZE: hook_finalize = true; break;
|
|
|
|
case OPT_HOOK_PREPARE: hook_prepare = true; break;
|
|
|
|
case 'p': ports = true; break;
|
|
|
|
case 's': system = true; break;
|
|
|
|
case 't': target = optarg; break;
|
|
|
|
case 'w': wait = true; break;
|
|
|
|
default: return 2;
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 22:28:49 +00:00
|
|
|
if ( 1 < booting + cancel + hook_finalize + hook_prepare + wait )
|
|
|
|
errx(2, "Mutually incompatible options were passed");
|
|
|
|
|
|
|
|
bool hook_only = hook_prepare || hook_finalize;
|
|
|
|
bool no_source = cancel;
|
|
|
|
bool no_cancel = booting || hook_only;
|
|
|
|
|
2016-02-18 21:16:02 +00:00
|
|
|
const char* source;
|
2016-08-22 22:28:49 +00:00
|
|
|
if ( no_source )
|
2016-02-18 21:16:02 +00:00
|
|
|
{
|
2016-08-22 22:28:49 +00:00
|
|
|
source = "";
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( optind < argc )
|
|
|
|
errx(2, "Unexpected extra operand: %s", argv[optind]);
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
|
|
|
else if ( booting )
|
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
source = join_paths(target, "sysmerge");
|
|
|
|
if ( !source )
|
|
|
|
err(2, "malloc");
|
|
|
|
if ( optind < argc )
|
|
|
|
errx(2, "Unexpected extra operand: %s", argv[optind]);
|
|
|
|
char* system_path = join_paths(target, "sysmerge/tix/sysmerge.system");
|
|
|
|
char* ports_path = join_paths(target, "sysmerge/tix/sysmerge.ports");
|
|
|
|
char* full_path = join_paths(target, "sysmerge/tix/sysmerge.full");
|
|
|
|
if ( !system_path || !ports_path || !full_path )
|
|
|
|
err(2, "malloc");
|
|
|
|
system = access_or_die(system_path, F_OK) == 0;
|
|
|
|
ports = access_or_die(ports_path, F_OK) == 0;
|
|
|
|
full = access_or_die(full_path, F_OK) == 0;
|
|
|
|
free(system_path);
|
|
|
|
free(ports_path);
|
|
|
|
free(full_path);
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( argc - optind < 1 )
|
2016-08-22 16:07:01 +00:00
|
|
|
errx(2, "No source operand was given");
|
2023-07-15 22:12:35 +00:00
|
|
|
source = argv[optind];
|
|
|
|
if ( 1 < argc - optind )
|
|
|
|
errx(2, "Unexpected extra operand: %s", argv[optind + 1]);
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:28:33 +00:00
|
|
|
if ( !system && !ports )
|
|
|
|
system = ports = true;
|
|
|
|
if ( !ports )
|
|
|
|
full = false;
|
|
|
|
|
2023-07-15 22:12:35 +00:00
|
|
|
char* system_manifest = join_paths(target, "tix/manifest/system");
|
|
|
|
if ( !system_manifest )
|
|
|
|
err(2, "malloc");
|
|
|
|
bool has_system = !access_or_die(system_manifest, F_OK);
|
|
|
|
free(system_manifest);
|
|
|
|
|
|
|
|
if ( !has_system )
|
|
|
|
system = false;
|
|
|
|
|
2023-08-07 19:31:59 +00:00
|
|
|
struct conf conf;
|
|
|
|
conf_init(&conf);
|
|
|
|
char* conf_path = join_paths(target, "etc/upgrade.conf");
|
|
|
|
if ( !conf_path )
|
|
|
|
err(2, "malloc");
|
|
|
|
if ( !conf_load(&conf, conf_path) && errno != ENOENT )
|
|
|
|
err(2, conf_path);
|
|
|
|
|
2016-08-22 22:28:49 +00:00
|
|
|
bool did_cancel = false;
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( !no_cancel && has_pending_upgrade(target) )
|
2016-02-18 21:16:02 +00:00
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
char* sysmerge = join_paths(target, "sysmerge");
|
2023-08-07 19:31:59 +00:00
|
|
|
char* boot_sysmerge = join_paths(target, "boot/sysmerge");
|
|
|
|
if ( !sysmerge || !boot_sysmerge )
|
2023-07-15 22:12:35 +00:00
|
|
|
err(2, "malloc");
|
|
|
|
execute((const char*[]) { "rm", "-rf", "--", sysmerge, NULL }, "");
|
2023-08-07 19:31:59 +00:00
|
|
|
execute((const char*[]) { "rm", "-rf", "--", boot_sysmerge, NULL }, "");
|
|
|
|
update_grub(&conf, target);
|
2016-02-18 21:16:02 +00:00
|
|
|
printf("Cancelled pending system upgrade.\n");
|
2016-08-22 22:28:49 +00:00
|
|
|
did_cancel = true;
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( cancel )
|
2016-08-22 22:28:49 +00:00
|
|
|
{
|
|
|
|
if ( !did_cancel )
|
|
|
|
printf("No system upgrade was pending.\n");
|
2016-02-18 21:16:02 +00:00
|
|
|
return 0;
|
2016-08-22 22:28:49 +00:00
|
|
|
}
|
2016-02-18 21:16:02 +00:00
|
|
|
|
2023-07-15 22:12:35 +00:00
|
|
|
char* old_release_path = join_paths(target, "etc/sortix-release");
|
|
|
|
if ( !old_release_path )
|
|
|
|
err(2, "malloc");
|
2016-02-18 21:16:02 +00:00
|
|
|
struct release old_release;
|
|
|
|
if ( !os_release_load(&old_release, old_release_path, old_release_path) )
|
2016-04-08 21:09:38 +00:00
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( has_system || errno != ENOENT )
|
|
|
|
{
|
|
|
|
if ( errno == ENOENT )
|
|
|
|
warn("%s", old_release_path);
|
|
|
|
exit(2);
|
|
|
|
}
|
2016-04-08 21:09:38 +00:00
|
|
|
}
|
2023-07-15 22:12:35 +00:00
|
|
|
free(old_release_path);
|
2016-02-18 21:16:02 +00:00
|
|
|
|
2023-07-15 22:12:35 +00:00
|
|
|
char* new_release_path = join_paths(source, "etc/sortix-release");
|
|
|
|
if ( !new_release_path )
|
|
|
|
err(2, "malloc");
|
2016-02-18 21:16:02 +00:00
|
|
|
struct release new_release;
|
|
|
|
if ( !os_release_load(&new_release, new_release_path, new_release_path) )
|
2016-04-08 21:09:38 +00:00
|
|
|
{
|
2023-07-07 22:28:33 +00:00
|
|
|
if ( !system )
|
|
|
|
new_release = old_release;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( errno == ENOENT )
|
|
|
|
warn("%s", new_release_path);
|
|
|
|
exit(2);
|
|
|
|
}
|
2016-04-08 21:09:38 +00:00
|
|
|
}
|
2016-02-18 21:16:02 +00:00
|
|
|
free(new_release_path);
|
|
|
|
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( has_system )
|
|
|
|
{
|
|
|
|
char* old_machine_path = join_paths(target, "etc/machine");
|
|
|
|
if ( !old_machine_path )
|
|
|
|
err(2, "malloc");
|
|
|
|
char* old_machine = read_string_file(old_machine_path);
|
|
|
|
if ( !old_machine )
|
|
|
|
err(2, "%s", old_machine_path);
|
|
|
|
free(old_machine_path);
|
|
|
|
char* new_machine_path = join_paths(source, "etc/machine");
|
|
|
|
if ( !new_machine_path )
|
|
|
|
err(2, "malloc");
|
|
|
|
char* new_machine = !system ? strdup(old_machine) :
|
|
|
|
read_string_file(new_machine_path);
|
|
|
|
if ( !new_machine )
|
|
|
|
err(2, "%s", new_machine_path);
|
|
|
|
if ( strcmp(old_machine, new_machine) != 0 )
|
|
|
|
errx(2, "%s (%s) does not match %s (%s)", new_machine_path,
|
|
|
|
new_machine, old_machine_path, old_machine);
|
|
|
|
free(old_machine);
|
|
|
|
free(new_machine_path);
|
|
|
|
free(new_machine);
|
|
|
|
}
|
2017-04-12 19:05:03 +00:00
|
|
|
|
2016-02-18 21:16:02 +00:00
|
|
|
// TODO: Check for version (skipping, downgrading).
|
|
|
|
|
2017-04-10 14:37:30 +00:00
|
|
|
bool can_run_new_abi =
|
|
|
|
abi_compatible(new_release.abi_major, new_release.abi_minor,
|
|
|
|
old_release.abi_major, old_release.abi_minor);
|
2016-08-22 22:28:49 +00:00
|
|
|
|
|
|
|
bool header;
|
|
|
|
bool copy_files;
|
|
|
|
bool run_prepare;
|
|
|
|
bool run_finalize;
|
|
|
|
bool my_prepare;
|
|
|
|
bool my_finalize;
|
|
|
|
if ( booting )
|
2016-02-18 21:16:02 +00:00
|
|
|
{
|
2016-08-22 22:28:49 +00:00
|
|
|
header = true;
|
|
|
|
copy_files = true;
|
|
|
|
run_prepare = true;
|
|
|
|
my_prepare = true;
|
|
|
|
run_finalize = true;
|
|
|
|
my_finalize = true;
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
2016-08-22 22:28:49 +00:00
|
|
|
else if ( hook_prepare )
|
2016-02-18 21:16:02 +00:00
|
|
|
{
|
2016-08-22 22:28:49 +00:00
|
|
|
header = false;
|
|
|
|
copy_files = false;
|
|
|
|
run_prepare = true;
|
|
|
|
my_prepare = true;
|
|
|
|
run_finalize = false;
|
|
|
|
my_finalize = false;
|
|
|
|
}
|
|
|
|
else if ( hook_finalize )
|
|
|
|
{
|
|
|
|
header = false;
|
|
|
|
copy_files = false;
|
|
|
|
run_prepare = false;
|
|
|
|
my_prepare = false;
|
|
|
|
run_finalize = true;
|
|
|
|
my_finalize = true;
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-22 22:28:49 +00:00
|
|
|
if ( !wait && !can_run_new_abi )
|
|
|
|
{
|
|
|
|
printf("%lu.%lu -> %lu.%lu ABI transition, "
|
2022-01-12 20:17:28 +00:00
|
|
|
"delaying upgrade to next boot.\n",
|
|
|
|
old_release.abi_major, old_release.abi_minor,
|
|
|
|
new_release.abi_major, new_release.abi_minor);
|
2016-08-22 22:28:49 +00:00
|
|
|
wait = true;
|
|
|
|
}
|
|
|
|
header = true;
|
|
|
|
copy_files = true;
|
|
|
|
run_prepare = !wait;
|
|
|
|
my_prepare = false;
|
|
|
|
run_finalize = !wait;
|
|
|
|
my_finalize = false;
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 22:28:33 +00:00
|
|
|
if ( !system )
|
|
|
|
{
|
|
|
|
run_prepare = false;
|
|
|
|
run_finalize = false;
|
|
|
|
}
|
|
|
|
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( wait && !has_system )
|
|
|
|
err(2, "--wait requires a system is installed in: %s", target);
|
|
|
|
|
2016-08-22 22:28:49 +00:00
|
|
|
if ( header )
|
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( wait && new_release.pretty_name )
|
2016-08-22 22:28:49 +00:00
|
|
|
printf("Scheduling upgrade to %s on next boot using %s:\n",
|
|
|
|
new_release.pretty_name, source);
|
2023-07-15 22:12:35 +00:00
|
|
|
else if ( new_release.pretty_name )
|
2016-08-22 22:28:49 +00:00
|
|
|
printf("Upgrading to %s using %s:\n",
|
|
|
|
new_release.pretty_name, source);
|
2023-07-15 22:12:35 +00:00
|
|
|
else
|
|
|
|
printf("Upgrading %s using %s:\n", target, source);
|
2016-08-22 22:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:32:04 +00:00
|
|
|
// Upgrade hooks that runs before the old system is replaced.
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( has_system && run_prepare )
|
2016-08-22 22:28:49 +00:00
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
const char* prefix = !strcmp(target, "/") ? "" : target;
|
2016-08-22 22:28:49 +00:00
|
|
|
if ( my_prepare )
|
2023-07-15 22:12:35 +00:00
|
|
|
upgrade_prepare(&old_release, &new_release, source, prefix);
|
2016-08-22 22:28:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char* new_sysmerge = join_paths(source, "sbin/sysmerge");
|
|
|
|
if ( !new_sysmerge )
|
2021-01-15 20:12:56 +00:00
|
|
|
err(2, "malloc");
|
2016-08-22 22:28:49 +00:00
|
|
|
execute((const char*[]) { new_sysmerge, "--hook-prepare", source,
|
|
|
|
NULL }, "e");
|
|
|
|
free(new_sysmerge);
|
|
|
|
}
|
|
|
|
if ( hook_prepare )
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( copy_files )
|
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
const char* sysmerge = target;
|
2016-08-22 22:28:49 +00:00
|
|
|
if ( wait )
|
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
sysmerge = join_paths(target, "sysmerge");
|
|
|
|
if ( !sysmerge )
|
|
|
|
err(2, "malloc");
|
|
|
|
if ( mkdir(sysmerge, 0755) < 0 )
|
|
|
|
err(2, "%s", sysmerge);
|
|
|
|
execute((const char*[]) { "tix-collection", sysmerge, "create",
|
2022-01-12 20:17:28 +00:00
|
|
|
NULL }, "e");
|
2016-08-22 22:28:49 +00:00
|
|
|
}
|
2023-07-15 22:12:35 +00:00
|
|
|
const char* prefix = !strcmp(sysmerge, "/") ? "" : sysmerge;
|
|
|
|
install_manifests_detect(source, prefix, system, ports, full);
|
2016-08-22 22:28:49 +00:00
|
|
|
}
|
2016-02-18 21:16:02 +00:00
|
|
|
|
|
|
|
if ( wait )
|
|
|
|
{
|
|
|
|
printf(" - Scheduling upgrade on next boot...\n");
|
2023-07-15 22:12:35 +00:00
|
|
|
char* system_path = join_paths(target, "sysmerge/tix/sysmerge.system");
|
|
|
|
char* ports_path = join_paths(target, "sysmerge/tix/sysmerge.ports");
|
|
|
|
char* full_path = join_paths(target, "sysmerge/tix/sysmerge.full");
|
2023-08-07 19:31:59 +00:00
|
|
|
char* ready_path = join_paths(target, "sysmerge/tix/sysmerge.ready");
|
|
|
|
char* sysmerge_boot = join_paths(target, "sysmerge/boot");
|
|
|
|
char* boot_sysmerge = join_paths(target, "boot/sysmerge");
|
|
|
|
if ( !system_path || !ports_path || !full_path || !ready_path ||
|
|
|
|
!sysmerge_boot || !boot_sysmerge )
|
2023-07-15 22:12:35 +00:00
|
|
|
err(2, "malloc");
|
2023-08-07 19:31:59 +00:00
|
|
|
|
2021-01-18 23:04:56 +00:00
|
|
|
if ( full )
|
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
int fd = open(full_path, O_WRONLY | O_CREAT);
|
2021-01-18 23:04:56 +00:00
|
|
|
if ( fd < 0 )
|
2023-07-15 22:12:35 +00:00
|
|
|
err(2, "%s", full_path);
|
2021-01-18 23:04:56 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2023-07-07 22:28:33 +00:00
|
|
|
if ( system && !ports )
|
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
int fd = open(system_path, O_WRONLY | O_CREAT);
|
2023-07-07 22:28:33 +00:00
|
|
|
if ( fd < 0 )
|
2023-07-15 22:12:35 +00:00
|
|
|
err(2, "%s", system_path);
|
2023-07-07 22:28:33 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
if ( ports && !system )
|
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
int fd = open(ports_path, O_WRONLY | O_CREAT);
|
2023-07-07 22:28:33 +00:00
|
|
|
if ( fd < 0 )
|
2023-07-15 22:12:35 +00:00
|
|
|
err(2, "%s", ports_path);
|
2023-07-07 22:28:33 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2023-08-07 19:31:59 +00:00
|
|
|
|
|
|
|
// Generate the new initrd in /sysmerge/boot.
|
|
|
|
execute((const char*[]) { "/sysmerge/libexec/sysmerge/prepare",
|
|
|
|
NULL }, "ce", target);
|
|
|
|
|
|
|
|
// Move the kernel and initrd files to the boot partition where the
|
|
|
|
// bootloader is guaranteed to be able to read them.
|
|
|
|
execute((const char*[]) { "rm", "-rf", "--", boot_sysmerge,
|
|
|
|
NULL }, "e");
|
|
|
|
execute((const char*[]) { "cp", "-RT", "--", sysmerge_boot,
|
|
|
|
boot_sysmerge, NULL }, "e");
|
|
|
|
|
|
|
|
// Signal the sysmerge upgrade is ready and isn't partial.
|
|
|
|
int fd = open(ready_path, O_WRONLY | O_CREAT);
|
|
|
|
if ( fd < 0 )
|
|
|
|
err(2, "%s", ready_path);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
update_grub(&conf, target);
|
2016-02-18 21:16:02 +00:00
|
|
|
|
|
|
|
printf("The system will be upgraded to %s on the next boot.\n",
|
|
|
|
new_release.pretty_name);
|
|
|
|
printf("Run %s --cancel to cancel the upgrade.\n", argv[0]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:32:04 +00:00
|
|
|
// Upgrade hooks that run after the new system is installed.
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( has_system && run_finalize )
|
2016-08-22 22:28:49 +00:00
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
const char* prefix = !strcmp(target, "/") ? "" : target;
|
2016-08-22 22:28:49 +00:00
|
|
|
if ( my_finalize )
|
2023-07-15 22:12:35 +00:00
|
|
|
upgrade_finalize(&old_release, &new_release, source, prefix);
|
2016-08-22 22:28:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char* new_sysmerge = join_paths(source, "sbin/sysmerge");
|
|
|
|
if ( !new_sysmerge )
|
2023-07-15 22:12:35 +00:00
|
|
|
err(2, "malloc");
|
2016-08-22 22:28:49 +00:00
|
|
|
execute((const char*[]) { new_sysmerge, "--hook-finalize", source,
|
|
|
|
NULL }, "e");
|
|
|
|
free(new_sysmerge);
|
|
|
|
}
|
|
|
|
if ( hook_finalize )
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-07 19:31:59 +00:00
|
|
|
// Remove the upgrade readiness marker now that the upgrade has gone through
|
|
|
|
// such that the bootloader configuration and initrds don't try to do the
|
|
|
|
// upgrade again.
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( has_system && booting )
|
2016-02-21 21:09:03 +00:00
|
|
|
{
|
2023-08-07 19:31:59 +00:00
|
|
|
char* ready_path = join_paths(target, "sysmerge/tix/sysmerge.ready");
|
|
|
|
if ( !ready_path )
|
2023-07-15 22:12:35 +00:00
|
|
|
err(2, "malloc");
|
2023-08-07 19:31:59 +00:00
|
|
|
unlink(ready_path);
|
|
|
|
free(ready_path);
|
2016-02-21 21:09:03 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 19:31:59 +00:00
|
|
|
// Update the initrd and bootloader. The new bootloader config won't refer
|
|
|
|
// to the upgrade as it's complete and the marker is gone.
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( has_system && access_or_die("/etc/fstab", F_OK) == 0 )
|
2016-02-18 21:16:02 +00:00
|
|
|
{
|
|
|
|
printf(" - Creating initrd...\n");
|
2023-07-15 22:12:35 +00:00
|
|
|
execute((const char*[]) { "update-initrd", NULL }, "ce", target);
|
2016-02-18 21:16:02 +00:00
|
|
|
|
|
|
|
if ( conf.grub )
|
|
|
|
{
|
2023-07-15 22:12:35 +00:00
|
|
|
char* boot_path = join_paths(target, "boot");
|
|
|
|
if ( !boot_path )
|
|
|
|
err(2, "malloc");
|
|
|
|
int boot_fd = open(boot_path, O_RDONLY);
|
2021-01-15 20:12:56 +00:00
|
|
|
if ( boot_fd < 0 )
|
2023-07-15 22:12:35 +00:00
|
|
|
err(2, boot_path);
|
2021-01-15 20:12:56 +00:00
|
|
|
char* boot_device = atcgetblob(boot_fd, "device-path", NULL);
|
|
|
|
if ( !boot_device )
|
2023-07-15 22:12:35 +00:00
|
|
|
err(2, "Failed to find device of filesystem: %s", boot_path);
|
2021-01-15 20:12:56 +00:00
|
|
|
close(boot_fd);
|
2023-07-15 22:12:35 +00:00
|
|
|
free(boot_path);
|
2023-08-07 19:31:59 +00:00
|
|
|
// TODO: A better design for finding the parent block device.
|
2021-01-15 20:12:56 +00:00
|
|
|
if ( is_partition_name(boot_device) )
|
|
|
|
*strrchr(boot_device, 'p') = '\0';
|
|
|
|
printf(" - Installing bootloader...\n");
|
|
|
|
execute((const char*[]) { "grub-install", boot_device,
|
2023-08-07 19:31:59 +00:00
|
|
|
NULL }, "ceqQ", target);
|
2021-01-15 20:12:56 +00:00
|
|
|
free(boot_device);
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
2023-08-07 19:31:59 +00:00
|
|
|
|
|
|
|
update_grub(&conf, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally clean up /sysmerge and /boot/sysmerge. They were left alone so
|
|
|
|
// the system remained bootable with the idempotent upgrade if it failed
|
|
|
|
// midway. Okay there's a bit of race conditions in grub-install, though the
|
|
|
|
// replacement of grub.cfg is atomic. Everything now points into the new
|
|
|
|
// system and nothing refers to the sysmerge directories.
|
|
|
|
if ( has_system && booting )
|
|
|
|
{
|
|
|
|
// TODO: After releasing Sortix 1.1, remove sysmerge.orig compatibility.
|
|
|
|
char* kernel = join_paths(target, "boot/sortix.bin.sysmerge.orig");
|
|
|
|
char* initrd = join_paths(target, "boot/sortix.initrd.sysmerge.orig");
|
|
|
|
char* sysmerge = join_paths(target, "sysmerge");
|
|
|
|
char* boot_sysmerge = join_paths(target, "boot/sysmerge");
|
|
|
|
if ( !kernel || !initrd || !sysmerge || !boot_sysmerge )
|
|
|
|
err(2, "malloc");
|
|
|
|
unlink(kernel);
|
|
|
|
unlink(initrd);
|
|
|
|
execute((const char*[]) { "rm", "-rf", "--", sysmerge, NULL }, "");
|
|
|
|
execute((const char*[]) { "rm", "-rf", "--", boot_sysmerge, NULL }, "");
|
|
|
|
free(kernel);
|
|
|
|
free(initrd);
|
|
|
|
free(sysmerge);
|
|
|
|
free(boot_sysmerge);
|
2016-02-18 21:16:02 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 22:12:35 +00:00
|
|
|
if ( new_release.pretty_name )
|
|
|
|
printf("Successfully upgraded to %s.\n", new_release.pretty_name);
|
|
|
|
else
|
|
|
|
printf("Successfully upgraded %s.\n", target);
|
2016-02-18 21:16:02 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|