final.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2019, 2020, 2022 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. ;: FIXME: Use of SIGKILL prevents the dynamic-wind exit handler of
  106. ;; THUNK to run.
  107. (false-if-exception
  108. (kill pid SIGKILL))))
  109. (match (waitpid pid)
  110. ((_ . status) status))))
  111. (define (install-locale locale)
  112. "Install the given LOCALE or the en_US.utf8 locale as a fallback."
  113. (let ((supported? (false-if-exception
  114. (setlocale LC_ALL locale))))
  115. (if supported?
  116. (begin
  117. (installer-log-line "install supported locale ~a." locale)
  118. (setenv "LC_ALL" locale))
  119. (begin
  120. ;; If the selected locale is not supported, install a default UTF-8
  121. ;; locale. This is required to copy some files with UTF-8
  122. ;; characters, in the nss-certs package notably. Set LANGUAGE
  123. ;; anyways, to have translated messages if possible.
  124. (installer-log-line "~a locale is not supported, installing \
  125. en_US.utf8 locale instead." locale)
  126. (setlocale LC_ALL "en_US.utf8")
  127. (setenv "LC_ALL" "en_US.utf8")
  128. (setenv "LANGUAGE"
  129. (string-take locale
  130. (or (string-index locale #\_)
  131. (string-length locale))))))))
  132. (define* (install-system locale #:key (users '()))
  133. "Create /etc/shadow and /etc/passwd on the installation target for USERS.
  134. Start COW-STORE service on target directory and launch guix install command in
  135. a subshell. LOCALE must be the locale name under which that command will run,
  136. or #f. Return #t on success and #f on failure."
  137. (define backing-directory
  138. ;; Sub-directory used as the backing store for copy-on-write.
  139. "/tmp/guix-inst")
  140. (define (assert-exit x)
  141. (primitive-exit (if x 0 1)))
  142. (let* ((options (catch 'system-error
  143. (lambda ()
  144. ;; If this file exists, it can provide
  145. ;; additional command-line options.
  146. (call-with-input-file
  147. "/tmp/installer-system-init-options"
  148. read))
  149. (const '())))
  150. (install-command (append (list "guix" "system" "init"
  151. "--fallback")
  152. options
  153. (list (%installer-configuration-file)
  154. (%installer-target-dir))))
  155. (database-dir "/var/guix/db")
  156. (database-file (string-append database-dir "/db.sqlite"))
  157. (saved-database (string-append database-dir "/db.save"))
  158. (ret #f))
  159. (mkdir-p (%installer-target-dir))
  160. ;; We want to initialize user passwords but we don't want to store them in
  161. ;; the config file since the password hashes would end up world-readable
  162. ;; in the store. Thus, create /etc/shadow & co. here such that, on the
  163. ;; first boot, the activation snippet that creates accounts will reuse the
  164. ;; passwords that we've put in there.
  165. (create-user-database users (%installer-target-dir))
  166. ;; When the store overlay is mounted, other processes such as kmscon, udev
  167. ;; and guix-daemon may open files from the store, preventing the
  168. ;; underlying install support from being umounted. See:
  169. ;; https://lists.gnu.org/archive/html/guix-devel/2018-12/msg00161.html.
  170. ;;
  171. ;; To avoid this situation, mount the store overlay inside a container,
  172. ;; and run the installation from within that container.
  173. (zero?
  174. (call-with-mnt-container
  175. (lambda ()
  176. (dynamic-wind
  177. (lambda ()
  178. ;; Install the locale before mounting the cow-store, otherwise
  179. ;; the loaded cow-store locale files will prevent umounting.
  180. (install-locale locale)
  181. ;; Stop the daemon and save the database, so that it can be
  182. ;; restored once the cow-store is umounted.
  183. (stop-service 'guix-daemon)
  184. (copy-file database-file saved-database)
  185. (installer-log-line "mounting copy-on-write store")
  186. (mount-cow-store (%installer-target-dir) backing-directory))
  187. (lambda ()
  188. ;; We need to drag the guix-daemon to the container MNT
  189. ;; namespace, so that it can operate on the cow-store.
  190. (start-service 'guix-daemon (list (number->string (getpid))))
  191. (setvbuf (current-output-port) 'none)
  192. (setvbuf (current-error-port) 'none)
  193. (setenv "PATH" "/run/current-system/profile/bin/")
  194. (set! ret (run-command install-command #:tty? #t)))
  195. (lambda ()
  196. ;; Stop guix-daemon so that it does no keep the MNT namespace
  197. ;; alive.
  198. (stop-service 'guix-daemon)
  199. ;; Restore the database and restart it. As part of restoring the
  200. ;; database, remove the WAL and shm files in case they were left
  201. ;; behind after guix-daemon was stopped. Failing to do so,
  202. ;; sqlite might behave as if transactions that appear in the WAL
  203. ;; file were committed. (See <https://www.sqlite.org/wal.html>.)
  204. (installer-log-line "restoring store database from '~a'"
  205. saved-database)
  206. (copy-file saved-database database-file)
  207. (for-each (lambda (suffix)
  208. (false-if-exception
  209. (delete-file (string-append database-file suffix))))
  210. '("-wal" "-shm"))
  211. (start-service 'guix-daemon)
  212. ;; Finally umount the cow-store and exit the container.
  213. (installer-log-line "unmounting copy-on-write store")
  214. (unmount-cow-store (%installer-target-dir) backing-directory)
  215. (assert-exit ret))))))))