0

I download Linux kernel source code, successfully compiled it and run it with BusyBox in QEMU.

Because of BusyBox, I can use some frequently-used tools, such as vi,ls,cp,cat, etc.

But when I try to compile a simple "hello world" C/C++ program, I get gcc: not found.

In addition, I can't make a new Linux module by make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules inside QEMU.

I googled a lot, still can't figure it out.

So my question is: how can I install common developer tools like gcc, make, etc. inside my bare-bones QEMU VM that is running my custom Linux kernel (and not a standard distribution)?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Tianxu
  • 61
  • 5
  • 1
    "how to install common develop tools like gcc, make in Linux kernel" - You cannot install tools in the Linux **kernel**. These tools simply doesn't belong to the kernel and the kernel is not aware of their existence. It is OS which is responsible for installing/providing tools, so you need to ask it how to install software. See e.g. [that question](https://unix.stackexchange.com/questions/416715/package-manager-for-busybox). – Tsyvarev Jul 06 '22 at 14:39
  • Specifically about Linux kernel modules: you are better to build them on **other machine** (e.g. where you build the kernel itself), and then upload into the target machine. Not sure, however, whether Busybox provides ways for load these modules into the kernel (e.g. `insmod`). – Tsyvarev Jul 06 '22 at 14:44
  • 1
    @Tsyvarev Yes, Busybox can provide insmod, rmmod and modprobe. – Ian Abbott Jul 06 '22 at 14:52
  • @Tsyvarev Thanks a lot for your gread answer, that's helpful – Tianxu Jul 06 '22 at 14:59
  • I reworded your answer and title to make it clearer, hope this helps. – Marco Bonelli Jul 06 '22 at 15:30
  • @MarcoBonelli I am not a native speaker, thanks for your help. – Tianxu Jul 07 '22 at 01:33

1 Answers1

2

I see that you are trying to compile some program (or module) to use it inside your QEMU machine, but you do not have a compiler toolchain installed in the machine itself. You have a couple of options:

  1. Probably the easiest: since you already compiled the kernel that you are using for QEMU externally (in your host machine), you can easily also compile anything else this way. For modules, just pointing make to the same kernel source directory where you built the VM kernel should suffice. Once compiled you can then copy them inside the VM disk/image like you did for busybox.

  2. You can download and compile your own GCC from source (always on the host), and then install it inside the QEMU virtual machine. This is usually done by mounting the VM disk (QEMU image or whatever you are using) somewhere (e.g. /mnt/my-qemu-disk) and then configuring GCC with --prefix=/mnt/my-qemu-disk/usr/local, building and installing it with make install. This and other stuff is explained in this documentation page.

    Once you have GCC installed inside the machine, you should be able to use it as you normally do. You can now use it to compile GNU Make inside the VM, or you can just compile outside in the same way.

    For complex stuff like building kernel modules you will probably also need to build and install GNU binutils in the machine, again either from the inside with the GCC you just installed or from the outside.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Great answer! The first option is exactly what I want to do. I write a simple driver that just `printk` some thing, in the host machine, I successfully compile it and `insmod`, get printk message by `dmesg`. But when I follow the [steps](https://stackoverflow.com/a/19680541/11525738) to put my driver in kernel source code, I can't find anything in the QEMU VM, I tried `lsmod`, `dmsg`. **I would appreciate it if you could help me out**. – Tianxu Jul 07 '22 at 01:56
  • 1
    @Tianxu well that is a different question than the one you asked in this post. It's hard to say what could be wrong. You should post a new question showing (1) the source code of your module (2) the steps you did to compile the kernel and then the module (3) how you created the VM and how you are running it (e.g. the qemu command line used) (4) the kernel version and architecture you are working on. – Marco Bonelli Jul 07 '22 at 14:20