install.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2016 Andreas Enge <andreas@enge.fr>
  5. ;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
  6. ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (gnu system install)
  23. #:use-module (gnu)
  24. #:use-module (gnu bootloader u-boot)
  25. #:use-module (guix gexp)
  26. #:use-module (guix store)
  27. #:use-module (guix monads)
  28. #:use-module ((guix store) #:select (%store-prefix))
  29. #:use-module (gnu services shepherd)
  30. #:use-module (gnu services ssh)
  31. #:use-module (gnu packages admin)
  32. #:use-module (gnu packages bash)
  33. #:use-module (gnu packages bootloaders)
  34. #:use-module (gnu packages guile)
  35. #:use-module (gnu packages linux)
  36. #:use-module (gnu packages ssh)
  37. #:use-module (gnu packages cryptsetup)
  38. #:use-module (gnu packages package-management)
  39. #:use-module (gnu packages disk)
  40. #:use-module (gnu packages texinfo)
  41. #:use-module (gnu packages compression)
  42. #:use-module (gnu packages nvi)
  43. #:use-module (ice-9 match)
  44. #:use-module (srfi srfi-26)
  45. #:export (installation-os
  46. beaglebone-black-installation-os))
  47. ;;; Commentary:
  48. ;;;
  49. ;;; This module provides an 'operating-system' definition for use on images
  50. ;;; for USB sticks etc., for the installation of the GNU system.
  51. ;;;
  52. ;;; Code:
  53. (define (log-to-info)
  54. "Return a script that spawns the Info reader on the right section of the
  55. manual."
  56. (program-file "log-to-info"
  57. #~(begin
  58. ;; 'gunzip' is needed to decompress the doc.
  59. (setenv "PATH" (string-append #$gzip "/bin"))
  60. (execl (string-append #$info-reader "/bin/info") "info"
  61. "-d" "/run/current-system/profile/share/info"
  62. "-f" (string-append #$guix "/share/info/guix.info")
  63. "-n" "System Installation"))))
  64. (define %backing-directory
  65. ;; Sub-directory used as the backing store for copy-on-write.
  66. "/tmp/guix-inst")
  67. (define (make-cow-store target)
  68. "Return a gexp that makes the store copy-on-write, using TARGET as the
  69. backing store. This is useful when TARGET is on a hard disk, whereas the
  70. current store is on a RAM disk."
  71. (define (set-store-permissions directory)
  72. ;; Set the right perms on DIRECTORY to use it as the store.
  73. #~(begin
  74. (chown #$directory 0 30000) ;use the fixed 'guixbuild' GID
  75. (chmod #$directory #o1775)))
  76. #~(begin
  77. ;; Bind-mount TARGET's /tmp in case we need space to build things.
  78. (let ((tmpdir (string-append #$target "/tmp")))
  79. (mkdir-p tmpdir)
  80. (mount tmpdir "/tmp" "none" MS_BIND))
  81. (let* ((rw-dir (string-append target #$%backing-directory))
  82. (work-dir (string-append rw-dir "/../.overlayfs-workdir")))
  83. (mkdir-p rw-dir)
  84. (mkdir-p work-dir)
  85. (mkdir-p "/.rw-store")
  86. #$(set-store-permissions #~rw-dir)
  87. #$(set-store-permissions "/.rw-store")
  88. ;; Mount the overlay, then atomically make it the store.
  89. (mount "none" "/.rw-store" "overlay" 0
  90. (string-append "lowerdir=" #$(%store-prefix) ","
  91. "upperdir=" rw-dir ","
  92. "workdir=" work-dir))
  93. (mount "/.rw-store" #$(%store-prefix) "" MS_MOVE)
  94. (rmdir "/.rw-store"))))
  95. (define cow-store-service-type
  96. (shepherd-service-type
  97. 'cow-store
  98. (lambda _
  99. (shepherd-service
  100. (requirement '(root-file-system user-processes))
  101. (provision '(cow-store))
  102. (documentation
  103. "Make the store copy-on-write, with writes going to \
  104. the given target.")
  105. ;; This is meant to be explicitly started by the user.
  106. (auto-start? #f)
  107. (start #~(case-lambda
  108. ((target)
  109. #$(make-cow-store #~target)
  110. target)
  111. (else
  112. ;; Do nothing, and mark the service as stopped.
  113. #f)))
  114. (stop #~(lambda (target)
  115. ;; Delete the temporary directory, but leave everything
  116. ;; mounted as there may still be processes using it since
  117. ;; 'user-processes' doesn't depend on us. The 'user-unmount'
  118. ;; service will unmount TARGET eventually.
  119. (delete-file-recursively
  120. (string-append target #$%backing-directory))))))))
  121. (define (cow-store-service)
  122. "Return a service that makes the store copy-on-write, such that writes go to
  123. the user's target storage device rather than on the RAM disk."
  124. ;; See <http://bugs.gnu.org/18061> for the initial report.
  125. (service cow-store-service-type 'mooooh!))
  126. (define (/etc/configuration-files _)
  127. "Return a list of tuples representing configuration templates to add to
  128. /etc."
  129. (define (file f)
  130. (local-file (string-append "examples/" f)))
  131. (define directory
  132. (computed-file "configuration-templates"
  133. (with-imported-modules '((guix build utils))
  134. #~(begin
  135. (mkdir #$output)
  136. (for-each (lambda (file target)
  137. (copy-file file
  138. (string-append #$output "/"
  139. target)))
  140. '(#$(file "bare-bones.tmpl")
  141. #$(file "beaglebone-black.tmpl")
  142. #$(file "desktop.tmpl")
  143. #$(file "lightweight-desktop.tmpl"))
  144. '("bare-bones.scm"
  145. "beaglebone-black.scm"
  146. "desktop.scm"
  147. "lightweight-desktop.scm"))
  148. #t))))
  149. `(("configuration" ,directory)))
  150. (define configuration-template-service-type
  151. (service-type (name 'configuration-template)
  152. (extensions
  153. (list (service-extension etc-service-type
  154. /etc/configuration-files)))))
  155. (define %configuration-template-service
  156. (service configuration-template-service-type #t))
  157. (define %nscd-minimal-caches
  158. ;; Minimal in-memory caching policy for nscd.
  159. (list (nscd-cache (database 'hosts)
  160. (positive-time-to-live (* 3600 12))
  161. ;; Do not cache lookup failures at all since they are
  162. ;; quite likely (for instance when someone tries to ping a
  163. ;; host before networking is functional.)
  164. (negative-time-to-live 0)
  165. (persistent? #f)
  166. (max-database-size (* 5 (expt 2 20)))))) ;5 MiB
  167. (define %installation-services
  168. ;; List of services of the installation system.
  169. (let ((motd (plain-file "motd" "
  170. \x1b[1;37mWelcome to the installation of the Guix System Distribution!\x1b[0m
  171. \x1b[2mThere is NO WARRANTY, to the extent permitted by law. In particular, you may
  172. LOSE ALL YOUR DATA as a side effect of the installation process. Furthermore,
  173. it is 'beta' software, so it may contain bugs.
  174. You have been warned. Thanks for being so brave.\x1b[0m
  175. ")))
  176. (define (normal-tty tty)
  177. (mingetty-service (mingetty-configuration (tty tty)
  178. (auto-login "root")
  179. (login-pause? #t))))
  180. (define bare-bones-os
  181. (load "examples/bare-bones.tmpl"))
  182. (list (mingetty-service (mingetty-configuration
  183. (tty "tty1")
  184. (auto-login "root")))
  185. (login-service (login-configuration
  186. (motd motd)))
  187. ;; Documentation. The manual is in UTF-8, but
  188. ;; 'console-font-service' sets up Unicode support and loads a font
  189. ;; with all the useful glyphs like em dash and quotation marks.
  190. (mingetty-service (mingetty-configuration
  191. (tty "tty2")
  192. (auto-login "guest")
  193. (login-program (log-to-info))))
  194. ;; Documentation add-on.
  195. %configuration-template-service
  196. ;; A bunch of 'root' ttys.
  197. (normal-tty "tty3")
  198. (normal-tty "tty4")
  199. (normal-tty "tty5")
  200. (normal-tty "tty6")
  201. ;; The usual services.
  202. (syslog-service)
  203. ;; The build daemon. Register the hydra.gnu.org key as trusted.
  204. ;; This allows the installation process to use substitutes by
  205. ;; default.
  206. (guix-service (guix-configuration (authorize-key? #t)))
  207. ;; Start udev so that useful device nodes are available.
  208. ;; Use device-mapper rules for cryptsetup & co; enable the CRDA for
  209. ;; regulations-compliant WiFi access.
  210. (udev-service #:rules (list lvm2 crda))
  211. ;; Add the 'cow-store' service, which users have to start manually
  212. ;; since it takes the installation directory as an argument.
  213. (cow-store-service)
  214. ;; Install Unicode support and a suitable font. Use a font that
  215. ;; doesn't have more than 256 glyphs so that we can use colors with
  216. ;; varying brightness levels (see note in setfont(8)).
  217. (service console-font-service-type
  218. (map (lambda (tty)
  219. (cons tty "lat9u-16"))
  220. '("tty1" "tty2" "tty3" "tty4" "tty5" "tty6")))
  221. ;; To facilitate copy/paste.
  222. (gpm-service)
  223. ;; Add an SSH server to facilitate remote installs.
  224. (service openssh-service-type
  225. (openssh-configuration
  226. (port-number 22)
  227. (permit-root-login #t)
  228. ;; The root account is passwordless, so make sure
  229. ;; a password is set before allowing logins.
  230. (allow-empty-passwords? #f)
  231. (password-authentication? #t)
  232. ;; Don't start it upfront.
  233. (%auto-start? #f)))
  234. ;; Since this is running on a USB stick with a overlayfs as the root
  235. ;; file system, use an appropriate cache configuration.
  236. (nscd-service (nscd-configuration
  237. (caches %nscd-minimal-caches)))
  238. ;; Having /bin/sh is a good idea. In particular it allows Tramp
  239. ;; connections to this system to work.
  240. (service special-files-service-type
  241. `(("/bin/sh" ,(file-append (canonical-package bash)
  242. "/bin/sh"))))
  243. ;; Keep a reference to BARE-BONES-OS to make sure it can be
  244. ;; installed without downloading/building anything. Also keep the
  245. ;; things needed by 'profile-derivation' to minimize the amount of
  246. ;; download.
  247. (service gc-root-service-type
  248. (list bare-bones-os
  249. glibc-utf8-locales
  250. texinfo
  251. (canonical-package guile-2.2))))))
  252. (define %issue
  253. ;; Greeting.
  254. "
  255. \x1b[1;37mThis is an installation image of the GNU system. Welcome.\x1b[0m
  256. \x1b[1;33mUse Alt-F2 for documentation.\x1b[0m
  257. ")
  258. (define installation-os
  259. ;; The operating system used on installation images for USB sticks etc.
  260. (operating-system
  261. (host-name "gnu")
  262. (timezone "Europe/Paris")
  263. (locale "en_US.utf8")
  264. (bootloader (bootloader-configuration
  265. (bootloader grub-bootloader)
  266. (target "/dev/sda")))
  267. (file-systems
  268. ;; Note: the disk image build code overrides this root file system with
  269. ;; the appropriate one.
  270. (cons* (file-system
  271. (mount-point "/")
  272. (device "GuixSD_image")
  273. (title 'label)
  274. (type "ext4"))
  275. ;; Make /tmp a tmpfs instead of keeping the overlayfs. This
  276. ;; originally was used for unionfs because FUSE creates
  277. ;; '.fuse_hiddenXYZ' files for each open file, and this confuses
  278. ;; Guix's test suite, for instance (see
  279. ;; <http://bugs.gnu.org/23056>). We keep this for overlayfs to be
  280. ;; on the safe side.
  281. (file-system
  282. (mount-point "/tmp")
  283. (device "none")
  284. (title 'device)
  285. (type "tmpfs")
  286. (check? #f))
  287. ;; XXX: This should be %BASE-FILE-SYSTEMS but we don't need
  288. ;; elogind's cgroup file systems.
  289. (list %pseudo-terminal-file-system
  290. %shared-memory-file-system
  291. %immutable-store)))
  292. (users (list (user-account
  293. (name "guest")
  294. (group "users")
  295. (supplementary-groups '("wheel")) ; allow use of sudo
  296. (password "")
  297. (comment "Guest of GNU")
  298. (home-directory "/home/guest"))))
  299. (issue %issue)
  300. (services %installation-services)
  301. ;; We don't need setuid programs, except for 'passwd', which can be handy
  302. ;; if one is to allow remote SSH login to the machine being installed.
  303. (setuid-programs (list (file-append shadow "/bin/passwd")))
  304. (pam-services
  305. ;; Explicitly allow for empty passwords.
  306. (base-pam-services #:allow-empty-passwords? #t))
  307. (packages (cons* (canonical-package glibc) ;for 'tzselect' & co.
  308. parted gptfdisk ddrescue
  309. grub ;mostly so xrefs to its manual work
  310. cryptsetup
  311. mdadm
  312. dosfstools ;mkfs.fat, for the UEFI boot partition
  313. btrfs-progs
  314. openssh ;we already have sshd, having ssh/scp can help
  315. wireless-tools iw wpa-supplicant-minimal iproute
  316. ;; XXX: We used to have GNU fdisk here, but as of version
  317. ;; 2.0.0a, that pulls Guile 1.8, which takes unreasonable
  318. ;; space; furthermore util-linux's fdisk is already
  319. ;; available here, so we keep that.
  320. bash-completion
  321. nvi ;:wq!
  322. %base-packages))))
  323. (define beaglebone-black-installation-os
  324. (operating-system
  325. (inherit installation-os)
  326. (bootloader (bootloader-configuration
  327. (bootloader u-boot-beaglebone-black-bootloader)
  328. (target "/dev/sda")))
  329. (kernel linux-libre)
  330. (initrd (lambda (fs . rest)
  331. (apply base-initrd fs
  332. ;; This module is required to mount the sd card.
  333. #:extra-modules (list "omap_hsmmc")
  334. rest)))
  335. (services (append
  336. ;; mingetty does not work on serial lines.
  337. ;; Use agetty with board-specific serial parameters.
  338. (list (agetty-service
  339. (agetty-configuration
  340. (extra-options '("-L"))
  341. (baud-rate "115200")
  342. (term "vt100")
  343. (tty "ttyO0"))))
  344. (operating-system-user-services installation-os)))))
  345. ;; Return the default os here so 'guix system' can consume it directly.
  346. installation-os
  347. ;;; install.scm ends here