final.scm 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu installer final)
  20. #:use-module (gnu installer newt page)
  21. #:use-module (gnu installer steps)
  22. #:use-module (gnu installer utils)
  23. #:use-module (gnu installer user)
  24. #:use-module (gnu services herd)
  25. #:use-module (guix build syscalls)
  26. #:use-module (guix build utils)
  27. #:use-module (gnu build accounts)
  28. #:use-module (gnu build install)
  29. #:use-module (gnu build linux-container)
  30. #:use-module ((gnu system shadow) #:prefix sys:)
  31. #:use-module (rnrs io ports)
  32. #:use-module (srfi srfi-1)
  33. #:use-module (ice-9 ftw)
  34. #:use-module (ice-9 popen)
  35. #:use-module (ice-9 match)
  36. #:use-module (ice-9 format)
  37. #:use-module (ice-9 rdelim)
  38. #:export (install-system))
  39. (define %seed
  40. (seed->random-state
  41. (logxor (getpid) (car (gettimeofday)))))
  42. (define (integer->alphanumeric-char n)
  43. "Map N, an integer in the [0..62] range, to an alphanumeric character."
  44. (cond ((< n 10)
  45. (integer->char (+ (char->integer #\0) n)))
  46. ((< n 36)
  47. (integer->char (+ (char->integer #\A) (- n 10))))
  48. ((< n 62)
  49. (integer->char (+ (char->integer #\a) (- n 36))))
  50. (else
  51. (error "integer out of bounds" n))))
  52. (define (random-string len)
  53. "Compute a random string of size LEN where each character is alphanumeric."
  54. (let loop ((chars '())
  55. (len len))
  56. (if (zero? len)
  57. (list->string chars)
  58. (let ((n (random 62 %seed)))
  59. (loop (cons (integer->alphanumeric-char n) chars)
  60. (- len 1))))))
  61. (define (create-user-database users root)
  62. "Create /etc/passwd, /etc/shadow, and /etc/group under ROOT for the given
  63. USERS."
  64. (define etc
  65. (string-append root "/etc"))
  66. (define (salt)
  67. ;; "$6" gives us a SHA512 password hash; the random string must be taken
  68. ;; from the './0-9A-Za-z' alphabet (info "(libc) Passphrase Storage").
  69. (string-append "$6$" (random-string 10)))
  70. (define users*
  71. (map (lambda (user)
  72. (define root?
  73. (string=? "root" (user-name user)))
  74. (sys:user-account (name (user-name user))
  75. (comment (user-real-name user))
  76. (group "users")
  77. (uid (if root? 0 #f))
  78. (home-directory
  79. (user-home-directory user))
  80. (password (crypt
  81. (secret-content (user-password user))
  82. (salt)))
  83. ;; We need a string here, not a file-like, hence
  84. ;; this choice.
  85. (shell
  86. "/run/current-system/profile/bin/bash")))
  87. users))
  88. (define-values (group password shadow)
  89. (user+group-databases users* sys:%base-groups
  90. #:current-passwd '()
  91. #:current-groups '()
  92. #:current-shadow '()))
  93. (mkdir-p etc)
  94. (write-group group (string-append etc "/group"))
  95. (write-passwd password (string-append etc "/passwd"))
  96. (write-shadow shadow (string-append etc "/shadow")))
  97. (define (call-with-mnt-container thunk)
  98. "This is a variant of call-with-container. Run THUNK in a new container
  99. process, within a separate MNT namespace. The container is not jailed so that
  100. it can interact with the rest of the system."
  101. (let ((pid (run-container "/" '() '(mnt) 1 thunk)))
  102. ;; Catch SIGINT and kill the container process.
  103. (sigaction SIGINT
  104. (lambda (signum)
  105. (false-if-exception
  106. (kill pid SIGKILL))))
  107. (match (waitpid pid)
  108. ((_ . status) status))))
  109. (define (install-locale locale)
  110. "Install the given LOCALE or the en_US.utf8 locale as a fallback."
  111. (let ((supported? (false-if-exception
  112. (setlocale LC_ALL locale))))
  113. (if supported?
  114. (begin
  115. (installer-log-line "install supported locale ~a." locale)
  116. (setenv "LC_ALL" locale))
  117. (begin
  118. ;; If the selected locale is not supported, install a default UTF-8
  119. ;; locale. This is required to copy some files with UTF-8
  120. ;; characters, in the nss-certs package notably. Set LANGUAGE
  121. ;; anyways, to have translated messages if possible.
  122. (installer-log-line "~a locale is not supported, installing \
  123. en_US.utf8 locale instead." locale)
  124. (setlocale LC_ALL "en_US.utf8")
  125. (setenv "LC_ALL" "en_US.utf8")
  126. (setenv "LANGUAGE"
  127. (string-take locale
  128. (or (string-index locale #\_)
  129. (string-length locale))))))))
  130. (define* (install-system locale #:key (users '()))
  131. "Create /etc/shadow and /etc/passwd on the installation target for USERS.
  132. Start COW-STORE service on target directory and launch guix install command in
  133. a subshell. LOCALE must be the locale name under which that command will run,
  134. or #f. Return #t on success and #f on failure."
  135. (define backing-directory
  136. ;; Sub-directory used as the backing store for copy-on-write.
  137. "/tmp/guix-inst")
  138. (define (assert-exit x)
  139. (primitive-exit (if x 0 1)))
  140. (let* ((options (catch 'system-error
  141. (lambda ()
  142. ;; If this file exists, it can provide
  143. ;; additional command-line options.
  144. (call-with-input-file
  145. "/tmp/installer-system-init-options"
  146. read))
  147. (const '())))
  148. (install-command (append (list "guix" "system" "init"
  149. "--fallback")
  150. options
  151. (list (%installer-configuration-file)
  152. (%installer-target-dir))))
  153. (database-dir "/var/guix/db")
  154. (database-file (string-append database-dir "/db.sqlite"))
  155. (saved-database (string-append database-dir "/db.save"))
  156. (ret #f))
  157. (mkdir-p (%installer-target-dir))
  158. ;; We want to initialize user passwords but we don't want to store them in
  159. ;; the config file since the password hashes would end up world-readable
  160. ;; in the store. Thus, create /etc/shadow & co. here such that, on the
  161. ;; first boot, the activation snippet that creates accounts will reuse the
  162. ;; passwords that we've put in there.
  163. (create-user-database users (%installer-target-dir))
  164. ;; When the store overlay is mounted, other processes such as kmscon, udev
  165. ;; and guix-daemon may open files from the store, preventing the
  166. ;; underlying install support from being umounted. See:
  167. ;; https://lists.gnu.org/archive/html/guix-devel/2018-12/msg00161.html.
  168. ;;
  169. ;; To avoid this situation, mount the store overlay inside a container,
  170. ;; and run the installation from within that container.
  171. (zero?
  172. (call-with-mnt-container
  173. (lambda ()
  174. (dynamic-wind
  175. (lambda ()
  176. ;; Install the locale before mounting the cow-store, otherwise
  177. ;; the loaded cow-store locale files will prevent umounting.
  178. (install-locale locale)
  179. ;; Save the database, so that it can be restored once the
  180. ;; cow-store is umounted.
  181. (copy-file database-file saved-database)
  182. (mount-cow-store (%installer-target-dir) backing-directory))
  183. (lambda ()
  184. ;; We need to drag the guix-daemon to the container MNT
  185. ;; namespace, so that it can operate on the cow-store.
  186. (stop-service 'guix-daemon)
  187. (start-service 'guix-daemon (list (number->string (getpid))))
  188. (setvbuf (current-output-port) 'none)
  189. (setvbuf (current-error-port) 'none)
  190. (setenv "PATH" "/run/current-system/profile/bin/")
  191. (set! ret (run-command install-command)))
  192. (lambda ()
  193. ;; Restart guix-daemon so that it does no keep the MNT namespace
  194. ;; alive.
  195. (restart-service 'guix-daemon)
  196. (copy-file saved-database database-file)
  197. ;; Finally umount the cow-store and exit the container.
  198. (unmount-cow-store (%installer-target-dir) backing-directory)
  199. (assert-exit ret))))))))