systemd / mkosi

💽 Build Bespoke OS Images
https://mkosi.systemd.io/
1.2k stars 325 forks source link

v20.2 The workspace directory cannot be located in the current working directory #2365

Closed iam-TJ closed 9 months ago

iam-TJ commented 9 months ago

I seem to be playing catch-up once more! This warning message is confusing. For at least 15 minutes I understood it to mean the workspace directory cannot be found in the current working directory but as it plainly is there I was busy reporting a bug!

$ ls -ld /home/tj/iam/mkosi/systemd-builder/mkosi.workspace
drwxr-xr-x 2 tj tj 4096 2023-07-21 12:54 /home/tj/iam/mkosi/systemd-builder/mkosi.workspace

Then, on re-reading the source in mkosi/__init__.py::check_workspace_directory() I realised that yet again the semantics of directory layouts have changed without explanation and that the use of ./mkosi.workspace has been removed.

So, there are two things here:

  1. The message needs rephrasing to something clearer
  2. How is one supposed to set WorkspaceDirectory= to take advantage of BTRFS Copy-on-Write?

I have a wrapper script around mkosi to configure the build environment for systemd-nspawn containers to use the BTRFS at /var/lib/machines/ . As part of that it bind-mounts that BTRFS so mkosi can use Copy-on-Write.

In respect of (2) my wrapper script aims for portability so it can be deployed in any source tree on any host to build that source using mkosi and for that reason needs all directories to be relative to the source tree, and then does bind mounts as commanded.

This is what the wrapper reports just prior to executing mkosi (using python debugging and mkosi in its source-code repository):

+ grep mkosi /proc/self/mountinfo                                                                                                                                          
1742 122 0:54 / /home/tj/iam/mkosi/systemd-builder/mkosi.output rw,relatime - btrfs /dev/mapper/SUNNY-Machines rw,space_cache,subvolid=5,subvol=/                          
1790 122 0:54 / /home/tj/iam/mkosi/systemd-builder/mkosi.workspace rw,relatime - btrfs /dev/mapper/SUNNY-Machines rw,space_cache,subvolid=5,subvol=/                       
1811 122 0:54 / /home/tj/iam/mkosi/systemd-builder/mkosi.cache rw,relatime - btrfs /dev/mapper/SUNNY-Machines rw,space_cache,subvolid=5,subvol=/                           
+ set -x                                                                                                                                                                   
+ command time --portability python3 -v -bb -X dev -B -m mkosi --debug --force --distribution=debian --release=testing --image-id debian-testing-amd64 --auto-bump --force
 build 

And the captured log reports:

import 'mkosi.installer.apt' # <_frozen_importlib_external.SourceFileLoader object at 0x7fb55102fa80>                                                                      
import 'mkosi.distributions.debian' # <_frozen_importlib_external.SourceFileLoader object at 0x7fb5511888d0>
‣ The workspace directory (/home/tj/iam/mkosi/systemd-builder/mkosi.workspace) cannot be located in the current working directory (/home/tj/iam/mkosi/systemd-builder)     
‣ (Use WorkspaceDirectory= to configure a different workspace directory)                                                                                                   
Traceback (most recent call last):                                                                                                                                         
File "/srv/NAS/Sunny/SourceCode/mkosi/mkosi/run.py", line 162, in uncaught_exception_handler                                                                               
yield                                                                                                                                                                      
File "/usr/lib/python3.11/contextlib.py", line 81, in inner                          
return func(*args, **kwds)                                                                                                                                                 
^^^^^^^^^^^^^^^^^^^                                                                                                                                                        
File "/srv/NAS/Sunny/SourceCode/mkosi/mkosi/__main__.py", line 34, in main                                                                                                 
run_verb(args, images)                                                                                                                                                     
File "/srv/NAS/Sunny/SourceCode/mkosi/mkosi/__init__.py", line 3538, in run_verb                                                                                           
check_workspace_directory(config)                                                                                                                                          
File "/srv/NAS/Sunny/SourceCode/mkosi/mkosi/__init__.py", line 3398, in check_workspace_directory                               
die(f"The workspace directory ({wd}) cannot be located in the current working directory ({Path.cwd()})",                                                                   
File "/srv/NAS/Sunny/SourceCode/mkosi/mkosi/log.py", line 31, in die                                                                                                       
sys.exit(1)                                                                                                                                                                
SystemExit: 1

The config fragment for Output:

$ cat mkosi.conf.d/95-output.conf 
[Output]
Format=directory
UseSubvolumes=true
WorkspaceDirectory=mkosi.workspace
DaanDeMeyer commented 9 months ago

@iam-TJ For technical reasons the workspace directory simply cannot be located inside the source directory that we're running from. Instead of bind mounting everything inside the current working directory, why not simply specify the directory you're mounting as the workspace directory?

iam-TJ commented 9 months ago

why not simply specify the directory you're mounting as the workspace directory? Would that be sufficient to enable CoW if it points directly to `/var/lib/machines/ ?

Update: answering myself - the first part at least seems to:

...
Create subvolume '/var/lib/machines/mkosi-workspacehvpt4ebs/root'  
...

Update 2:

...
‣  /home/tj/iam/mkosi/systemd-builder/mkosi.output/debian-testing-amd64_0.8 size is 419.4M.
‣ Increasing last component of version by one, bumping '0.8' → '0.9'.
...
$ sudo btrfs subvol list /var/lib/machines
ID 316 gen 2041 top level 5 path lineage-builder
ID 317 gen 698 top level 316 path lineage-builder/home
ID 318 gen 698 top level 316 path lineage-builder/srv
ID 319 gen 698 top level 316 path lineage-builder/var
ID 320 gen 698 top level 319 path lineage-builder/var/tmp
ID 321 gen 698 top level 319 path lineage-builder/var/lib/machines
ID 479 gen 2442 top level 5 path .mkosi.cache
ID 544 gen 2678 top level 5 path debian-bookworm-amd64-0.6
ID 546 gen 2759 top level 5 path debian-testing-amd64_0.8

Latest build here was 0.8 so yes, that does work - thank-you.

iam-TJ commented 9 months ago

But the image is not usable by systemd-machined due to the old bug about hyphens in machine names ( I also notice lib and cache have been left behind!):

$ machinectl list-images
NAME                      TYPE      RO USAGE CREATED                     MODIFIED
cache                     directory no       Sat 2024-02-10 13:36:12 GMT -
debian-bookworm-amd64     subvolume no       Sun 2023-07-23 08:29:58 BST -
debian-bookworm-amd64-0.6 subvolume no       Sun 2023-07-23 08:29:58 BST -
debian-bookworm-amd64_0.7 directory no       Sat 2024-02-10 13:59:29 GMT -
debian-testing-amd64_0.8  subvolume no       Sat 2024-02-10 15:17:42 GMT -
lib                       directory no       Sat 2024-02-10 13:36:12 GMT -
lineage-builder           subvolume no       Sat 2022-10-22 12:36:44 BST -
linux-builder             subvolume no       Thu 2021-08-12 01:30:41 BST -

8 images listed.
$ machinectl start debian-testing-amd64_0.8
Invalid machine name debian-testing-amd64_0.8.
iam-TJ commented 9 months ago

to deal with the lib and cache issue I changed to:

WorkspaceDirectory=/var/lib/machines/mkosi.workspace

and this seems to work with one exception; if mkosi creates that directory it does not also remove it afterwards, so machinectl list-images then lists it:

$  machinectl list-images
...
mkosi.workspace           directory no       Sat 2024-02-10 16:10:05 GMT -
DaanDeMeyer commented 9 months ago

The original issue is fixed so I'm closing this, please open new issues for any other problems.