patches and low-level development discussion
 help / color / mirror / code / Atom feed
66fb358cb391f4ba07fce6f8637699ba8399efc9 blob 5617 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
 
with import ./. {};

let
  makeSquashfs = (callPackage nixos/lib/make-squashfs.nix {}).override;

  kernel' = linux_cros;

  kernel = with import lib/kernel.nix { inherit lib; inherit (kernel') version; };
    kernel'.override { structuredExtraConfig = {
                         VIRTIO_PCI = yes;
                         VIRTIO_BLK = yes;
                         VIRTIO_WL = yes;
                         VIRTIO_NET = yes;
                         DEVTMPFS_MOUNT = yes;
                         SQUASHFS = yes;

                         # VOP is needed to work around a Kconfig bug:
                         # https://lore.kernel.org/lkml/87wob4tf9b.fsf@alyssa.is/
                         VOP = yes;
                         VOP_BUS = yes;
                         HW_RANDOM = yes;
                         HW_RANDOM_VIRTIO = yes;

                         NET_9P = yes;
                         "9P_FS" = yes;
                       }; };

  login = writeScript "login" ''
    #! ${execline}/bin/execlineb -s0
    unexport !
    ${busybox}/bin/login -p -f root $@
  '';

  makeServicesDir = services: with lib;
    let
      services' = {

        ".s6-svscan" = {
          finish = writeScript "init-stage3" ''
            #! ${execline}/bin/execlineb -P
            foreground { s6-nuke -th }
            s6-sleep -m -- 2000
            foreground { s6-nuke -k }
            wait { }
            s6-linux-init-hpr -fr
          '';
        } // services.".s6-svscan" or {};
      } // services;

    in
      runCommandNoCC "services" {} ''
        mkdir $out
        ${concatStrings (mapAttrsToList (name: attrs: ''
          mkdir $out/${name}
          ${concatStrings (mapAttrsToList (key: value: ''
            cp ${value} $out/${name}/${key}
          '') attrs)}
        '') services')}
      '';

  makeStage1 = { run ? null, tapFD }: writeScript "init-stage1" ''
    #! ${execline}/bin/execlineb -P
    export PATH ${lib.makeBinPath
      [ s6-linux-init s6-portable-utils s6-linux-utils s6 execline busybox ]}
    ${s6}/bin/s6-setsid -qb --
    umask 022
    if { s6-mount -t tmpfs -o mode=0755 tmpfs /run }
    if { s6-hiercopy /etc/service /run/service }
    emptyenv -p

    background {
      s6-setsid --
      if { s6-mkdir -p /run/user/0 /dev/pts /dev/shm }
      if { s6-mount -t devpts -o gid=4,mode=620 none /dev/pts }
      if { s6-mount -t tmpfs none /dev/shm }
      if { s6-mount -t proc none /proc }

      if { ip addr add 10.0.10${toString tapFD}.2/24 dev eth0 }
      if { ip link set eth0 up }
      ${lib.optionalString (run != null) "if {"}
          ip route add default via 10.0.10${toString tapFD}.1
      ${lib.optionalString (run != null) ''
        }
        export XDG_RUNTIME_DIR /run/user/0
        foreground { ${run} }
        importas -i ? ?
        if { s6-echo STATUS: $? }
        s6-svscanctl -6 /run/service
      ''}
    }

    unexport !
    cd /run/service
    s6-svscan
  '';

  passwd = writeText "passwd" ''
    root:x:0:0:System administrator:/:/bin/sh
  '';

  makeRootfs = { services, run ? null, tapFD }: runCommand "rootfs" {} ''
    mkdir $out
    cd $out
    mkdir bin sbin dev etc proc run tmp
    ln -s ${dash}/bin/dash bin/sh
    ln -s ${makeStage1 { inherit run tapFD; }} sbin/init
    cp ${passwd} etc/passwd
    touch etc/login.defs
    cp -r ${makeServicesDir services} etc/service
  '';

  makeRootSquashfs = rootfs: runCommand "root-squashfs" {} ''
    cd ${rootfs}
    (
        grep -v ^${rootfs} ${writeReferencesToFile rootfs}
        printf "%s\n" *
    ) \
        | xargs tar -cP --owner root:0 --group root:0 --hard-dereference \
        | ${squashfs-tools-ng}/bin/tar2sqfs $out
  '';

  makeVM =
    { name, services ? {}, run ? null, wayland ? false, tapFD ? null }:
    let
      rootfs = makeRootfs { inherit run services tapFD; };
    in writeShellScript name ''
      exec ${crosvm}/bin/crosvm run \
          ${lib.optionalString wayland
              "--wayland-sock $XDG_RUNTIME_DIR/$WAYLAND_DISPLAY"} \
          ${lib.optionalString (tapFD != null) "--tap-fd ${toString tapFD}"} \
          -p init=/sbin/init \
          --root ${makeRootSquashfs rootfs} \
          ${kernel}/bzImage
    '';

  fsVM = makeVM {
    name = "fs-vm";
    tapFD = 3;

    services.rust-9p.run = writeScript "rust-9p-run" ''
      #! ${execline}/bin/execlineb -P
      ${rust-9p}/bin/unpfs tcp!0.0.0.0!564 /
    '';
  };

  waylandVM = makeVM {
    name = "wayland-vm";
    services.getty.run = writeScript "getty-run" ''
      #! ${execline}/bin/execlineb -P
      ${busybox}/bin/getty -i -n -l ${login} 38400 ttyS0
    '';
    run = ''
      background {
        if { s6-mkdir /run/mnt }
        loopwhilex
        if -n { s6-mount -t 9p 10.0.103.2 /run/mnt }
        s6-sleep 1
      }

      ${sommelier}/bin/sommelier
      ${westonLite}/bin/weston-terminal --shell /bin/sh
    '';
    wayland = true;
    tapFD = 4;
  };

in

writeScript "crosvm" ''
  #! ${execline}/bin/execlineb -P

  importas -i xdg_runtime_dir XDG_RUNTIME_DIR
  importas -i wayland_display WAYLAND_DISPLAY

  sudo

  importas -i uid SUDO_UID
  importas -i gid SUDO_GID

  ${mktuntap}/bin/mktuntap -pvB 3
  importas -iu tap_name_3 TUNTAP_NAME

  ${mktuntap}/bin/mktuntap -pvB 4
  importas -iu tap_name_4 TUNTAP_NAME

  if { ip addr add 10.0.103.1/24 dev $tap_name_3 }
  if { ip addr add 10.0.104.1/24 dev $tap_name_4 }
  if { ip link set $tap_name_3 up }
  if { ip link set $tap_name_4 up }

  ${s6}/bin/s6-applyuidgid -u $uid -g $gid

  export XDG_RUNTIME_DIR $xdg_runtime_dir
  export WAYLAND_DISPLAY $wayland_display

  background { ${fsVM} }
  ${waylandVM}
''
debug log:

solving 66fb358cb39 ...
found 66fb358cb39 in https://spectrum-os.org/git/nixpkgs

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).