2019-12-28 04:43:28 +03:00
|
|
|
class Tasks::SwitchRoot < SingletonTask
|
2019-12-28 04:56:12 +03:00
|
|
|
# Relative to root.
|
2019-12-24 08:54:55 +03:00
|
|
|
SYSTEM_LINK = "/nix/var/nix/profiles/system"
|
2019-12-28 04:56:12 +03:00
|
|
|
|
|
|
|
# Where the system will be mounted.
|
|
|
|
SYSTEM_MOUNT_POINT = "/mnt"
|
|
|
|
|
2019-12-24 08:54:55 +03:00
|
|
|
def initialize()
|
|
|
|
add_dependency(:Target, :SwitchRoot)
|
2019-12-28 04:56:12 +03:00
|
|
|
@target = SYSTEM_MOUNT_POINT
|
2019-12-24 08:54:55 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_generation()
|
2019-12-24 10:00:53 +03:00
|
|
|
# The default generation
|
|
|
|
if File.symlink?(File.join(@target, SYSTEM_LINK))
|
|
|
|
return SYSTEM_LINK
|
2019-12-24 08:54:55 +03:00
|
|
|
end
|
|
|
|
|
2019-12-24 10:00:53 +03:00
|
|
|
# Otherwise, we need to re-hydrate a system!
|
2019-12-24 08:54:55 +03:00
|
|
|
registration = File.join(@target, "nix-path-registration")
|
|
|
|
if File.exist?(registration)
|
|
|
|
path = File.read(registration)
|
|
|
|
.split("\n")
|
|
|
|
.grep(%r{^/nix/store/[a-z0-9]+-nixos-system-})
|
|
|
|
.first
|
|
|
|
return path if path
|
|
|
|
end
|
|
|
|
|
|
|
|
System.failure("init_not_found", "Could not find init path for stage-2", color: "FF00FF")
|
|
|
|
end
|
|
|
|
|
|
|
|
def run()
|
|
|
|
# TODO: Implement generation selection choice.
|
|
|
|
init = "#{find_generation}/init"
|
|
|
|
|
|
|
|
# This is the traditional way we printed the init path.
|
|
|
|
# This is still helpful to take vertical real estate when visually looking
|
|
|
|
# through the log.
|
|
|
|
log("")
|
|
|
|
log("***")
|
|
|
|
log("")
|
|
|
|
log("Switching root to #{init}")
|
|
|
|
log("")
|
|
|
|
log("***")
|
|
|
|
log("")
|
|
|
|
|
|
|
|
[
|
|
|
|
"/proc",
|
|
|
|
"/sys",
|
|
|
|
"/dev",
|
|
|
|
"/run",
|
|
|
|
].each do |mount_point|
|
|
|
|
new_location = File.join(@target, mount_point)
|
|
|
|
FileUtils.mkdir_p(new_location)
|
|
|
|
System.run("mount", "--move", mount_point, new_location)
|
|
|
|
end
|
|
|
|
|
|
|
|
switch_root = System.which("switch_root")
|
|
|
|
System.exec({}, switch_root, @target, init)
|
|
|
|
end
|
|
|
|
end
|