📜 ⬆️ ⬇️

Linux in one file for Macbook



TL; DR You can fit a full-fledged Linux Live distribution in one file, if you compile into the kernel initramfs with the root file system. Computers with UEFI can download such a file directly, without the help of a GRUB loader. I managed to fit the distribution with the programs aircrack-ng, reaver and drivers for Wi-Fi cards in a file of 12 megabytes in size (you can probably use even less).

Main advantages



Why do you need it?


For all cases when you need a native Linux without a virtual machine.
To use PCIe devices in Linux, when they can not be thrown into a virtual machine. For example, a built-in Wi-Fi card for packet injection. When lazy to install a virtual machine and download a large ISO file with the distribution. When you do not want to mess with USB flash drives.
Once the file is copied, it will always allow you to have a Linux distribution at hand that will survive even a reinstallation of the system.
')

Mac Installation Instructions


All commands need to run from macOS.

BootCamp, rEFInd, and any other non-standard configurations do not matter if the boot configuration does not matter. Mac computers no older than 2009 are supported (performance on older ones has not been verified, but it can earn).

  1. Download OneFileLinux.efi file (20 megabytes)
  2. Mount EFI-partition in the system.

    diskutil mount disk__ 

    You can find out the EFI partition number using the diskutil list command.


    In my case, the command will look like this:
     diskutil mount disk0s1 

  3. Copy OneFileLinux.efi to EFI section

     cp ~/Downloads/OneFileLinux.efi /Volumes/EFI/ 

  4. Add boot option to NVRAM

     bless --mount /Volumes/EFI --setBoot --nextonly --file /Volumes/EFI/OneFileLinux.efi 

    The nextonly option means that this boot option will be executed once. The next reboot will restore the previous settings. Therefore, to return from Linux to macOS, just reboot again.

The latest versions of macOS, starting with El Capitan, use System Integrity Protection (SIP) technology, the so-called “no root mode”. This technology prohibits the modification of system files and variables, even the superuser. SIP is enabled by default, so the last bless command will return an error. It can be done from the Recovery Mode. To do this, hold down cmd + R when you turn on the computer and open the console
Utilities -> Terminal . In the console, perform steps 2 and 4. Commands in the Recover console need to be entered without sudo .

Now, every time you need to download OneFileLinux.efi, it’s enough to perform steps 2 and 4 in the Recovery console, or from the main system if SIP is turned off.
You can probably do bless without mounting the partition, but I didn’t find how to do it. Then one command would be enough.

PC instructions


Download options on the PC set. If your motherboard has a built-in UEFI Shell, it is enough to specify the path to the OneFileLinux.efi file in it to boot into Linux once. I will describe the setup process on my Thinkpad X220.

  1. Download OneFileLinux.efi and put it on the EFI partition
  2. Add download option to NVRAM

     efibootmgr --disk /dev/sda --part 2 --create --label "One File Linux" --loader /OneFileLinux.efi 
  3. During boot, press F12 and select the desired option.



Assembly instructions from source


Project sources github.com/zhovner/OneFileLinux
The distribution is built on a clean kernel 4.16-rc1 with kernel.org and the Alpine Linux Mini Root filesystem .
It can be easily assembled by yourself.

Preparing initramfs


Initramfs is a disk image that is mounted in memory when the kernel boots. In regular distributions it contains the drivers required at an early stage of loading. You can put a full root file system into it.

I used the root filesystem from Alpine Linux. This is a minimalistic distribution for embedded systems and containers. It has a delivery option without a kernel and pre-installed programs, only the root file system based on busybox and the package manager apk .

chroot-name alpine linux:

 chroot ./alpine-minirootfs /bin/ash 

Being inside the environment, you can make the necessary changes. Add packages via “apk add”, modify services using openrc.

All the necessary information is in the wiki .

Assembly


Run the build script. It will assemble the kernel modules, put them in the initramfs, and assemble the kernel.
 ./build.sh 


Help !! 111



At the moment, the distribution is pretty curved. If you know Linux, I will be very grateful for any help. It will be cool if you can finish this project to an acceptable level.

Known issues:


I invite everyone to commit and create issues about problems github.com/zhovner/OneFileLinux .

Source: https://habr.com/ru/post/349758/


All Articles