Linux in one file for Macbook



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

    Main advantages


    • It works on any computer with UEFI - the same file will work on PC and Mac.
    • It does not require installation - just copy one file to the EFI-partition of the disk and specify the path to this file in the NVRAM variables.
    • No need to install GRUB bootloaders, rEFInd - the Linux kernel built with EFI Stub support can be loaded directly without an intermediate bootloader.
    • No USB flash drives needed - the distribution package copied to the EFI partition remains there forever and can be downloaded at any time. It does not take up space on the partition of the main system, since the EFI partition is not used in the OS.
    • Does not change the boot process - the system can be booted once, without changing the boot order in the UEFI settings. The next reboot of the computer will load the normal operating system. There will be no trace of Linux in the boot order.
    • Compatible with FileVault disk encryption, etc. - the file is copied to the EFI System Partition, a special reserved disk partition. On Mac computers, its size is about 200 megabytes. It is dedicated to Boot Camp and is usually not used.

    Why is this needed?


    For all cases when you need native Linux without a virtual machine.
    To use PCIe devices in Linux when they cannot be forwarded to a virtual machine. For example, a built-in Wi-Fi card for packet injection. When it is too lazy to install a virtual machine and download a large ISO file with a distribution kit. When you do not want to mess with USB flash drives.
    Once the copied file allows you to always have at hand the Linux distribution, which will survive even reinstalling the system.

    Mac Installation Instructions


    All commands must be run from macOS.

    The boot configuration does not matter, the method does not interfere with the operation of BootCamp, rEFInd and any other non-standard configurations. Mac computers not older than 2009 are supported (performance was not tested on older ones, but it may work).

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

      diskutil mount disk_номер_раздела 
      

      You can find the EFI partition number with the diskutil list command .


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

    3. Copy OneFileLinux.efi to the 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 download 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 to the superuser. SIP is enabled by default, so the last bless command will return an error. It can be performed from Recovery Mode. To do this, hold cmd + R when you turn on the computer and open the
    Utilities -> Terminal console . In the console, perform steps 2 and 4. Commands in the Recover console must be entered without sudo .

    Now, every time you need to download OneFileLinux.efi, it is enough to follow 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 have not found how to do this. Then one team would be enough.

    Instruction for PC


    There are many download options on the PC. If your motherboard has a built-in UEFI Shell, just 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 section
    2. Add boot 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 option you want.



    Assembly instructions from source


    Sources of the github.com/zhovner/OneFileLinux project The
    distribution is compiled on a clean 4.16-rc1 kernel from 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, the drivers required at an early stage of loading are placed in it. You can put a full-fledged root file system into it.

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

    chroot in 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 initramfs, and assemble the kernel.
    
    ./build.sh
    


    Help !! 111



    At the moment, the distribution is quite crooked. If you know Linux, I would really appreciate any help. It will be cool if you can finish this project to an acceptable level.

    Known Issues:

    • Disabled loading of kernel modules - all drivers are compiled into the kernel.
      It’s probably right to make them separate modules and put them into initramfs

      ✓ Fixed
    • Fonts on HiDPI displays - Due to the huge resolution of HiDPI screens,
      standard 8x16 fonts look very small. I compiled a 16x32 font that looks normal with a high pixel density, but too large for regular screens. In a good way, the font should be selected depending on the screen resolution.

      ✓ Fixed. Now a large font is automatically selected for HiDPI screens. You can also change the font size at any time with the fontsize command.
    • Only one WiFi card driver - now one driver for the Broadcom 43602 adapter integrated into the MacBook is compiled. In a good way, you need to collect all the popular drivers in the form of kernel modules, as well as firmware for them.
      ✓ Fixed. Added support for many popular WiFi adapters.
    • Is udev / mdev broken? “I don't know how it works.” How to load modules depending on hardware configuration?
      ✓ Fixed
    • Garbage - now a large number of outdated drivers are installed with the kernel. In uncompressed form, kernel modules weigh more than 100 megabytes. Most of them can be excluded.

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

    Also popular now: