1
1
mirror of https://github.com/NixOS/mobile-nixos.git synced 2024-12-18 05:21:47 +03:00
mobile-nixos/boot/init/lib/system.rb

76 lines
2.2 KiB
Ruby
Raw Normal View History

2019-12-22 07:12:39 +03:00
# "System" helpers.
module System
class CommandError < StandardError
end
class CommandNotFound < CommandError
end
2019-12-23 06:32:34 +03:00
class MountError < StandardError
end
2019-12-22 07:12:39 +03:00
# Runs and pretty-prints a command. Parameters and shelling-out have the same
# meaning as with +Kernel#spawn+; one parameter is shelling-out, multiple is
# direct +exec+.
#
# @param args [Array<String>] Command and parameters
# @raise [System::CommandNotFound] on exit status 127, commonly used for command not found.
# @raise [System::CommandError] on any other exit status.
def self.run(*args)
pretty_command =
if args.length == 1
args.first
else
args.shelljoin
end
2019-12-22 07:12:39 +03:00
$logger.debug(" $ #{pretty_command}")
unless system(*args)
raise CommandError.new("Could not execute `#{pretty_command}`, status nil") if $?.nil?
status = $?.exitstatus
if status == 127
raise CommandNotFound.new("Command not found... `#{pretty_command}` (#{status})")
else
raise CommandError.new("Command failed... `#{pretty_command}` (#{status})")
end
end
end
# Mounts a filesystem of type +type+ on +dest+.
#
# The +source+ parameter is optional, though kept first to keep a coherent
# order compared to the actual +mount+ command.
#
# @overload mount(source, dest, type:)
# @param source [String] (Optional) mount source, defaults to type
# @param dest [String] Destination path to mount to
# @param type [String] Type of the mount (+-t+).
# @param options [Array<String>] Mount options (+-o+).
2019-12-22 07:12:39 +03:00
# @overload mount(dest, type:)
# @param dest [String] Destination path to mount to
# @param type [String] Type of the mount (+-t+).
# @param options [Array<String>] Mount options (+-o+).
def self.mount(source, dest = nil, type: nil, options: nil)
2019-12-22 07:12:39 +03:00
# Fill-in the "reversed" optional parameters.
unless dest
dest = source
source = type
end
2019-12-23 06:32:34 +03:00
if source.nil? and type.nil?
raise MountError.new("Cannot mount when missing both source and type.")
end
args = []
if type
args << "-t"
args << type
end
if options
args << "-o"
args << options.join(",")
end
2019-12-23 06:32:34 +03:00
args << source
args << dest
run("mount", *args)
2019-12-22 07:12:39 +03:00
end
end