final.scm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 (user-password user)
  81. (salt)))
  82. ;; We need a string here, not a file-like, hence
  83. ;; this choice.
  84. (shell
  85. "/run/current-system/profile/bin/bash")))
  86. users))
  87. (define-values (group password shadow)
  88. (user+group-databases users* sys:%base-groups
  89. #:current-passwd '()
  90. #:current-groups '()
  91. #:current-shadow '()))
  92. (mkdir-p etc)
  93. (write-group group (string-append etc "/group"))
  94. (write-passwd password (string-append etc "/passwd"))
  95. (write-shadow shadow (string-append etc "/shadow")))
  96. (define* (kill-cow-users cow-path #:key (spare '("udevd")))
  97. "Kill all processes that have references to the given COW-PATH in their
  98. 'maps' file. The process whose names are in SPARE list are spared."
  99. (define %not-nul
  100. (char-set-complement (char-set #\nul)))
  101. (let ((pids
  102. (filter-map (lambda (pid)
  103. (false-if-exception
  104. (call-with-input-file
  105. (string-append "/proc/" pid "/maps")
  106. (lambda (port)
  107. (and (string-contains (get-string-all port)
  108. cow-path)
  109. (string->number pid))))))
  110. (scandir "/proc" string->number))))
  111. (for-each (lambda (pid)
  112. ;; cmdline does not always exist.
  113. (false-if-exception
  114. (call-with-input-file
  115. (string-append "/proc/" (number->string pid) "/cmdline")
  116. (lambda (port)
  117. (match (string-tokenize (read-string port) %not-nul)
  118. ((argv0 _ ...)
  119. (unless (member (basename argv0) spare)
  120. (syslog "Killing process ~a (~a)~%" pid argv0)
  121. (kill pid SIGKILL)))
  122. (_ #f))))))
  123. pids)))
  124. (define (call-with-mnt-container thunk)
  125. "This is a variant of call-with-container. Run THUNK in a new container
  126. process, within a separate MNT namespace. The container is not jailed so that
  127. it can interact with the rest of the system."
  128. (let ((pid (run-container "/" '() '(mnt) 1 thunk)))
  129. ;; Catch SIGINT and kill the container process.
  130. (sigaction SIGINT
  131. (lambda (signum)
  132. (false-if-exception
  133. (kill pid SIGKILL))))
  134. (match (waitpid pid)
  135. ((_ . status) status))))
  136. (define* (install-system locale #:key (users '()))
  137. "Create /etc/shadow and /etc/passwd on the installation target for USERS.
  138. Start COW-STORE service on target directory and launch guix install command in
  139. a subshell. LOCALE must be the locale name under which that command will run,
  140. or #f. Return #t on success and #f on failure."
  141. (define backing-directory
  142. ;; Sub-directory used as the backing store for copy-on-write.
  143. "/tmp/guix-inst")
  144. (define (assert-exit x)
  145. (primitive-exit (if x 0 1)))
  146. (let* ((options (catch 'system-error
  147. (lambda ()
  148. ;; If this file exists, it can provide
  149. ;; additional command-line options.
  150. (call-with-input-file
  151. "/tmp/installer-system-init-options"
  152. read))
  153. (const '())))
  154. (install-command (append (list "guix" "system" "init"
  155. "--fallback")
  156. options
  157. (list (%installer-configuration-file)
  158. (%installer-target-dir))))
  159. (database-dir "/var/guix/db")
  160. (database-file (string-append database-dir "/db.sqlite"))
  161. (saved-database (string-append database-dir "/db.save"))
  162. (ret #f))
  163. (mkdir-p (%installer-target-dir))
  164. ;; We want to initialize user passwords but we don't want to store them in
  165. ;; the config file since the password hashes would end up world-readable
  166. ;; in the store. Thus, create /etc/shadow & co. here such that, on the
  167. ;; first boot, the activation snippet that creates accounts will reuse the
  168. ;; passwords that we've put in there.
  169. (create-user-database users (%installer-target-dir))
  170. ;; When the store overlay is mounted, other processes such as kmscon, udev
  171. ;; and guix-daemon may open files from the store, preventing the
  172. ;; underlying install support from being umounted. See:
  173. ;; https://lists.gnu.org/archive/html/guix-devel/2018-12/msg00161.html.
  174. ;;
  175. ;; To avoid this situation, mount the store overlay inside a container,
  176. ;; and run the installation from within that container.
  177. (zero?
  178. (call-with-mnt-container
  179. (lambda ()
  180. (dynamic-wind
  181. (lambda ()
  182. ;; Save the database, so that it can be restored once the
  183. ;; cow-store is umounted.
  184. (copy-file database-file saved-database)
  185. (mount-cow-store (%installer-target-dir) backing-directory))
  186. (lambda ()
  187. ;; We need to drag the guix-daemon to the container MNT
  188. ;; namespace, so that it can operate on the cow-store.
  189. (stop-service 'guix-daemon)
  190. (start-service 'guix-daemon (list (number->string (getpid))))
  191. (setvbuf (current-output-port) 'none)
  192. (setvbuf (current-error-port) 'none)
  193. ;; If there are any connected clients, assume that we are running
  194. ;; installation tests. In that case, dump the standard and error
  195. ;; outputs to syslog.
  196. (set! ret
  197. (if (not (null? (current-clients)))
  198. (with-output-to-file "/dev/console"
  199. (lambda ()
  200. (with-error-to-file "/dev/console"
  201. (lambda ()
  202. (run-command install-command
  203. #:locale locale)))))
  204. (run-command install-command #:locale locale))))
  205. (lambda ()
  206. ;; Restart guix-daemon so that it does no keep the MNT namespace
  207. ;; alive.
  208. (restart-service 'guix-daemon)
  209. (copy-file saved-database database-file)
  210. ;; Finally umount the cow-store and exit the container.
  211. (unmount-cow-store (%installer-target-dir) backing-directory)
  212. (assert-exit ret))))))))