getch 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 551c234f0650bb73f209c06becaedefe429bb5c14b6c65debc49c4f4f60b86f7
4
- data.tar.gz: 5f1259b54738dfd0e43787a509c7c327888698c8af43be498bec0f86646ad0bf
3
+ metadata.gz: 769727fcda94cc7d18928f9ae8dc107756073179877b2b47fcc75c03f5483dc2
4
+ data.tar.gz: 471c26161637a5d94ebd8c77708a4ae93bdfbc8f443ff1f120b60b4986f1f637
5
5
  SHA512:
6
- metadata.gz: de7fcca94c7461638bdf0c9ef691d8299eb52b090394c605e819473dada25e2a15125ce67bd7bc3aca27276b6fad1d422634ffd34ee3f04e69d2975b88b632c2
7
- data.tar.gz: 3115a72e891651b61020c42c2ccf0cd3d1134192116df7c9bd9baad5b26d5e98db233ae63c8d627e7655f40b69336c35d355fc4d41a2aabc9525e8fbfffd00dc
6
+ metadata.gz: dbe1bc854c99ae26e7a8dd4d62325224b9949909bcde2bb389830183bc91878847148b3995e8b8928e0481e3fc86233b359989b758bf7a915254a18f5a66940c
7
+ data.tar.gz: 3335a022ab70eecbae37c21026c3739751718f9dd38d4410137a15de26c32f8c7d0d7d658b609c29f03976233f54a33130971c7c9cff5291249c8a525e21e7a8
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,6 +1,9 @@
1
1
  require_relative 'getch/options'
2
2
  require_relative 'getch/disk'
3
3
  require_relative 'getch/states'
4
+ require_relative 'getch/mount'
5
+ require_relative 'getch/gentoo'
6
+ require_relative 'getch/helpers'
4
7
 
5
8
  module Getch
6
9
 
@@ -11,15 +14,20 @@ module Getch
11
14
  disk: 'sda',
12
15
  fs: 'ext4',
13
16
  username: nil
14
- }
17
+ }.freeze
15
18
 
16
19
  STATES = {
17
20
  :partition => false,
18
21
  :format => false,
19
22
  :mount => false,
20
- :gentoo_base => false
23
+ :gentoo_base => false,
24
+ :gentoo_config => false,
25
+ :gentoo_update => false
21
26
  }
22
27
 
28
+ MOUNTPOINT = "/mnt/gentoo".freeze
29
+ #MOUNTPOINT = "/home/daggoth/lol".freeze
30
+
23
31
  def self.resume_options(opts)
24
32
  puts "\nBuild Gentoo with the following args:\n"
25
33
  puts "lang: #{opts.language}"
@@ -53,10 +61,28 @@ module Getch
53
61
  end
54
62
  end
55
63
 
64
+ def self.mount(disk, user)
65
+ return if STATES[:mount]
66
+ mount = Getch::Mount.new(disk, user)
67
+ mount.swap
68
+ mount.root
69
+ mount.boot
70
+ mount.home
71
+ end
72
+
73
+ def self.init_gentoo(options)
74
+ gentoo = Getch::Gentoo
75
+ gentoo.stage3
76
+ gentoo.config(options)
77
+ gentoo.chroot
78
+ end
79
+
56
80
  def self.main(argv)
57
- options = Options.new(DEFAULT_OPTIONS, argv)
81
+ options = Options.new(argv)
58
82
  resume_options(options)
59
83
  Getch::States.new() # Update States
60
84
  format(options.disk, options.fs)
85
+ mount(options.disk, options.username)
86
+ init_gentoo(options)
61
87
  end
62
88
  end
@@ -6,10 +6,6 @@ module Getch
6
6
  @state = Getch::States.new()
7
7
  end
8
8
 
9
- def efi?
10
- Dir.exist? '/sys/firmware/efi/efivars'
11
- end
12
-
13
9
  # https://wiki.archlinux.org/index.php/Securely_wipe_disk
14
10
  def cleaning
15
11
  return if STATES[:partition ]
@@ -25,9 +21,9 @@ module Getch
25
21
 
26
22
  def partition
27
23
  return if STATES[:partition]
28
- system("wipefs -a /dev/#{@hdd}")
29
24
  system("sgdisk --zap-all /dev/#{@hdd}")
30
- if efi? then
25
+ system("wipefs -a /dev/#{@hdd}")
26
+ if Helpers::efi? then
31
27
  puts "Partition disk #{@hdd} for an EFI system"
32
28
  partition_efi
33
29
  else
@@ -40,15 +36,13 @@ module Getch
40
36
  def format
41
37
  return if STATES[:format]
42
38
  puts "Format #{@hdd} with #{@fs}"
43
- if efi? then
44
- system("mkfs.vfat -F32 /dev/#{@hdd}1")
39
+ if Helpers::efi? then
40
+ system("mkfs.fat -F32 /dev/#{@hdd}1")
45
41
  system("mkswap /dev/#{@hdd}2")
46
- system("swapon /dev/#{@hdd}2")
47
42
  system("mkfs.ext4 /dev/#{@hdd}3")
48
43
  system("mkfs.ext4 /dev/#{@hdd}4")
49
44
  else
50
45
  system("mkswap /dev/#{@hdd}2")
51
- system("swapon /dev/#{@hdd}2")
52
46
  system("mkfs.ext4 /dev/#{@hdd}3")
53
47
  system("mkfs.ext4 /dev/#{@hdd}4")
54
48
  end
@@ -0,0 +1,45 @@
1
+ require 'open-uri'
2
+ require 'open3'
3
+ require_relative 'gentoo/stage'
4
+ require_relative 'gentoo/config'
5
+ require_relative 'gentoo/chroot'
6
+
7
+ module Getch
8
+ module Gentoo
9
+ class << self
10
+ def new
11
+ @state = Getch::States.new()
12
+ end
13
+
14
+ def stage3
15
+ return if STATES[:gentoo_base]
16
+ new
17
+ stage = Getch::Gentoo::Stage.new()
18
+ stage.get_stage3
19
+ stage.control_files
20
+ stage.checksum
21
+ @state.stage3
22
+ end
23
+
24
+ def config(options)
25
+ return if STATES[:gentoo_config]
26
+ new
27
+ config = Getch::Gentoo::Config.new()
28
+ config.portage
29
+ config.portage_fs
30
+ config.repo
31
+ config.network
32
+ config.systemd(options)
33
+ @state.config
34
+ end
35
+
36
+ def chroot
37
+ chroot = Getch::Gentoo::Chroot.new()
38
+ chroot.update
39
+ chroot.world
40
+ chroot.systemd
41
+ chroot.kernel
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,59 @@
1
+ module Getch
2
+ module Gentoo
3
+ class Chroot
4
+ def initialize
5
+ @state = Getch::States.new()
6
+ mount
7
+ end
8
+
9
+ def update
10
+ return if STATES[:gentoo_update]
11
+ puts "Downloading the last ebuilds for Gentoo..."
12
+ cmd = "emerge-webrsync"
13
+ exec_chroot(cmd)
14
+ end
15
+
16
+ def world
17
+ return if STATES[:gentoo_update]
18
+ puts "Update Gentoo"
19
+ cmd = "emerge --update --deep --newuse @world"
20
+ exec_chroot(cmd)
21
+ @state.update
22
+ end
23
+
24
+ def systemd
25
+ puts "Updating locale, keymap..."
26
+ cmd = "locale-gen; emerge --config sys-libs/timezone-data"
27
+ exec_chroot(cmd)
28
+ end
29
+
30
+ def kernel
31
+ puts "Installing kernel gentoo-sources..."
32
+ cmd = "emerge sys-kernel/gentoo-sources sys-kernel/linux-firmware"
33
+ license = "#{MOUNTPOINT}/etc/portage/package.license"
34
+ File.write(license, "sys-kernel/linux-firmware linux-fw-redistributable no-source-code\n")
35
+ exec_chroot(cmd)
36
+ end
37
+
38
+ private
39
+ def mount
40
+ puts "Populate /proc, /sys and /dev."
41
+ Helpers::exec_or_die("mount --types proc /proc \"#{MOUNTPOINT}/proc\"")
42
+ Helpers::exec_or_die("mount --rbind /sys \"#{MOUNTPOINT}/sys\"")
43
+ Helpers::exec_or_die("mount --make-rslave \"#{MOUNTPOINT}/sys\"")
44
+ Helpers::exec_or_die("mount --rbind /dev \"#{MOUNTPOINT}/dev\"")
45
+ Helpers::exec_or_die("mount --make-rslave \"#{MOUNTPOINT}/dev\"")
46
+ # Maybe add /dev/shm like describe here:
47
+ # https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Base
48
+ end
49
+
50
+ def exec_chroot(cmd)
51
+ script = "chroot #{MOUNTPOINT} /bin/bash -c \"
52
+ source /etc/profile
53
+ #{cmd}
54
+ \""
55
+ Helpers::exec_or_die(script)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,94 @@
1
+ require 'fileutils'
2
+ require 'tempfile'
3
+
4
+ module Getch
5
+ module Gentoo
6
+ class Config
7
+ def initialize
8
+ @make = "#{MOUNTPOINT}/etc/portage/make.conf"
9
+ end
10
+
11
+ def portage
12
+ nproc = `nproc`.chomp()
13
+ data = [
14
+ '',
15
+ 'ACCEPT_KEYWORD="amd64 ~amd64"',
16
+ "MAKEOPTS=\"-j#{nproc} -l#{nproc}\"",
17
+ 'INPUT_DEVICES="libinput"'
18
+ ]
19
+ File.write(@make, data.join("\n"), mode: "a")
20
+ end
21
+
22
+ def repo
23
+ src = "#{MOUNTPOINT}/usr/share/portage/config/repos.conf"
24
+ dest = "#{MOUNTPOINT}/etc/portage/repos.conf"
25
+ FileUtils.mkdir dest, mode: 0644 if ! Dir.exist?(dest)
26
+ line_count = 0
27
+ tmp_file = Tempfile.new('gentoo.conf')
28
+ File.open(src).each { |l|
29
+ File.write(tmp_file, "sync-allow-hardlinks = yes\n", mode: 'a') if line_count == 2
30
+ File.write(tmp_file, l, mode: 'a')
31
+ line_count += 1
32
+ }
33
+ FileUtils.copy_file(tmp_file, "#{dest}/gentoo.conf", preserve = false)
34
+ end
35
+
36
+ def network
37
+ src = '/etc/resolv.conf'
38
+ dest = "#{MOUNTPOINT}/etc/resolv.conf"
39
+ FileUtils.copy_file(src, dest, preserve = false)
40
+ end
41
+
42
+ def systemd(options)
43
+ control_options(options)
44
+ File.write("#{MOUNTPOINT}/etc/locale.gen", @utf8)
45
+ File.write("#{MOUNTPOINT}/etc/locale.conf", "LANG=#{@lang}")
46
+ File.write("#{MOUNTPOINT}/etc/locale.conf", 'LC_COLLATE=C', mode: 'a')
47
+ File.write("#{MOUNTPOINT}/etc/timezone", "#{options.zoneinfo}")
48
+ File.write("#{MOUNTPOINT}/etc/vconsole.conf", "KEYMAP=#{@keymap}")
49
+ end
50
+
51
+ def portage_fs
52
+ portage = "#{MOUNTPOINT}/etc/portage"
53
+ Helpers::create_dir("#{portage}/package.use")
54
+ Helpers::create_dir("#{portage}/package.accept_keywords")
55
+ Helpers::create_dir("#{portage}/package.unmask")
56
+
57
+ Helpers::add_file("#{portage}/package.use/zzz_via_autounmask")
58
+ Helpers::add_file("#{portage}/package.accept_keywords/zzz_via_autounmask")
59
+ Helpers::add_file("#{portage}/package.unmask/zzz_via_autounmask")
60
+ end
61
+
62
+ private
63
+
64
+ def control_options(options)
65
+ search_zone(options.zoneinfo)
66
+ search_utf8(options.language)
67
+ search_key(options.keyboard)
68
+ end
69
+
70
+ def search_key(keys)
71
+ @keymap = nil
72
+ Dir.glob("#{MOUNTPOINT}/usr/share/keymaps/**/#{keys}.map.gz") { |f|
73
+ @keymap = f
74
+ }
75
+ raise "No keymap #{@keymap} found" if ! @keymap
76
+ end
77
+
78
+ def search_zone(zone)
79
+ if ! File.exist?("#{MOUNTPOINT}/usr/share/zoneinfo/#{zone}")
80
+ raise "Zoneinfo #{zone} doesn\'t exist."
81
+ end
82
+ end
83
+
84
+ def search_utf8(lang)
85
+ @utf8, @lang = nil, nil
86
+ File.open("#{MOUNTPOINT}/usr/share/i18n/SUPPORTED").each { |l|
87
+ @utf8 = $~[0] if l.match(/^#{lang}[. ]+[utf\-8 ]+/i)
88
+ @lang = $~[0] if l.match(/^#{lang}[. ]+utf\-8/i)
89
+ }
90
+ raise "Lang #{lang} no found" if ! @utf8
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,74 @@
1
+ require 'open-uri'
2
+ require 'open3'
3
+
4
+ module Getch
5
+ module Gentoo
6
+ class Stage
7
+
8
+ def initialize
9
+ @mirror = "https://mirrors.soeasyto.com/distfiles.gentoo.org"
10
+ @release = release
11
+ @stage_file="stage3-amd64-systemd-#{@release}.tar.xz"
12
+ end
13
+
14
+ def stage3
15
+ @mirror + '/releases/amd64/autobuilds/latest-stage3-amd64-systemd.txt'
16
+ end
17
+
18
+ def release
19
+ URI.open(stage3) do |file|
20
+ file.read.match(/^[[:alnum:]]+/)
21
+ end
22
+ end
23
+
24
+ def file
25
+ "#{@release}/#{@stage_file}"
26
+ end
27
+
28
+ def get_stage3
29
+ Dir.chdir(MOUNTPOINT)
30
+ return if File.exist?(@stage_file)
31
+ puts "Download the last #{@stage_file}, please wait..."
32
+ Helpers::get_file_online(@mirror + "/releases/amd64/autobuilds/" + file, @stage_file)
33
+ end
34
+
35
+ def control_files
36
+ puts "Download the DIGESTS"
37
+ Helpers::get_file_online(@mirror + "/releases/amd64/autobuilds/" + file + ".DIGESTS", "#{@stage_file}.DIGESTS")
38
+ puts "Download the DIGESTS.asc"
39
+ Helpers::get_file_online(@mirror + "/releases/amd64/autobuilds/" + file + ".DIGESTS.asc", "#{@stage_file}.DIGESTS.asc")
40
+ puts "Download the CONTENTS.gz"
41
+ Helpers::get_file_online(@mirror + "/releases/amd64/autobuilds/" + file + ".CONTENTS.gz", "#{@stage_file}.CONTENTS.gz")
42
+ end
43
+
44
+ def checksum
45
+ puts 'Check the SHA512 checksum.'
46
+ command = "awk '/SHA512 HASH/{getline;print}' #{@stage_file}.DIGESTS.asc | sha512sum --check"
47
+ _, stderr, status = Open3.capture3(command)
48
+ if status.success? then
49
+ puts "Checksum is ok"
50
+ decompress
51
+ cleaning
52
+ else
53
+ cleaning
54
+ raise "Problem with the checksum, stderr\n#{stderr}"
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ # https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Stage
61
+ def decompress
62
+ puts "Decompressing archive #{@stage_file}..."
63
+ cmd = "tar xpvf #{@stage_file} --xattrs-include='*.*' --numeric-owner"
64
+ Helpers::exec_or_die(cmd)
65
+ end
66
+
67
+ def cleaning
68
+ Dir.glob("stage3-amd64-systemd*").each do |f|
69
+ File.delete(f)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,32 @@
1
+ require 'open-uri'
2
+ require 'open3'
3
+ require 'fileutils'
4
+
5
+ module Helpers
6
+ def self.efi?
7
+ Dir.exist? '/sys/firmware/efi/efivars'
8
+ end
9
+
10
+ def self.get_file_online(url, dest)
11
+ URI.open(url) do |l|
12
+ File.open(dest, "wb") do |f|
13
+ f.write(l.read)
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.exec_or_die(cmd)
19
+ _, stderr, status = Open3.capture3(cmd)
20
+ unless status.success?
21
+ raise "Problem running #{cmd}, stderr was:\n#{stderr}"
22
+ end
23
+ end
24
+
25
+ def self.create_dir(path, perm = 0755)
26
+ FileUtils.mkdir_p path, mode: perm if ! Dir.exist?(path)
27
+ end
28
+
29
+ def self.add_file(path, content = '')
30
+ File.write path, content if ! File.exist? path
31
+ end
32
+ end
@@ -0,0 +1,43 @@
1
+ require 'fileutils'
2
+
3
+ module Getch
4
+ class Mount
5
+ def initialize(disk, user)
6
+ @disk = disk
7
+ @user = user
8
+ @dest = MOUNTPOINT
9
+ @boot_efi = MOUNTPOINT + '/boot/efi'
10
+ @home = @user == nil ? MOUNTPOINT + '/home' : MOUNTPOINT + "/home/#{@user}"
11
+ @state = Getch::States.new()
12
+ end
13
+
14
+ def swap
15
+ return if STATES[:mount]
16
+ system("swapon /dev/#{@disk}2")
17
+ end
18
+
19
+ def root
20
+ return if STATES[:mount]
21
+ Dir.mkdir(@dest, 0700) if ! Dir.exist?(@dest)
22
+ system("mount /dev/#{@disk}3 #{@dest}")
23
+ end
24
+
25
+ def boot
26
+ return if STATES[:mount]
27
+ if Helpers::efi? then
28
+ FileUtils.mkdir_p @boot_efi, mode: 0700 if ! Dir.exist?(@boot_efi)
29
+ system("mount /dev/#{@disk}1 #{@boot_efi}")
30
+ end
31
+ end
32
+
33
+ def home
34
+ return if STATES[:mount]
35
+ if @user != nil then
36
+ FileUtils.mkdir_p @home, mode: 0700 if ! Dir.exist?(@home)
37
+ system("mount /dev/#{@disk}4 #{@home}")
38
+ FileUtils.chown @user, @user, @home
39
+ end
40
+ @state.mount
41
+ end
42
+ end
43
+ end
@@ -4,13 +4,13 @@ module Getch
4
4
  class Options
5
5
  attr_reader :language, :zoneinfo, :keyboard, :disk, :fs, :username
6
6
 
7
- def initialize(default, argv)
8
- @language = default[:language]
9
- @zoneinfo = default[:location]
10
- @keyboard = default[:keyboard]
11
- @disk = default[:disk]
12
- @fs = default[:fs]
13
- @username = default[:username]
7
+ def initialize(argv)
8
+ @language = DEFAULT_OPTIONS[:language]
9
+ @zoneinfo = DEFAULT_OPTIONS[:location]
10
+ @keyboard = DEFAULT_OPTIONS[:keyboard]
11
+ @disk = DEFAULT_OPTIONS[:disk]
12
+ @fs = DEFAULT_OPTIONS[:fs]
13
+ @username = DEFAULT_OPTIONS[:username]
14
14
  parse(argv)
15
15
  end
16
16
 
@@ -17,6 +17,26 @@ module Getch
17
17
  save
18
18
  end
19
19
 
20
+ def mount
21
+ STATES[:mount] = true
22
+ save
23
+ end
24
+
25
+ def stage3
26
+ STATES[:gentoo_base] = true
27
+ save
28
+ end
29
+
30
+ def config
31
+ STATES[:gentoo_config] = true
32
+ save
33
+ end
34
+
35
+ def update
36
+ STATES[:gentoo_update] = true
37
+ save
38
+ end
39
+
20
40
  private
21
41
 
22
42
  def save
@@ -24,7 +44,7 @@ module Getch
24
44
  end
25
45
 
26
46
  def load_state()
27
- if File.exists? @file
47
+ if File.exist? @file
28
48
  state_file = YAML.load_file(@file)
29
49
  STATES.merge!(state_file)
30
50
  else
@@ -1,3 +1,3 @@
1
1
  module Getch
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.0.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: getch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - szorfein
@@ -35,7 +35,7 @@ cert_chain:
35
35
  J/zT/q2Ac7BWpSLbv6p9lChBiEnD9j24x463LR5QQjDNS5SsjzRQfFuprsa9Nqf2
36
36
  Tw==
37
37
  -----END CERTIFICATE-----
38
- date: 2020-09-12 00:00:00.000000000 Z
38
+ date: 2020-09-14 00:00:00.000000000 Z
39
39
  dependencies: []
40
40
  description:
41
41
  email:
@@ -53,6 +53,12 @@ files:
53
53
  - getch.gemspec
54
54
  - lib/getch.rb
55
55
  - lib/getch/disk.rb
56
+ - lib/getch/gentoo.rb
57
+ - lib/getch/gentoo/chroot.rb
58
+ - lib/getch/gentoo/config.rb
59
+ - lib/getch/gentoo/stage.rb
60
+ - lib/getch/helpers.rb
61
+ - lib/getch/mount.rb
56
62
  - lib/getch/options.rb
57
63
  - lib/getch/states.rb
58
64
  - lib/getch/version.rb
metadata.gz.sig CHANGED
Binary file