You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1231 lines
49 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
  4. ;;; Copyright © 2016, 2017, 2018 Chris Marusich <cmmarusich@gmail.com>
  5. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix scripts system)
  22. #:use-module (guix config)
  23. #:use-module (guix ui)
  24. #:use-module (guix store)
  25. #:autoload (guix store database) (register-path)
  26. #:use-module (guix grafts)
  27. #:use-module (guix gexp)
  28. #:use-module (guix derivations)
  29. #:use-module (guix packages)
  30. #:use-module (guix utils)
  31. #:use-module (guix monads)
  32. #:use-module (guix records)
  33. #:use-module (guix profiles)
  34. #:use-module (guix scripts)
  35. #:use-module (guix scripts build)
  36. #:use-module (guix graph)
  37. #:use-module (guix scripts graph)
  38. #:use-module (guix build utils)
  39. #:use-module (guix progress)
  40. #:use-module ((guix build syscalls) #:select (terminal-columns))
  41. #:use-module (gnu build install)
  42. #:autoload (gnu build file-systems)
  43. (find-partition-by-label find-partition-by-uuid)
  44. #:autoload (gnu build linux-modules)
  45. (device-module-aliases matching-modules)
  46. #:use-module (gnu system linux-initrd)
  47. #:use-module (gnu system)
  48. #:use-module (gnu bootloader)
  49. #:use-module (gnu system file-systems)
  50. #:use-module (gnu system mapped-devices)
  51. #:use-module (gnu system linux-container)
  52. #:use-module (gnu system uuid)
  53. #:use-module (gnu system vm)
  54. #:use-module (gnu services)
  55. #:use-module (gnu services shepherd)
  56. #:use-module (gnu services herd)
  57. #:use-module (srfi srfi-1)
  58. #:use-module (srfi srfi-11)
  59. #:use-module (srfi srfi-19)
  60. #:use-module (srfi srfi-26)
  61. #:use-module (srfi srfi-34)
  62. #:use-module (srfi srfi-35)
  63. #:use-module (srfi srfi-37)
  64. #:use-module (ice-9 match)
  65. #:use-module (rnrs bytevectors)
  66. #:export (guix-system
  67. read-operating-system))
  68. ;;;
  69. ;;; Operating system declaration.
  70. ;;;
  71. (define %user-module
  72. ;; Module in which the machine description file is loaded.
  73. (make-user-module '((gnu system)
  74. (gnu services)
  75. (gnu system shadow))))
  76. (define (read-operating-system file)
  77. "Read the operating-system declaration from FILE and return it."
  78. (load* file %user-module))
  79. ;;;
  80. ;;; Installation.
  81. ;;;
  82. (define-syntax-rule (save-load-path-excursion body ...)
  83. "Save the current values of '%load-path' and '%load-compiled-path', run
  84. BODY..., and restore them."
  85. (let ((path %load-path)
  86. (cpath %load-compiled-path))
  87. (dynamic-wind
  88. (const #t)
  89. (lambda ()
  90. body ...)
  91. (lambda ()
  92. (set! %load-path path)
  93. (set! %load-compiled-path cpath)))))
  94. (define-syntax-rule (save-environment-excursion body ...)
  95. "Save the current environment variables, run BODY..., and restore them."
  96. (let ((env (environ)))
  97. (dynamic-wind
  98. (const #t)
  99. (lambda ()
  100. body ...)
  101. (lambda ()
  102. (environ env)))))
  103. (define topologically-sorted*
  104. (store-lift topologically-sorted))
  105. (define* (copy-item item references target
  106. #:key (log-port (current-error-port)))
  107. "Copy ITEM to the store under root directory TARGET and register it with
  108. REFERENCES as its set of references."
  109. (let ((dest (string-append target item))
  110. (state (string-append target "/var/guix")))
  111. (format log-port "copying '~a'...~%" item)
  112. ;; Remove DEST if it exists to make sure that (1) we do not fail badly
  113. ;; while trying to overwrite it (see <http://bugs.gnu.org/20722>), and
  114. ;; (2) we end up with the right contents.
  115. (when (file-exists? dest)
  116. (delete-file-recursively dest))
  117. (copy-recursively item dest
  118. #:log (%make-void-port "w"))
  119. ;; Register ITEM; as a side-effect, it resets timestamps, etc.
  120. ;; Explicitly use "TARGET/var/guix" as the state directory, to avoid
  121. ;; reproducing the user's current settings; see
  122. ;; <http://bugs.gnu.org/18049>.
  123. (unless (register-path item
  124. #:prefix target
  125. #:state-directory state
  126. #:references references)
  127. (leave (G_ "failed to register '~a' under '~a'~%")
  128. item target))))
  129. (define* (copy-closure item target
  130. #:key (log-port (current-error-port)))
  131. "Copy ITEM and all its dependencies to the store under root directory
  132. TARGET, and register them."
  133. (mlet* %store-monad ((to-copy (topologically-sorted* (list item)))
  134. (refs (mapm %store-monad references* to-copy)))
  135. (define progress-bar
  136. (progress-reporter/bar (length to-copy)
  137. (format #f (G_ "copying to '~a'...")
  138. target)))
  139. (call-with-progress-reporter progress-bar
  140. (lambda (report)
  141. (let ((void (%make-void-port "w")))
  142. (for-each (lambda (item refs)
  143. (copy-item item refs target #:log-port void)
  144. (report))
  145. to-copy refs))))
  146. (return *unspecified*)))
  147. (define* (install-bootloader installer-drv
  148. #:key
  149. bootcfg bootcfg-file
  150. target)
  151. "Call INSTALLER-DRV with error handling, in %STORE-MONAD."
  152. (with-monad %store-monad
  153. (let* ((gc-root (string-append target %gc-roots-directory
  154. "/bootcfg"))
  155. (temp-gc-root (string-append gc-root ".new"))
  156. (install (and installer-drv
  157. (derivation->output-path installer-drv)))
  158. (bootcfg (derivation->output-path bootcfg)))
  159. ;; Prepare the symlink to bootloader config file to make sure that it's
  160. ;; a GC root when 'installer-drv' completes (being a bit paranoid.)
  161. (switch-symlinks temp-gc-root bootcfg)
  162. (unless (false-if-exception
  163. (begin
  164. (install-boot-config bootcfg bootcfg-file target)
  165. (when install
  166. (save-load-path-excursion (primitive-load install)))))
  167. (delete-file temp-gc-root)
  168. (leave (G_ "failed to install bootloader ~a~%") install))
  169. ;; Register bootloader config file as a GC root so that its dependencies
  170. ;; (background image, font, etc.) are not reclaimed.
  171. (rename-file temp-gc-root gc-root)
  172. (return #t))))
  173. (define* (install os-drv target
  174. #:key (log-port (current-output-port))
  175. bootloader-installer install-bootloader?
  176. bootcfg bootcfg-file)
  177. "Copy the closure of BOOTCFG, which includes the output of OS-DRV, to
  178. directory TARGET. TARGET must be an absolute directory name since that's what
  179. 'register-path' expects.
  180. When INSTALL-BOOTLOADER? is true, install bootloader using BOOTCFG."
  181. (define (maybe-copy to-copy)
  182. (with-monad %store-monad
  183. (if (string=? target "/")
  184. (begin
  185. (warning (G_ "initializing the current root file system~%"))
  186. (return #t))
  187. (begin
  188. ;; Make sure the target store exists.
  189. (mkdir-p (string-append target (%store-prefix)))
  190. ;; Copy items to the new store.
  191. (copy-closure to-copy target #:log-port log-port)))))
  192. ;; Make sure TARGET is root-owned when running as root, but still allow
  193. ;; non-root uses (useful for testing.) See
  194. ;; <http://lists.gnu.org/archive/html/guix-devel/2015-05/msg00452.html>.
  195. (if (zero? (geteuid))
  196. (chown target 0 0)
  197. (warning (G_ "not running as 'root', so \
  198. the ownership of '~a' may be incorrect!~%")
  199. target))
  200. (chmod target #o755)
  201. (let ((os-dir (derivation->output-path os-drv))
  202. (format (lift format %store-monad))
  203. (populate (lift2 populate-root-file-system %store-monad)))
  204. (mbegin %store-monad
  205. ;; Copy the closure of BOOTCFG, which includes OS-DIR,
  206. ;; eventual background image and so on.
  207. (maybe-copy
  208. (derivation->output-path bootcfg))
  209. ;; Create a bunch of additional files.
  210. (format log-port "populating '~a'...~%" target)
  211. (populate os-dir target)
  212. (mwhen install-bootloader?
  213. (install-bootloader bootloader-installer
  214. #:bootcfg bootcfg
  215. #:bootcfg-file bootcfg-file
  216. #:target target)))))
  217. ;;;
  218. ;;; Reconfiguration.
  219. ;;;
  220. (define %system-profile
  221. ;; The system profile.
  222. (string-append %state-directory "/profiles/system"))
  223. (define-syntax-rule (with-shepherd-error-handling mbody ...)
  224. "Catch and report Shepherd errors that arise when binding MBODY, a monadic
  225. expression in %STORE-MONAD."
  226. (lambda (store)
  227. (catch 'system-error
  228. (lambda ()
  229. (guard (c ((shepherd-error? c)
  230. (values (report-shepherd-error c) store)))
  231. (values (run-with-store store (begin mbody ...))
  232. store)))
  233. (lambda (key proc format-string format-args errno . rest)
  234. (warning (G_ "while talking to shepherd: ~a~%")
  235. (apply format #f format-string format-args))
  236. (values #f store)))))
  237. (define (report-shepherd-error error)
  238. "Report ERROR, a '&shepherd-error' error condition object."
  239. (cond ((service-not-found-error? error)
  240. (report-error (G_ "service '~a' could not be found~%")
  241. (service-not-found-error-service error)))
  242. ((action-not-found-error? error)
  243. (report-error (G_ "service '~a' does not have an action '~a'~%")
  244. (action-not-found-error-service error)
  245. (action-not-found-error-action error)))
  246. ((action-exception-error? error)
  247. (report-error (G_ "exception caught while executing '~a' \
  248. on service '~a':~%")
  249. (action-exception-error-action error)
  250. (action-exception-error-service error))
  251. (print-exception (current-error-port) #f
  252. (action-exception-error-key error)
  253. (action-exception-error-arguments error)))
  254. ((unknown-shepherd-error? error)
  255. (report-error (G_ "something went wrong: ~s~%")
  256. (unknown-shepherd-error-sexp error)))
  257. ((shepherd-error? error)
  258. (report-error (G_ "shepherd error~%")))
  259. ((not error) ;not an error
  260. #t)))
  261. (define (call-with-service-upgrade-info new-services mproc)
  262. "Call MPROC, a monadic procedure in %STORE-MONAD, passing it the list of
  263. names of services to load (upgrade), and the list of names of services to
  264. unload."
  265. (match (current-services)
  266. ((services ...)
  267. (let-values (((to-unload to-load)
  268. (shepherd-service-upgrade services new-services)))
  269. (mproc to-load
  270. (map (compose first live-service-provision)
  271. to-unload))))
  272. (#f
  273. (with-monad %store-monad
  274. (warning (G_ "failed to obtain list of shepherd services~%"))
  275. (return #f)))))
  276. (define (upgrade-shepherd-services os)
  277. "Upgrade the Shepherd (PID 1) by unloading obsolete services and loading new
  278. services specified in OS and not currently running.
  279. This is currently very conservative in that it does not stop or unload any
  280. running service. Unloading or stopping the wrong service ('udev', say) could
  281. bring the system down."
  282. (define new-services
  283. (service-value
  284. (fold-services (operating-system-services os)
  285. #:target-type shepherd-root-service-type)))
  286. ;; Arrange to simply emit a warning if the service upgrade fails.
  287. (with-shepherd-error-handling
  288. (call-with-service-upgrade-info new-services
  289. (lambda (to-load to-unload)
  290. (for-each (lambda (unload)
  291. (info (G_ "unloading service '~a'...~%") unload)
  292. (unload-service unload))
  293. to-unload)
  294. (with-monad %store-monad
  295. (munless (null? to-load)
  296. (let ((to-load-names (map shepherd-service-canonical-name to-load))
  297. (to-start (filter shepherd-service-auto-start? to-load)))
  298. (info (G_ "loading new services:~{ ~a~}...~%") to-load-names)
  299. (mlet %store-monad ((files (mapm %store-monad
  300. (compose lower-object
  301. shepherd-service-file)
  302. to-load)))
  303. ;; Here we assume that FILES are exactly those that were computed
  304. ;; as part of the derivation that built OS, which is normally the
  305. ;; case.
  306. (load-services (map derivation->output-path files))
  307. (for-each start-service
  308. (map shepherd-service-canonical-name to-start))
  309. (return #t)))))))))
  310. (define* (switch-to-system os
  311. #:optional (profile %system-profile))
  312. "Make a new generation of PROFILE pointing to the directory of OS, switch to
  313. it atomically, and then run OS's activation script."
  314. (mlet* %store-monad ((drv (operating-system-derivation os))
  315. (script (operating-system-activation-script os)))
  316. (let* ((system (derivation->output-path drv))
  317. (number (+ 1 (generation-number profile)))
  318. (generation (generation-file-name profile number)))
  319. (switch-symlinks generation system)
  320. (switch-symlinks profile generation)
  321. (format #t (G_ "activating system...~%"))
  322. ;; The activation script may change $PATH, among others, so protect
  323. ;; against that.
  324. (save-environment-excursion
  325. ;; Tell 'activate-current-system' what the new system is.
  326. (setenv "GUIX_NEW_SYSTEM" system)
  327. ;; The activation script may modify '%load-path' & co., so protect
  328. ;; against that. This is necessary to ensure that
  329. ;; 'upgrade-shepherd-services' gets to see the right modules when it
  330. ;; computes derivations with 'gexp->derivation'.
  331. (save-load-path-excursion
  332. (primitive-load (derivation->output-path script))))
  333. ;; Finally, try to update system services.
  334. (upgrade-shepherd-services os))))
  335. (define-syntax-rule (unless-file-not-found exp)
  336. (catch 'system-error
  337. (lambda ()
  338. exp)
  339. (lambda args
  340. (if (= ENOENT (system-error-errno args))
  341. #f
  342. (apply throw args)))))
  343. (define (seconds->string seconds)
  344. "Return a string representing the date for SECONDS."
  345. (let ((time (make-time time-utc 0 seconds)))
  346. (date->string (time-utc->date time)
  347. "~Y-~m-~d ~H:~M")))
  348. (define* (profile-boot-parameters #:optional (profile %system-profile)
  349. (numbers
  350. (reverse (generation-numbers profile))))
  351. "Return a list of 'boot-parameters' for the generations of PROFILE specified
  352. by NUMBERS, which is a list of generation numbers. The list is ordered from
  353. the most recent to the oldest profiles."
  354. (define (system->boot-parameters system number time)
  355. (unless-file-not-found
  356. (let* ((params (read-boot-parameters-file system))
  357. (label (boot-parameters-label params)))
  358. (boot-parameters
  359. (inherit params)
  360. (label (string-append label " (#"
  361. (number->string number) ", "
  362. (seconds->string time) ")"))))))
  363. (let* ((systems (map (cut generation-file-name profile <>)
  364. numbers))
  365. (times (map (lambda (system)
  366. (unless-file-not-found
  367. (stat:mtime (lstat system))))
  368. systems)))
  369. (filter-map system->boot-parameters systems numbers times)))
  370. ;;;
  371. ;;; Roll-back.
  372. ;;;
  373. (define (roll-back-system store)
  374. "Roll back the system profile to its previous generation. STORE is an open
  375. connection to the store."
  376. (switch-to-system-generation store "-1"))
  377. ;;;
  378. ;;; Switch generations.
  379. ;;;
  380. (define (switch-to-system-generation store spec)
  381. "Switch the system profile to the generation specified by SPEC, and
  382. re-install bootloader with a configuration file that uses the specified system
  383. generation as its default entry. STORE is an open connection to the store."
  384. (let ((number (relative-generation-spec->number %system-profile spec)))
  385. (if number
  386. (begin
  387. (reinstall-bootloader store number)
  388. (switch-to-generation* %system-profile number))
  389. (leave (G_ "cannot switch to system generation '~a'~%") spec))))
  390. (define* (system-bootloader-name #:optional (system %system-profile))
  391. "Return the bootloader name stored in SYSTEM's \"parameters\" file."
  392. (let ((params (unless-file-not-found
  393. (read-boot-parameters-file system))))
  394. (boot-parameters-bootloader-name params)))
  395. (define (reinstall-bootloader store number)
  396. "Re-install bootloader for existing system profile generation NUMBER.
  397. STORE is an open connection to the store."
  398. (let* ((generation (generation-file-name %system-profile number))
  399. ;; Detect the bootloader used in %system-profile.
  400. (bootloader (lookup-bootloader-by-name (system-bootloader-name)))
  401. ;; Use the detected bootloader with default configuration.
  402. ;; It will be enough to allow the system to boot.
  403. (bootloader-config (bootloader-configuration
  404. (bootloader bootloader)))
  405. ;; Make the specified system generation the default entry.
  406. (params (profile-boot-parameters %system-profile (list number)))
  407. (old-generations (delv number (generation-numbers %system-profile)))
  408. (old-params (profile-boot-parameters
  409. %system-profile old-generations))
  410. (entries (map boot-parameters->menu-entry params))
  411. (old-entries (map boot-parameters->menu-entry old-params)))
  412. (run-with-store store
  413. (mlet* %store-monad
  414. ((bootcfg ((bootloader-configuration-file-generator bootloader)
  415. bootloader-config entries
  416. #:old-entries old-entries))
  417. (bootcfg-file -> (bootloader-configuration-file bootloader))
  418. (target -> "/")
  419. (drvs -> (list bootcfg)))
  420. (mbegin %store-monad
  421. (show-what-to-build* drvs)
  422. (built-derivations drvs)
  423. ;; Only install bootloader configuration file. Thus, no installer is
  424. ;; provided here.
  425. (install-bootloader #f
  426. #:bootcfg bootcfg
  427. #:bootcfg-file bootcfg-file
  428. #:target target))))))
  429. ;;;
  430. ;;; Graphs.
  431. ;;;
  432. (define (service-node-label service)
  433. "Return a label to represent SERVICE."
  434. (let ((type (service-kind service))
  435. (value (service-value service)))
  436. (string-append (symbol->string (service-type-name type))
  437. (cond ((or (number? value) (symbol? value))
  438. (string-append " " (object->string value)))
  439. ((string? value)
  440. (string-append " " value))
  441. ((file-system? value)
  442. (string-append " " (file-system-mount-point value)))
  443. (else
  444. "")))))
  445. (define (service-node-type services)
  446. "Return a node type for SERVICES. Since <service> instances are not
  447. self-contained (they express dependencies on service types, not on services),
  448. we have to create the 'edges' procedure dynamically as a function of the full
  449. list of services."
  450. (node-type
  451. (name "service")
  452. (description "the DAG of services")
  453. (identifier (lift1 object-address %store-monad))
  454. (label service-node-label)
  455. (edges (lift1 (service-back-edges services) %store-monad))))
  456. (define (shepherd-service-node-label service)
  457. "Return a label for a node representing a <shepherd-service>."
  458. (string-join (map symbol->string (shepherd-service-provision service))))
  459. (define (shepherd-service-node-type services)
  460. "Return a node type for SERVICES, a list of <shepherd-service>."
  461. (node-type
  462. (name "shepherd-service")
  463. (description "the dependency graph of shepherd services")
  464. (identifier (lift1 shepherd-service-node-label %store-monad))
  465. (label shepherd-service-node-label)
  466. (edges (lift1 (shepherd-service-back-edges services) %store-monad))))
  467. ;;;
  468. ;;; Generations.
  469. ;;;
  470. (define* (display-system-generation number
  471. #:optional (profile %system-profile))
  472. "Display a summary of system generation NUMBER in a human-readable format."
  473. (unless (zero? number)
  474. (let* ((generation (generation-file-name profile number))
  475. (params (read-boot-parameters-file generation))
  476. (label (boot-parameters-label params))
  477. (bootloader-name (boot-parameters-bootloader-name params))
  478. (root (boot-parameters-root-device params))
  479. (root-device (if (bytevector? root)
  480. (uuid->string root)
  481. root))
  482. (kernel (boot-parameters-kernel params)))
  483. (display-generation profile number)
  484. (format #t (G_ " file name: ~a~%") generation)
  485. (format #t (G_ " canonical file name: ~a~%") (readlink* generation))
  486. ;; TRANSLATORS: Please preserve the two-space indentation.
  487. (format #t (G_ " label: ~a~%") label)
  488. (format #t (G_ " bootloader: ~a~%") bootloader-name)
  489. (format #t (G_ " root device: ~a~%")
  490. (if (uuid? root-device)
  491. (uuid->string root-device)
  492. root-device))
  493. (format #t (G_ " kernel: ~a~%") kernel))))
  494. (define* (list-generations pattern #:optional (profile %system-profile))
  495. "Display in a human-readable format all the system generations matching
  496. PATTERN, a string. When PATTERN is #f, display all the system generations."
  497. (cond ((not (file-exists? profile)) ; XXX: race condition
  498. (raise (condition (&profile-not-found-error
  499. (profile profile)))))
  500. ((string-null? pattern)
  501. (for-each display-system-generation (profile-generations profile)))
  502. ((matching-generations pattern profile)
  503. =>
  504. (lambda (numbers)
  505. (if (null-list? numbers)
  506. (exit 1)
  507. (leave-on-EPIPE
  508. (for-each display-system-generation numbers)))))
  509. (else
  510. (leave (G_ "invalid syntax: ~a~%") pattern))))
  511. ;;;
  512. ;;; File system declaration checks.
  513. ;;;
  514. (define (check-file-system-availability file-systems)
  515. "Check whether the UUIDs or partition labels that FILE-SYSTEMS refer to, if
  516. any, are available. Raise an error if they're not."
  517. (define relevant
  518. (filter (lambda (fs)
  519. (and (file-system-mount? fs)
  520. (not (member (file-system-type fs)
  521. %pseudo-file-system-types))
  522. (not (memq 'bind-mount (file-system-flags fs)))))
  523. file-systems))
  524. (define labeled
  525. (filter (lambda (fs)
  526. (file-system-label? (file-system-device fs)))
  527. relevant))
  528. (define literal
  529. (filter (lambda (fs)
  530. (string? (file-system-device fs)))
  531. relevant))
  532. (define uuid
  533. (filter (lambda (fs)
  534. (uuid? (file-system-device fs)))
  535. relevant))
  536. (define fail? #f)
  537. (define (file-system-location* fs)
  538. (location->string
  539. (source-properties->location
  540. (file-system-location fs))))
  541. (let-syntax ((error (syntax-rules ()
  542. ((_ args ...)
  543. (begin
  544. (set! fail? #t)
  545. (format (current-error-port)
  546. args ...))))))
  547. (for-each (lambda (fs)
  548. (catch 'system-error
  549. (lambda ()
  550. (stat (file-system-device fs)))
  551. (lambda args
  552. (let ((errno (system-error-errno args))
  553. (device (file-system-device fs)))
  554. (error (G_ "~a: error: device '~a' not found: ~a~%")
  555. (file-system-location* fs) device
  556. (strerror errno))
  557. (unless (string-prefix? "/" device)
  558. (display-hint (format #f (G_ "If '~a' is a file system
  559. label, write @code{(file-system-label ~s)} in your @code{device} field.")
  560. device device)))))))
  561. literal)
  562. (for-each (lambda (fs)
  563. (let ((label (file-system-label->string
  564. (file-system-device fs))))
  565. (unless (find-partition-by-label label)
  566. (error (G_ "~a: error: file system with label '~a' not found~%")
  567. (file-system-location* fs) label))))
  568. labeled)
  569. (for-each (lambda (fs)
  570. (unless (find-partition-by-uuid (file-system-device fs))
  571. (error (G_ "~a: error: file system with UUID '~a' not found~%")
  572. (file-system-location* fs)
  573. (uuid->string (file-system-device fs)))))
  574. uuid)
  575. (when fail?
  576. ;; Better be safe than sorry.
  577. (exit 1))))
  578. (define (check-mapped-devices os)
  579. "Check that each of MAPPED-DEVICES is valid according to the 'check'
  580. procedure of its type."
  581. (define boot-mapped-devices
  582. (operating-system-boot-mapped-devices os))
  583. (define (needed-for-boot? md)
  584. (memq md boot-mapped-devices))
  585. (define initrd-modules
  586. (operating-system-initrd-modules os))
  587. (for-each (lambda (md)
  588. (let ((check (mapped-device-kind-check
  589. (mapped-device-type md))))
  590. ;; We expect CHECK to raise an exception with a detailed
  591. ;; '&message' if something goes wrong.
  592. (check md
  593. #:needed-for-boot? (needed-for-boot? md)
  594. #:initrd-modules initrd-modules)))
  595. (operating-system-mapped-devices os)))
  596. (define (check-initrd-modules os)
  597. "Check that modules needed by 'needed-for-boot' file systems in OS are
  598. available in the initrd. Note that mapped devices are responsible for
  599. checking this by themselves in their 'check' procedure."
  600. (define (file-system-/dev fs)
  601. (let ((device (file-system-device fs)))
  602. (match device
  603. ((? string?)
  604. device)
  605. ((? uuid?)
  606. (find-partition-by-uuid device))
  607. ((? file-system-label?)
  608. (find-partition-by-label (file-system-label->string device))))))
  609. (define file-systems
  610. (filter file-system-needed-for-boot?
  611. (operating-system-file-systems os)))
  612. (for-each (lambda (fs)
  613. (check-device-initrd-modules (file-system-/dev fs)
  614. (operating-system-initrd-modules os)
  615. (source-properties->location
  616. (file-system-location fs))))
  617. file-systems))
  618. ;;;
  619. ;;; Action.
  620. ;;;
  621. (define* (system-derivation-for-action os action
  622. #:key image-size file-system-type
  623. full-boot? mappings)
  624. "Return as a monadic value the derivation for OS according to ACTION."
  625. (case action
  626. ((build init reconfigure)
  627. (operating-system-derivation os))
  628. ((container)
  629. (container-script os #:mappings mappings))
  630. ((vm-image)
  631. (system-qemu-image os #:disk-image-size image-size))
  632. ((vm)
  633. (system-qemu-image/shared-store-script os
  634. #:full-boot? full-boot?
  635. #:disk-image-size
  636. (if full-boot?
  637. image-size
  638. (* 70 (expt 2 20)))
  639. #:mappings mappings))
  640. ((disk-image)
  641. (system-disk-image os
  642. #:name (match file-system-type
  643. ("iso9660" "image.iso")
  644. (_ "disk-image"))
  645. #:disk-image-size image-size
  646. #:file-system-type file-system-type))
  647. ((docker-image)
  648. (system-docker-image os #:register-closures? #t))))
  649. (define (maybe-suggest-running-guix-pull)
  650. "Suggest running 'guix pull' if this has never been done before."
  651. ;; The reason for this is that the 'guix' binding that we see here comes
  652. ;; from either ~/.config/latest or, if it's missing, from the
  653. ;; globally-installed Guix, which is necessarily older. See
  654. ;; <http://lists.gnu.org/archive/html/guix-devel/2014-08/msg00057.html> for
  655. ;; a discussion.
  656. (define latest
  657. (string-append (config-directory) "/current"))
  658. (unless (file-exists? latest)
  659. (warning (G_ "~a not found: 'guix pull' was never run~%") latest)
  660. (warning (G_ "Consider running 'guix pull' before 'reconfigure'.~%"))
  661. (warning (G_ "Failing to do that may downgrade your system!~%"))))
  662. (define (bootloader-installer-derivation installer
  663. bootloader device target)
  664. "Return a file calling INSTALLER gexp with given BOOTLOADER, DEVICE
  665. and TARGET arguments."
  666. (with-monad %store-monad
  667. (gexp->file "bootloader-installer"
  668. (with-imported-modules '((gnu build bootloader)
  669. (guix build utils))
  670. #~(begin
  671. (use-modules (gnu build bootloader)
  672. (guix build utils)
  673. (ice-9 binary-ports))
  674. (#$installer #$bootloader #$device #$target))))))
  675. (define* (perform-action action os
  676. #:key skip-safety-checks?
  677. install-bootloader?
  678. dry-run? derivations-only?
  679. use-substitutes? bootloader-target target
  680. image-size file-system-type full-boot?
  681. (mappings '())
  682. (gc-root #f))
  683. "Perform ACTION for OS. INSTALL-BOOTLOADER? specifies whether to install
  684. bootloader; BOOTLOADER-TAGET is the target for the bootloader; TARGET is the
  685. target root directory; IMAGE-SIZE is the size of the image to be built, for
  686. the 'vm-image' and 'disk-image' actions. The root file system is created as a
  687. FILE-SYSTEM-TYPE file system. FULL-BOOT? is used for the 'vm' action; it
  688. determines whether to boot directly to the kernel or to the bootloader.
  689. When DERIVATIONS-ONLY? is true, print the derivation file name(s) without
  690. building anything.
  691. When GC-ROOT is a path, also make that path an indirect root of the build
  692. output when building a system derivation, such as a disk image.
  693. When SKIP-SAFETY-CHECKS? is true, skip the file system and initrd module
  694. static checks."
  695. (define println
  696. (cut format #t "~a~%" <>))
  697. (when (eq? action 'reconfigure)
  698. (maybe-suggest-running-guix-pull))
  699. ;; Check whether the declared file systems exist. This is better than
  700. ;; instantiating a broken configuration. Assume that we can only check if
  701. ;; running as root.
  702. (when (and (not skip-safety-checks?)
  703. (memq action '(init reconfigure)))
  704. (check-mapped-devices os)
  705. (when (zero? (getuid))
  706. (check-file-system-availability (operating-system-file-systems os))
  707. (check-initrd-modules os)))
  708. (mlet* %store-monad
  709. ((sys (system-derivation-for-action os action
  710. #:file-system-type file-system-type
  711. #:image-size image-size
  712. #:full-boot? full-boot?
  713. #:mappings mappings))
  714. (bootloader -> (bootloader-configuration-bootloader
  715. (operating-system-bootloader os)))
  716. (bootloader-package
  717. (let ((package (bootloader-package bootloader)))
  718. (if package
  719. (package->derivation package)
  720. (return #f))))
  721. (bootcfg (if (eq? 'container action)
  722. (return #f)
  723. (operating-system-bootcfg
  724. os
  725. (if (eq? 'init action)
  726. '()
  727. (map boot-parameters->menu-entry
  728. (profile-boot-parameters))))))
  729. (bootcfg-file -> (bootloader-configuration-file bootloader))
  730. (bootloader-installer
  731. (let ((installer (bootloader-installer bootloader))
  732. (target (or target "/")))
  733. (bootloader-installer-derivation installer
  734. bootloader-package
  735. bootloader-target target)))
  736. ;; For 'init' and 'reconfigure', always build BOOTCFG, even if
  737. ;; --no-bootloader is passed, because we then use it as a GC root.
  738. ;; See <http://bugs.gnu.org/21068>.
  739. (drvs -> (if (memq action '(init reconfigure))
  740. (if (and install-bootloader? bootloader-package)
  741. (list sys bootcfg
  742. bootloader-package
  743. bootloader-installer)
  744. (list sys bootcfg))
  745. (list sys)))
  746. (% (if derivations-only?
  747. (return (for-each (compose println derivation-file-name)
  748. drvs))
  749. (maybe-build drvs #:dry-run? dry-run?
  750. #:use-substitutes? use-substitutes?))))
  751. (if (or dry-run? derivations-only?)
  752. (return #f)
  753. (begin
  754. (for-each (compose println derivation->output-path)
  755. drvs)
  756. (case action
  757. ((reconfigure)
  758. (mbegin %store-monad
  759. (switch-to-system os)
  760. (mwhen install-bootloader?
  761. (install-bootloader bootloader-installer
  762. #:bootcfg bootcfg
  763. #:bootcfg-file bootcfg-file
  764. #:target "/"))))
  765. ((init)
  766. (newline)
  767. (format #t (G_ "initializing operating system under '~a'...~%")
  768. target)
  769. (install sys (canonicalize-path target)
  770. #:install-bootloader? install-bootloader?
  771. #:bootcfg bootcfg
  772. #:bootcfg-file bootcfg-file
  773. #:bootloader-installer bootloader-installer))
  774. (else
  775. ;; All we had to do was to build SYS and maybe register an
  776. ;; indirect GC root.
  777. (let ((output (derivation->output-path sys)))
  778. (mbegin %store-monad
  779. (mwhen gc-root
  780. (register-root* (list output) gc-root))
  781. (return output)))))))))
  782. (define (export-extension-graph os port)
  783. "Export the service extension graph of OS to PORT."
  784. (let* ((services (operating-system-services os))
  785. (system (find (lambda (service)
  786. (eq? (service-kind service) system-service-type))
  787. services)))
  788. (export-graph (list system) (current-output-port)
  789. #:node-type (service-node-type services)
  790. #:reverse-edges? #t)))
  791. (define (export-shepherd-graph os port)
  792. "Export the graph of shepherd services of OS to PORT."
  793. (let* ((services (operating-system-services os))
  794. (pid1 (fold-services services
  795. #:target-type shepherd-root-service-type))
  796. (shepherds (service-value pid1)) ;list of <shepherd-service>
  797. (sinks (filter (lambda (service)
  798. (null? (shepherd-service-requirement service)))
  799. shepherds)))
  800. (export-graph sinks (current-output-port)
  801. #:node-type (shepherd-service-node-type shepherds)
  802. #:reverse-edges? #t)))
  803. ;;;
  804. ;;; Options.
  805. ;;;
  806. (define (show-help)
  807. (display (G_ "Usage: guix system [OPTION ...] ACTION [ARG ...] [FILE]
  808. Build the operating system declared in FILE according to ACTION.
  809. Some ACTIONS support additional ARGS.\n"))
  810. (newline)
  811. (display (G_ "The valid values for ACTION are:\n"))
  812. (newline)
  813. (display (G_ "\
  814. search search for existing service types\n"))
  815. (display (G_ "\
  816. reconfigure switch to a new operating system configuration\n"))
  817. (display (G_ "\
  818. roll-back switch to the previous operating system configuration\n"))
  819. (display (G_ "\
  820. switch-generation switch to an existing operating system configuration\n"))
  821. (display (G_ "\
  822. list-generations list the system generations\n"))
  823. (display (G_ "\
  824. build build the operating system without installing anything\n"))
  825. (display (G_ "\
  826. container build a container that shares the host's store\n"))
  827. (display (G_ "\
  828. vm build a virtual machine image that shares the host's store\n"))
  829. (display (G_ "\
  830. vm-image build a freestanding virtual machine image\n"))
  831. (display (G_ "\
  832. disk-image build a disk image, suitable for a USB stick\n"))
  833. (display (G_ "\
  834. docker-image build a Docker image\n"))
  835. (display (G_ "\
  836. init initialize a root file system to run GNU\n"))
  837. (display (G_ "\
  838. extension-graph emit the service extension graph in Dot format\n"))
  839. (display (G_ "\
  840. shepherd-graph emit the graph of shepherd services in Dot format\n"))
  841. (show-build-options-help)
  842. (display (G_ "
  843. -d, --derivation return the derivation of the given system"))
  844. (display (G_ "
  845. -e, --expression=EXPR consider the operating-system EXPR evaluates to
  846. instead of reading FILE, when applicable"))
  847. (display (G_ "
  848. --on-error=STRATEGY
  849. apply STRATEGY when an error occurs while reading FILE"))
  850. (display (G_ "
  851. --file-system-type=TYPE
  852. for 'disk-image', produce a root file system of TYPE
  853. (one of 'ext4', 'iso9660')"))
  854. (display (G_ "
  855. --image-size=SIZE for 'vm-image', produce an image of SIZE"))
  856. (display (G_ "
  857. --no-bootloader for 'init', do not install a bootloader"))
  858. (display (G_ "
  859. --share=SPEC for 'vm', share host file system according to SPEC"))
  860. (display (G_ "
  861. -r, --root=FILE for 'vm', 'vm-image', 'disk-image', 'container',
  862. and 'build', make FILE a symlink to the result, and
  863. register it as a garbage collector root"))
  864. (display (G_ "
  865. --expose=SPEC for 'vm', expose host file system according to SPEC"))
  866. (display (G_ "
  867. --full-boot for 'vm', make a full boot sequence"))
  868. (display (G_ "
  869. --skip-checks skip file system and initrd module safety checks"))
  870. (newline)
  871. (display (G_ "
  872. -h, --help display this help and exit"))
  873. (display (G_ "
  874. -V, --version display version information and exit"))
  875. (newline)
  876. (show-bug-report-information))
  877. (define %options
  878. ;; Specifications of the command-line options.
  879. (cons* (option '(#\h "help") #f #f
  880. (lambda args
  881. (show-help)
  882. (exit 0)))
  883. (option '(#\V "version") #f #f
  884. (lambda args
  885. (show-version-and-exit "guix system")))
  886. (option '(#\e "expression") #t #f
  887. (lambda (opt name arg result)
  888. (alist-cons 'expression arg result)))
  889. (option '(#\d "derivation") #f #f
  890. (lambda (opt name arg result)
  891. (alist-cons 'derivations-only? #t result)))
  892. (option '("on-error") #t #f
  893. (lambda (opt name arg result)
  894. (alist-cons 'on-error (string->symbol arg)
  895. result)))
  896. (option '(#\t "file-system-type") #t #f
  897. (lambda (opt name arg result)
  898. (alist-cons 'file-system-type arg
  899. result)))
  900. (option '("image-size") #t #f
  901. (lambda (opt name arg result)
  902. (alist-cons 'image-size (size->number arg)
  903. result)))
  904. (option '("no-bootloader" "no-grub") #f #f
  905. (lambda (opt name arg result)
  906. (alist-cons 'install-bootloader? #f result)))
  907. (option '("full-boot") #f #f
  908. (lambda (opt name arg result)
  909. (alist-cons 'full-boot? #t result)))
  910. (option '("skip-checks") #f #f
  911. (lambda (opt name arg result)
  912. (alist-cons 'skip-safety-checks? #t result)))
  913. (option '("share") #t #f
  914. (lambda (opt name arg result)
  915. (alist-cons 'file-system-mapping
  916. (specification->file-system-mapping arg #t)
  917. result)))
  918. (option '("expose") #t #f
  919. (lambda (opt name arg result)
  920. (alist-cons 'file-system-mapping
  921. (specification->file-system-mapping arg #f)
  922. result)))
  923. (option '(#\n "dry-run") #f #f
  924. (lambda (opt name arg result)
  925. (alist-cons 'dry-run? #t (alist-cons 'graft? #f result))))
  926. (option '(#\s "system") #t #f
  927. (lambda (opt name arg result)
  928. (alist-cons 'system arg
  929. (alist-delete 'system result eq?))))
  930. (option '(#\r "root") #t #f
  931. (lambda (opt name arg result)
  932. (alist-cons 'gc-root arg result)))
  933. %standard-build-options))
  934. (define %default-options
  935. ;; Alist of default option values.
  936. `((system . ,(%current-system))
  937. (substitutes? . #t)
  938. (build-hook? . #t)
  939. (graft? . #t)
  940. (verbosity . 0)
  941. (file-system-type . "ext4")
  942. (image-size . guess)
  943. (install-bootloader? . #t)))
  944. ;;;
  945. ;;; Entry point.
  946. ;;;
  947. (define (process-action action args opts)
  948. "Process ACTION, a sub-command, with the arguments are listed in ARGS.
  949. ACTION must be one of the sub-commands that takes an operating system
  950. declaration as an argument (a file name.) OPTS is the raw alist of options
  951. resulting from command-line parsing."
  952. (let* ((file (match args
  953. (() #f)
  954. ((x . _) x)))
  955. (expr (assoc-ref opts 'expression))
  956. (system (assoc-ref opts 'system))
  957. (os (cond
  958. ((and expr file)
  959. (leave
  960. (G_ "both file and expression cannot be specified~%")))
  961. (expr
  962. (read/eval expr))
  963. (file
  964. (load* file %user-module
  965. #:on-error (assoc-ref opts 'on-error)))
  966. (else
  967. (leave (G_ "no configuration specified~%")))))
  968. (dry? (assoc-ref opts 'dry-run?))
  969. (bootloader? (assoc-ref opts 'install-bootloader?))
  970. (target (match args
  971. ((first second) second)
  972. (_ #f)))
  973. (bootloader-target
  974. (and bootloader?
  975. (bootloader-configuration-target
  976. (operating-system-bootloader os)))))
  977. (with-store store
  978. (set-build-options-from-command-line store opts)
  979. (run-with-store store
  980. (mbegin %store-monad
  981. (set-guile-for-build (default-guile))
  982. (case action
  983. ((extension-graph)
  984. (export-extension-graph os (current-output-port)))
  985. ((shepherd-graph)
  986. (export-shepherd-graph os (current-output-port)))
  987. (else
  988. (unless (memq action '(build init))
  989. (warn-about-old-distro #:suggested-command
  990. "guix system reconfigure"))
  991. (perform-action action os
  992. #:dry-run? dry?
  993. #:derivations-only? (assoc-ref opts
  994. 'derivations-only?)
  995. #:use-substitutes? (assoc-ref opts 'substitutes?)
  996. #:skip-safety-checks?
  997. (assoc-ref opts 'skip-safety-checks?)
  998. #:file-system-type (assoc-ref opts 'file-system-type)
  999. #:image-size (assoc-ref opts 'image-size)
  1000. #:full-boot? (assoc-ref opts 'full-boot?)
  1001. #:mappings (filter-map (match-lambda
  1002. (('file-system-mapping . m)
  1003. m)
  1004. (_ #f))
  1005. opts)
  1006. #:install-bootloader? bootloader?
  1007. #:target target
  1008. #:bootloader-target bootloader-target
  1009. #:gc-root (assoc-ref opts 'gc-root)))))
  1010. #:system system))))
  1011. (define (resolve-subcommand name)
  1012. (let ((module (resolve-interface
  1013. `(guix scripts system ,(string->symbol name))))
  1014. (proc (string->symbol (string-append "guix-system-" name))))
  1015. (module-ref module proc)))
  1016. (define (process-command command args opts)
  1017. "Process COMMAND, one of the 'guix system' sub-commands. ARGS is its
  1018. argument list and OPTS is the option alist."
  1019. (case command
  1020. ;; The following commands do not need to use the store, and they do not need
  1021. ;; an operating system configuration file.
  1022. ((list-generations)
  1023. (let ((pattern (match args
  1024. (() "")
  1025. ((pattern) pattern)
  1026. (x (leave (G_ "wrong number of arguments~%"))))))
  1027. (list-generations pattern)))
  1028. ((search)
  1029. (apply (resolve-subcommand "search") args))
  1030. ;; The following commands need to use the store, but they do not need an
  1031. ;; operating system configuration file.
  1032. ((switch-generation)
  1033. (let ((pattern (match args
  1034. ((pattern) pattern)
  1035. (x (leave (G_ "wrong number of arguments~%"))))))
  1036. (with-store store
  1037. (set-build-options-from-command-line store opts)
  1038. (switch-to-system-generation store pattern))))
  1039. ((roll-back)
  1040. (let ((pattern (match args
  1041. (() "")
  1042. (x (leave (G_ "wrong number of arguments~%"))))))
  1043. (with-store store
  1044. (set-build-options-from-command-line store opts)
  1045. (roll-back-system store))))
  1046. ;; The following commands need to use the store, and they also
  1047. ;; need an operating system configuration file.
  1048. (else (process-action command args opts))))
  1049. (define (guix-system . args)
  1050. (define (parse-sub-command arg result)
  1051. ;; Parse sub-command ARG and augment RESULT accordingly.
  1052. (if (assoc-ref result 'action)
  1053. (alist-cons 'argument arg result)
  1054. (let ((action (string->symbol arg)))
  1055. (case action
  1056. ((build container vm vm-image disk-image reconfigure init
  1057. extension-graph shepherd-graph list-generations roll-back
  1058. switch-generation search docker-image)
  1059. (alist-cons 'action action result))
  1060. (else (leave (G_ "~a: unknown action~%") action))))))
  1061. (define (match-pair car)
  1062. ;; Return a procedure that matches a pair with CAR.
  1063. (match-lambda
  1064. ((head . tail)
  1065. (and (eq? car head) tail))
  1066. (_ #f)))
  1067. (define (option-arguments opts)
  1068. ;; Extract the plain arguments from OPTS.
  1069. (let* ((args (reverse (filter-map (match-pair 'argument) opts)))
  1070. (count (length args))
  1071. (action (assoc-ref opts 'action))
  1072. (expr (assoc-ref opts 'expression)))
  1073. (define (fail)
  1074. (leave (G_ "wrong number of arguments for action '~a'~%")
  1075. action))
  1076. (unless action
  1077. (format (current-error-port)
  1078. (G_ "guix system: missing command name~%"))
  1079. (format (current-error-port)
  1080. (G_ "Try 'guix system --help' for more information.~%"))
  1081. (exit 1))
  1082. (case action
  1083. ((build container vm vm-image disk-image docker-image reconfigure)
  1084. (unless (or (= count 1)
  1085. (and expr (= count 0)))
  1086. (fail)))
  1087. ((init)
  1088. (unless (= count 2)
  1089. (fail))))
  1090. args))
  1091. (with-error-handling
  1092. (let* ((opts (parse-command-line args %options
  1093. (list %default-options)
  1094. #:argument-handler
  1095. parse-sub-command))
  1096. (args (option-arguments opts))
  1097. (command (assoc-ref opts 'action)))
  1098. (parameterize ((%graft? (assoc-ref opts 'graft?))
  1099. (current-terminal-columns (terminal-columns)))
  1100. (process-command command args opts)))))
  1101. ;;; Local Variables:
  1102. ;;; eval: (put 'call-with-service-upgrade-info 'scheme-indent-function 1)
  1103. ;;; End:
  1104. ;;; system.scm ends here