[gnome-build-meta/valentindavid/gnome-os-ostree: 9/12] Add OSTree based VM image



commit ea9e015b38e296c2b25dcc9eda344d8ba6b76d0b
Author: Valentin David <valentin david codethink co uk>
Date:   Sun Nov 17 11:48:52 2019 +0100

    Add OSTree based VM image

 elements/vm/deps.bst                        |  24 ++++++
 elements/vm/filesystem.bst                  |  13 ++++
 elements/vm/flathub-config.bst              |  17 +++++
 elements/vm/image.bst                       | 111 ++++++++++++++++++++++++++++
 elements/vm/initial-scripts.bst             |   7 ++
 elements/vm/initramfs.bst                   |  67 +++++++++++++++++
 elements/vm/initramfs/deps.bst              |  15 ++++
 elements/vm/initramfs/initial-scripts.bst   |   7 ++
 elements/vm/ostree-config.bst               |  46 ++++++++++++
 elements/vm/repo-deps.bst                   |   5 ++
 elements/vm/repo.bst                        |  40 ++++++++++
 files/vm/flathub-config/add-flathub.preset  |   1 +
 files/vm/flathub-config/add-flathub.service |  12 +++
 files/vm/gnome.gpg                          |  29 ++++++++
 files/vm/ostree-config/gnome.conf.in        |   4 +
 files/vm/ostree-config/ostree.conf          |   4 +
 plugins/collect_initial_scripts.py          |  55 ++++++++++++++
 project.conf                                |   6 ++
 18 files changed, 463 insertions(+)
---
diff --git a/elements/vm/deps.bst b/elements/vm/deps.bst
new file mode 100644
index 00000000..be7cc50a
--- /dev/null
+++ b/elements/vm/deps.bst
@@ -0,0 +1,24 @@
+kind: stack
+
+runtime-depends:
+- freedesktop-sdk.bst:components/util-linux.bst
+- freedesktop-sdk.bst:components/tzdata.bst
+- freedesktop-sdk.bst:components/systemd.bst
+- freedesktop-sdk.bst:components/xorg-server.bst
+- freedesktop-sdk.bst:components/shadow.bst
+- freedesktop-sdk.bst:vm/mesa-default.bst
+
+- vm/initramfs.bst
+- vm/ostree-config.bst
+
+- vm/plymouth-gnome-theme.bst
+- vm/kbd.bst
+
+- freedesktop-sdk.bst:vm/config/pulseaudio.bst
+- freedesktop-sdk.bst:vm/config/sudo.bst
+- freedesktop-sdk.bst:vm/config/journald.bst
+- vm/flathub-config.bst
+
+- sdk-platform.bst
+- core.bst
+- core/systemd-presets.bst
diff --git a/elements/vm/filesystem.bst b/elements/vm/filesystem.bst
new file mode 100644
index 00000000..da7e9f3f
--- /dev/null
+++ b/elements/vm/filesystem.bst
@@ -0,0 +1,13 @@
+kind: compose
+
+build-depends:
+- vm/deps.bst
+
+# Currently we just take everything and remove the
+# devel stuff (headers and static libraries), and debugging
+# symbols which take a huge amount of space, we could
+# refine this and make it much more selective, though.
+config:
+  exclude:
+  - devel
+  - debug
diff --git a/elements/vm/flathub-config.bst b/elements/vm/flathub-config.bst
new file mode 100644
index 00000000..25d2036b
--- /dev/null
+++ b/elements/vm/flathub-config.bst
@@ -0,0 +1,17 @@
+kind: manual
+
+depends:
+- freedesktop-sdk.bst:bootstrap-import.bst
+
+runtime-depends:
+- core-deps/flatpak.bst
+
+config:
+  install-commands:
+  - |
+    install -Dm644 -t "%{install-root}%{indep-libdir}/systemd/system" add-flathub.service
+    install -Dm644 -t "%{install-root}%{indep-libdir}/systemd/system-preset" add-flathub.preset
+
+sources:
+- kind: local
+  path: files/vm/flathub-config
diff --git a/elements/vm/image.bst b/elements/vm/image.bst
new file mode 100644
index 00000000..bf2556c9
--- /dev/null
+++ b/elements/vm/image.bst
@@ -0,0 +1,111 @@
+kind: script
+
+build-depends:
+- core-deps/ostree.bst
+- vm/qemu-tools.bst
+- vm/repo.bst
+- freedesktop-sdk.bst:integration/mtab.bst
+- freedesktop-sdk.bst:components/genimage.bst
+
+variables:
+  (?):
+  - arch == "x86_64":
+      linux-root: 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709
+  - arch == "i686":
+      linux-root: 44479540-F297-41B2-9AF7-D131D5F0458A
+  - arch == "arm":
+      linux-root: 69DAD710-2CE4-4E3C-B16C-21A1D49ABED3
+  - arch == "aarch64":
+      linux-root: B921B045-1DF0-41C3-AF44-4C6F280D3FAE
+
+  sysroot: "/sysroot"
+
+environment:
+  OSTREE_REPO: "%{sysroot}/ostree/repo"
+
+config:
+  layout:
+  - element: ''
+    destination: '/sysroot'
+  - element: ''
+    destination: '/genimage'
+  - element: vm/repo.bst
+    destination: '/source-repo'
+  - element: core-deps/ostree.bst
+    destination: '/'
+  - element: vm/qemu-tools.bst
+    destination: '/'
+  - element: integration/mtab.bst
+    destination: '/'
+  - element: components/genimage.bst
+    destination: '/'
+
+  commands:
+  - mkdir -p "${OSTREE_REPO}"
+  - ostree init --repo="${OSTREE_REPO}" --mode=bare
+
+  - ostree config --group sysroot set bootloader none
+  - ostree pull-local "/source-repo" %{ostree-branch}
+
+  - mkdir -p "%{sysroot}/boot"
+
+  - ostree admin init-fs "%{sysroot}"
+  - ostree admin os-init --sysroot="%{sysroot}" gnome-os
+  - |
+    ostree admin deploy --os="gnome-os" \
+         --sysroot="%{sysroot}" %{ostree-branch} \
+         --karg="rw" --karg="quiet" --karg="splash"
+
+  - |
+    ostree admin set-origin --sysroot="%{sysroot}" \
+           --index=0 \
+           GnomeOS %{ostree-remote-url} \
+           %{ostree-branch}
+
+  - |
+    cp -r "%{sysroot}"/ostree/boot.1/gnome-os/*/*/boot/EFI/ "%{sysroot}/boot/"
+
+  - |
+    cat >/genimage/genimage.cfg <<EOF
+    image efi.img {
+        vfat {
+            extraargs = "-F32 -n EFI"
+        }
+        mountpoint = "/boot"
+        size = 100M
+    }
+    image root.img {
+        ext4  {
+            label = "root"
+        }
+        size = 16G
+    }
+    image disk.img {
+        hdimage {
+            align = 1M
+            gpt = true
+        }
+        partition efi {
+            image = "efi.img"
+            partition-type-uuid = "U"
+        }
+        partition root {
+            image = "root.img"
+            partition-type-uuid = "%{linux-root}"
+        }
+    }
+    image disk.qcow2 {
+        qemu {
+        }
+        partition image {
+            image = "disk.img"
+        }
+    }
+    EOF
+
+  - |
+    cd /genimage
+    genimage --rootpath "%{sysroot}"
+
+  - |
+    install -Dm644 -t "%{install-root}" genimage/images/disk.qcow2
diff --git a/elements/vm/initial-scripts.bst b/elements/vm/initial-scripts.bst
new file mode 100644
index 00000000..a811a267
--- /dev/null
+++ b/elements/vm/initial-scripts.bst
@@ -0,0 +1,7 @@
+kind: collect_initial_scripts
+
+build-depends:
+- vm/deps.bst
+
+config:
+  path: /etc/fdsdk/initial_scripts
diff --git a/elements/vm/initramfs.bst b/elements/vm/initramfs.bst
new file mode 100644
index 00000000..2914123d
--- /dev/null
+++ b/elements/vm/initramfs.bst
@@ -0,0 +1,67 @@
+kind: script
+
+build-depends:
+- vm/initramfs/deps.bst
+- vm/initramfs/initial-scripts.bst
+- freedesktop-sdk.bst:vm/prepare-image.bst
+
+variables:
+  uuidnamespace: aea54278-2587-4075-ae67-8688ace4ce3d
+
+config:
+  layout:
+  - element: ''
+    destination: '/tmp'
+  - element: ''
+    destination: '/efi'
+  - element: ''
+    destination: '/var/tmp'
+  - element: vm/initramfs/deps.bst
+    destination: '/'
+  - element: vm/initramfs/initial-scripts.bst
+    destination: '/'
+  - element: vm/prepare-image.bst
+    destination: '/'
+
+  commands:
+  - |
+    prepare-image.sh \
+       --seed "%{uuidnamespace}" \
+       --rootsource /dev/gpt-auto-root \
+       --efisource LABEL=EFI \
+       --efipath /boot \
+       --rootpasswd "root" >/dev/null
+
+  - |
+    mkdir -p "%{install-root}/usr/lib/"
+    cp -r /usr/lib/modules "%{install-root}/usr/lib/"
+
+  - |
+    version="$(ls -1 /lib/modules | head -n1)"
+    mkdir -p "%{install-root}/usr/lib/modules/${version}"
+    dracut -v --fstab \
+           --no-machineid \
+           --kver "${version}" \
+           --add ostree \
+           --add plymouth \
+           --install 'fsck.ext4' \
+           "%{install-root}/usr/lib/modules/${version}/initramfs"
+
+  - |
+    version="$(ls -1 /lib/modules | head -n1)"
+    cp /boot/vmlinuz "%{install-root}/usr/lib/modules/${version}/vmlinuz"
+
+  - dbus-uuidgen >/etc/machine-id
+  - SYSTEMD_RELAX_ESP_CHECKS=1 bootctl --path='/efi' --no-variables install
+  - rm /etc/machine-id
+
+  - |
+    cp -r /efi "%{install-root}/boot"
+
+  - |
+    cat <<EOF >%{install-root}/boot/loader/loader.conf
+    timeout 3
+    editor yes
+    console-mode keep
+    default *
+    EOF
diff --git a/elements/vm/initramfs/deps.bst b/elements/vm/initramfs/deps.bst
new file mode 100644
index 00000000..29872db7
--- /dev/null
+++ b/elements/vm/initramfs/deps.bst
@@ -0,0 +1,15 @@
+kind: stack
+
+runtime-depends:
+- freedesktop-sdk.bst:bootstrap-import.bst
+- freedesktop-sdk.bst:components/util-linux.bst
+- freedesktop-sdk.bst:components/linux.bst
+- freedesktop-sdk.bst:components/systemd.bst
+- freedesktop-sdk.bst:components/dbus.bst
+- freedesktop-sdk.bst:components/dracut.bst
+- freedesktop-sdk.bst:components/os-release.bst
+- freedesktop-sdk.bst:components/shadow.bst
+- freedesktop-sdk.bst:components/tzdata.bst
+- freedesktop-sdk.bst:components/pkg-config.bst
+- core-deps/ostree.bst
+- vm/plymouth-gnome-theme.bst
diff --git a/elements/vm/initramfs/initial-scripts.bst b/elements/vm/initramfs/initial-scripts.bst
new file mode 100644
index 00000000..069d8f3f
--- /dev/null
+++ b/elements/vm/initramfs/initial-scripts.bst
@@ -0,0 +1,7 @@
+kind: collect_initial_scripts
+
+build-depends:
+- vm/initramfs/deps.bst
+
+config:
+  path: /etc/fdsdk/initial_scripts
diff --git a/elements/vm/ostree-config.bst b/elements/vm/ostree-config.bst
new file mode 100644
index 00000000..8bc5232e
--- /dev/null
+++ b/elements/vm/ostree-config.bst
@@ -0,0 +1,46 @@
+kind: manual
+
+build-depends:
+- freedesktop-sdk.bst:bootstrap-import.bst
+- freedesktop-sdk.bst:components/m4.bst
+
+runtime-depends:
+- freedesktop-sdk.bst:components/systemd.bst
+- core-deps/ostree.bst
+
+config:
+  build-commands:
+  - |
+    m4 -DOSTREE_REMOTE_URL="%{ostree-remote-url}" \
+      gnome.conf.in >gnome.conf
+
+  install-commands:
+  - |
+    mkdir %{install-root}/boot
+    mkdir %{install-root}/efi
+    mkdir %{install-root}/etc
+    mkdir %{install-root}/mnt
+    mkdir %{install-root}/run
+    mkdir %{install-root}/opt
+    mkdir %{install-root}/sys
+    mkdir %{install-root}/tmp
+    mkdir %{install-root}/dev
+    mkdir %{install-root}/proc
+
+  - |
+    mkdir -p "%{install-root}/sysroot"
+    ln -s sysroot/ostree "%{install-root}/ostree"
+    ln -s var/home "%{install-root}/home"
+    ln -s var/roothome "%{install-root}/root"
+    ln -s run/media "%{install-root}/media"
+
+  - |
+    install -Dm644 -t "%{install-root}/usr/lib/tmpfiles.d" ostree.conf
+    install -Dm644 *.gpg "%{install-root}/etc/pki/ostree/gnome.gpg"
+    install -Dm644 -t "%{install-root}/etc/ostree/remotes.d" gnome.conf
+
+sources:
+- kind: local
+  path: files/vm/ostree-config
+- kind: local
+  path: files/vm/gnome.gpg
diff --git a/elements/vm/repo-deps.bst b/elements/vm/repo-deps.bst
new file mode 100644
index 00000000..120ad68c
--- /dev/null
+++ b/elements/vm/repo-deps.bst
@@ -0,0 +1,5 @@
+kind: stack
+
+runtime-depends:
+- core-deps/ostree.bst
+- freedesktop-sdk.bst:vm/prepare-image.bst
diff --git a/elements/vm/repo.bst b/elements/vm/repo.bst
new file mode 100644
index 00000000..b2f10f3a
--- /dev/null
+++ b/elements/vm/repo.bst
@@ -0,0 +1,40 @@
+kind: script
+
+build-depends:
+- vm/repo-deps.bst
+- vm/filesystem.bst
+- vm/initial-scripts.bst
+- freedesktop-sdk.bst:vm/prepare-image.bst
+
+variables:
+  uuidnamespace: aea54278-2587-4075-ae67-8688ace4ce3d
+
+environment:
+  OSTREE_REPO: "%{install-root}"
+
+config:
+  layout:
+  - element: ''
+    destination: /tmp
+  - element: vm/repo-deps.bst
+    destination: /
+  - element: vm/filesystem.bst
+    destination: /sysroot
+  - element: vm/initial-scripts.bst
+    destination: /
+
+  commands:
+  - |
+    prepare-image.sh \
+       --sysroot /sysroot \
+       --seed "%{uuidnamespace}" \
+       --rootsource /dev/gpt-auto-root \
+        --efisource LABEL=EFI \
+       --efipath /boot \
+       --rootpasswd "root" >/dev/null
+
+  - mkdir -p "${OSTREE_REPO}"
+  - ostree init --repo="${OSTREE_REPO}" --mode=archive
+  - mv /sysroot/etc /sysroot/usr/etc
+  - ostree commit --branch=%{ostree-branch} --timestamp="$(date --date="@${SOURCE_DATE_EPOCH}" 
--rfc-3339=seconds)" /sysroot
+
diff --git a/files/vm/flathub-config/add-flathub.preset b/files/vm/flathub-config/add-flathub.preset
new file mode 100644
index 00000000..185c4062
--- /dev/null
+++ b/files/vm/flathub-config/add-flathub.preset
@@ -0,0 +1 @@
+enable add-flathub.service
diff --git a/files/vm/flathub-config/add-flathub.service b/files/vm/flathub-config/add-flathub.service
new file mode 100644
index 00000000..0fa35e0a
--- /dev/null
+++ b/files/vm/flathub-config/add-flathub.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Add flathub
+ConditionFirstBoot=yes
+Wants=network-online.target
+After=network-online.target systemd-resolved.service nss-lookup.target
+
+[Service]
+Type=oneshot
+ExecStart=flatpak remote-add flathub https://flathub.org/repo/flathub.flatpakrepo
+
+[Install]
+WantedBy=multi-user.target
diff --git a/files/vm/gnome.gpg b/files/vm/gnome.gpg
new file mode 100644
index 00000000..ecd64cde
--- /dev/null
+++ b/files/vm/gnome.gpg
@@ -0,0 +1,29 @@
+-----BEGIN PGP PUBLIC KEY BLOCK-----
+
+mQINBFuhBAsBEADDIU1v3mx7yvJwwbVw11gBm3HYR1kV0EbTI311nDuZ3oSf3zf9
+Rm2gI73C8v5nq6WcTjwkL0tvArf4OxWNFufWKqa6UfODOzwBXeNhkyZEFvvgI0qi
+f6IIbxMSXgntNmH+sD1quXJ7tiGyf2cA1FzQJJVUMRwsb2yGx3XLIZW7JDZgVJvW
+NVDmPg7unuECRXsmEGFusfiejsIwDLyOzXIushFk7APLR5QWyYdI2Ujb077aaMqo
+Ytfv4Iv1Ulgg44dlKqEqdRCbCtlBfirqbi4zi5TNI3dQbyxiPuCBls18Aw9sRq96
+MmpKlyD02Ig8TxzmVcjAJ2nGN/3ptW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Ga7dHork
+WmRlBUVTO0G6yAUKNRZ5fBk4Jt5Uh/aAXBOf4D4uC5sy3OHLL0oG/oRhT7lPwMTX
+NnblEwZgPFDcqGUYSLUFOKyDHqu04hVrzwKgOE4fUBJv4AKtWiEnmPEvEAANe0kK
+hrMbt8BZCpPMnrfB0HohI+KXIfiQAjLv
+=MJcD
+-----END PGP PUBLIC KEY BLOCK-----
diff --git a/files/vm/ostree-config/gnome.conf.in b/files/vm/ostree-config/gnome.conf.in
new file mode 100644
index 00000000..2732f205
--- /dev/null
+++ b/files/vm/ostree-config/gnome.conf.in
@@ -0,0 +1,4 @@
+[remote "GnomeOS"]
+url=OSTREE_REMOTE_URL
+gpg-verify=true
+gpgkeypath=/etc/pki/ostree/gnome.gpg
diff --git a/files/vm/ostree-config/ostree.conf b/files/vm/ostree-config/ostree.conf
new file mode 100644
index 00000000..a8531636
--- /dev/null
+++ b/files/vm/ostree-config/ostree.conf
@@ -0,0 +1,4 @@
+d /var/log/journal 0755 root root -
+L /var/home - - - - ../sysroot/home
+d /var/roothome 0700 root root -
+d /run/media 0755 root root -
diff --git a/plugins/collect_initial_scripts.py b/plugins/collect_initial_scripts.py
new file mode 100644
index 00000000..fe3b346d
--- /dev/null
+++ b/plugins/collect_initial_scripts.py
@@ -0,0 +1,55 @@
+import os
+import re
+from buildstream import Element, ElementError, Scope
+
+class ExtractInitialScriptsElement(Element):
+    def configure(self, node):
+        self.node_validate(node, [
+            'path',
+        ])
+
+        self.path = self.node_subst_member(node, 'path')
+
+    def preflight(self):
+        runtime_deps = list(self.dependencies(Scope.RUN, recurse=False))
+        if runtime_deps:
+            raise ElementError("{}: Only build type dependencies supported by collect-integration elements"
+                               .format(self))
+
+        sources = list(self.sources())
+        if sources:
+            raise ElementError("{}: collect-integration elements may not have sources".format(self))
+
+    def get_unique_key(self):
+        key = {
+            'path': self.path,
+        }
+        return key
+
+    def configure_sandbox(self, sandbox):
+        pass
+
+    def stage(self, sandbox):
+        pass
+
+    def assemble(self, sandbox):
+        basedir = sandbox.get_directory()
+        path = os.path.join(basedir, self.path.lstrip(os.sep))
+        index = 0
+        for dependency in self.dependencies(Scope.BUILD):
+            public = dependency.get_public_data('initial-script')
+            if public and 'script' in public:
+                script = self.node_subst_member(public, 'script')
+                index += 1
+                depname = re.sub('[^A-Za-z0-9]', '_', dependency.name)
+                basename = '{:03}-{}'.format(index, depname)
+                filename = os.path.join(path, basename)
+                os.makedirs(path, exist_ok=True)
+                with open(filename, 'w') as f:
+                    f.write(script)
+                os.chmod(filename, 0o755)
+
+        return os.sep
+
+def setup():
+    return ExtractInitialScriptsElement
diff --git a/project.conf b/project.conf
index 54cca149..21d4970d 100644
--- a/project.conf
+++ b/project.conf
@@ -205,6 +205,8 @@ shell:
 
 variables:
   branch: master
+  ostree-branch: "gnome-os/%{arch}/%{branch}"
+  ostree-remote-url: "https://nightly.gnome.org/gnomeos/repo";
   gcc_arch: "%{arch}"
   abi: "gnu"
   gcc_triplet: "%{gcc_arch}-linux-%{abi}"
@@ -284,3 +286,7 @@ plugins:
   sources:
     cargo: 0
     git_tag: 1
+- origin: local
+  path: plugins
+  elements:
+    collect_initial_scripts: 0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]