nanobyte-dev / nanobyte_os

OS tutorial from Nanobyte YouTube channel.
The Unlicense
458 stars 71 forks source link

libguestfs error on wsl 2 #27

Closed sytzemeijer closed 11 months ago

sytzemeijer commented 1 year ago
Creating disk image...
> creating partition table...
> formatting file using fat32...
> installing stage1...
> installing stage2...
> mounting image to build/i686_debug/tmp_mount_1674479016...
> cleaning up...
scons: *** [build/i686_debug/image.img] ErrorReturnCode_1 :

  RAN: /usr/bin/guestmount build/i686_debug/tmp_mount_1674479016 --add=build/i686_debug/image.img --mount=/dev/sda1

  STDOUT:

  STDERR:
libguestfs: trace: set_verbose true
libguestfs: trace: set_verbose = 0
libguestfs: create: flags = 0, handle = 0x559d5aed8500, program = guestmount
libguestfs: trace: set_recovery_proc false
libguestfs: trace: set_recovery_proc = 0
libguestfs: trace: add_drive "build/i686_debug/image.img"
libguestfs: trace: add_drive = 0
libguestfs: trace: launch
libguestfs: trace: max_disks
libguestfs: trace: max_disks = 255
libguestfs: trace: get_tmpdir
libguestfs: trace: get_tmpdir = "/tmp"
libguestfs: trace: version
libguestfs: trace: version = <struct guestfs_version = major: 1, minor: 46, release: 2, extra: , >
libguestfs: trace: get_backend
libguestfs: trace: get_backend = "direct"
libguestfs: launch: program=guestmount
libguestfs: launch: version=1.... (3478 more, please see e.stderr)
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/SCons/Action.py", line 1280, in execute
    result = self.execfunction(target=target, source=rsources, env=env)
  File "/home/sytze/nanobyte_os/image/SConscript", line 235, in build_image
    build_disk(image, stage1, stage2, kernel, files, env)
  File "/home/sytze/nanobyte_os/image/SConscript", line 193, in build_disk
    mount_fs(image, tempdir)
  File "/home/sytze/nanobyte_os/image/SConscript", line 152, in mount_fs
    sh.guestmount(mount_dir, add=image, mount='/dev/sda1')
  File "/usr/local/lib/python3.10/dist-packages/sh.py", line 1524, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/usr/local/lib/python3.10/dist-packages/sh.py", line 788, in __init__
    self.wait()
  File "/usr/local/lib/python3.10/dist-packages/sh.py", line 845, in wait
    self.handle_command_exit_code(exit_code)
  File "/usr/local/lib/python3.10/dist-packages/sh.py", line 869, in handle_command_exit_code
    raise exc
sh.ErrorReturnCode_1:

  RAN: /usr/bin/guestmount build/i686_debug/tmp_mount_1674479016 --add=build/i686_debug/image.img --mount=/dev/sda1

  STDOUT:

  STDERR:
libguestfs: trace: set_verbose true
libguestfs: trace: set_verbose = 0
libguestfs: create: flags = 0, handle = 0x559d5aed8500, program = guestmount
libguestfs: trace: set_recovery_proc false
libguestfs: trace: set_recovery_proc = 0
libguestfs: trace: add_drive "build/i686_debug/image.img"
libguestfs: trace: add_drive = 0
libguestfs: trace: launch
libguestfs: trace: max_disks
libguestfs: trace: max_disks = 255
libguestfs: trace: get_tmpdir
libguestfs: trace: get_tmpdir = "/tmp"
libguestfs: trace: version
libguestfs: trace: version = <struct guestfs_version = major: 1, minor: 46, release: 2, extra: , >
libguestfs: trace: get_backend
libguestfs: trace: get_backend = "direct"
libguestfs: launch: program=guestmount
libguestfs: launch: version=1.... (3478 more, please see e.stderr)
scons: building terminated because of errors.

windows cannot mount the image and wsl2 gives this error

sytzemeijer commented 1 year ago

I was using the standard Ubuntu from the app store

chibicitiberiu commented 1 year ago

If guestmount isn't working, you can try replacing the commands with the traditional mount and umount in image/SConscript (the sh library basically lets you call shell commands as if they were functions in python, you can find more details in the documentation for the sh library ).

Note that guestmount is doing 2 steps here... it is reading the disk image and searching for the partition, and then it is mounting the partition from the disk image. This would be done in 2 steps:

losetup -Pf disk_image 
# parse the output of the losetup command to see which /dev/loop<num> device it assigned
mount /dev/loop<num>p0 <mount_directory>

And for unmounting:

umount <mount_directory>
losetup -d /dev/loop<num>

Also note that you will need to run the scons command with sudo, which isn't great. To make things a bit better, I would change the Default target from here to contain all the other stuff (stage1, stage2, kernel), and only run scons image with sudo.

chibicitiberiu commented 1 year ago

While using sudo is generally not recommended in scripts, I think it is probably fine in this case... It's even better because you only use it with the commands that actually need it.

ajh123 commented 1 year ago

@chibicitiberiu I have found another fix for WSL it is to install the linux-image-generic with sudo apt install linux-image-generic. found at Álvaro González's comment.

However I have this error libguestfs: error: mount_options: mount_options_stub: /dev/sda1: No such file or directory guestmount: ‘/dev/sda1’ could not be mounted. guestmount: Did you mean to mount one of these filesystems? guestmount: /dev/sda (unknown). I don't have a /dev/sda1. Would using /dev/sda overwrite my main partition?

chibicitiberiu commented 12 months ago

I found a way to address this issue by doing a mount instead. I will post it soon.

chibicitiberiu commented 11 months ago

I am planning on covering this in a future livestream/video, so I created a gist with the changes that need to be done to fix the problem: https://gist.github.com/chibicitiberiu/6ab664feda5b508cedb35fe4ce62bed3

After modifying the image/SConscript file, you can use the mountMethod option in build_scripts/config.py. The main disadvantage of using mount instead of guestfs is it requires typing the sudo password every time the image is built.

Also note that I haven't done a lot of testing, some things might be broken.

chibicitiberiu commented 11 months ago

I've done it, you can see how in this livestream, the code is in the cpp-refactoring branch,