Skip to content

Instantly share code, notes, and snippets.

@vasi
Created February 14, 2014 01:37
Show Gist options
  • Save vasi/8994281 to your computer and use it in GitHub Desktop.
Save vasi/8994281 to your computer and use it in GitHub Desktop.
LFS on OS X from 2005 or so
= LinuxFromScratch using Mac OS X =
Linux From Scratch is a fun way to learn about Linux, but it expects you to already have Linux running. [http://trac.cross-lfs.org Cross-compiling] is already supported but OS X is a little weird--so here are some hints for bootstrapping LFS from OS X. I've only tried this on 32-bit PPC, I have no idea how well or badly it would work on G5 or Intel chips.
Generally, you can follow the [http://cross-lfs.org/view/1.0.0rc4/ppc/ Cross-LFS book], I only refer to areas where changes are needed.
== Chapter 2 ==
=== Partitioning ===
To perform the partitioning, you'll probably want to use a Linux rescue CD. Gentoo's PPC CD has a version of parted that can even resize existing HFS+ partitions non-destructively, I think Debian's does too. Using Disk Utility or a commercial tool like iPartition is an option, but I'm not certain they can create the special partition types Apple_Bootstrap and Apple_UNIX_SVR2.
In order to install LFS from OS X, you'll need a partition that can be read and written by both OS X and Linux, and that supports symlinks. As far as I know, only two such filesystems exist:
'''ext2/3'''::
Great linux drivers, but the [http://sourceforge.net/projects/ext2fsx/ OS X driver] is non-standard and still in development. It apparently work for most people, but there have been reports of bugs as well.
'''HFS+'''::
Great OS X drivers, and supported in Linux. However, you must have journaling turned *off* on a HFS+ partition in order for Linux to write to it, you can change this setting with Disk Utility on OS X.
I chose to do the initial install on a HFS+ partition, and then transfer it to a ext3 partition once I no longer needed OS X. Here's what my partition map looked like:
|| hda0 - hda8 || Apple hidden partitions ||
|| hda9 || HFS+ OS X boot disk (with journaling) || 30 GB
|| hda10 || HFS+ OS 9 boot disk (no journaling) || 512 MB
|| hda11 || Bootstrap partition || 800 KB
|| hda12 || Swap partition || 896 MB
|| hda13 || Final linux partition || 6.6 GB
I backed up and wiped my OS 9 partition, so that the initial Linux install could go there. You can't format the bootstrap parition or swap or ext2 filesystems under OS X (at least without the third-party driver mentioned above), so I left those uninitialized for now.
=== Setup ===
* There is no adduser or addgroup on OS X for creating the clfs user. You can either do everything as you regular admin account, or use the Accounts preference pane to add the new user.
* Some GNU utilities have different options from their OS X counterparts, you'll need at least GNU sed and GNU fileutils before starting Chapter 3. They're easily available from Fink, Darwinports, and many other sources. Make sure you put them at the front of your PATH!
== Chapter 3 ==
You can begin creating your cross-compilation toolchain on OS X, following the book. The only difficulty here is compiling glibc, which expects to be on a case-sensitive file-system. Most OS X users use case-insensitive HFS+, so glibc will confuse ''foo.os'' with ''foo.oS''. The following script tells it to use ''.on'' instead of ''.oS'':
{{{
find . -type f | xargs perl -ne 'print "$ARGV\n" if /\.oS/ && $ARGV !~ /ChangeLog/' \
| uniq | xargs perl -i.bak -pe 's/\.oS/.on/g'
}}}
Glibc also tries to use a native ''readelf'', which doesn't exist. Instead it should use the cross-readelf installed with binutils:
{{{
perl -i.bak -pe "s,readelf,$CLFS_TARGET-readelf,g" configure
}}}
== Chapter 4 ==
Since we're on a different platform, we have to reboot instead of chrooting of course. Some of the packages built in this section need minor fixes, here are most of them. I'm sure I forgot one or two minor problems, please tell me if you notice.
* Make has somewhat different default rules on OS X, so when building '''''binutils''''', it thinks that bl_flat.m is an Objective-C file when it is not. Just build with "`make -r`", to prevent make from using default rules.
* '''''texinfo''''' has a special "feature" while cross-compiling, where it compiles twice--once for the host (so it can install its own info files) and once for the target. It tries to access the host compiler by clearing the environment, but it's overzealous. It removes ''HOME'', and Apple's GCC won't work with ''HOME'' undefined. The following script fixes things:
{{{
perl -i.bak -pe 's/tools_only=1 /$& HOME="\$HOME"/g' configure
}}}
* '''''zlib''''' uses ''uname -s'' to determine how to build a shared library, but that clearly won't work when cross compiling. Just hardcode linux:
{{{
perl -i.bak -pe 's/uname -s/linux/g' configure
}}}
* '''''udev''''' uses the ''-D'' option to ''install'', and even my GNU install doesn't seem to have that. Just remove it:
{{{
perl -i.bak -pe 's/\s-D\s/ /g' Makefile
}}}
* '''''yaboot''''' has an ancient makefile. In some places it forgets to use the cross-compiler, in others it insists on using ''/usr/include'' instead of the new linux headers. Fix it with [http://vasi.dyndns.org:3128/trac/browser/clfs/trunk/fixes/yaboot-patch.sh the patch here].
=== The Linux kernel ===
The kernel can be particularly difficult to get right. Here are some patches:
* The makefile for ''lxdialog'' (ie: ''menuconfig'') doesn't [http://vasi.dyndns.org:3128/trac/browser/clfs/trunk/fixes/linux-patch/lxdialog.patch look for curses in the right places].
* There is no ''memmem()'' function on OS X, we can [http://vasi.dyndns.org:3128/trac/browser/clfs/trunk/fixes/linux-patch/memmem.patch steal the one from glibc].
* The book says to create ''/dev/null'' and ''/dev/console'' with ''mknod'', but OS X ''mknod'' doesn't seem to make device files that Linux can use. Instead, we'll [http://vasi.dyndns.org:3128/trac/browser/clfs/trunk/fixes/linux-patch/mknod.patch hack the kernel] to generate these files on startup.
If you try to use a minimal kernel configuration, it might take several tries to catch all the drivers you need. Unless space and time are at a high premium, I recommend just starting with the default config, like so:
{{{
make ARCH=powerpc CROSS_COMPILE=${CLFS_TARGET}- KBUILD_DEFCONFIG=pmac32_defconfig defconfig
make ARCH=powerpc CROSS_COMPILE=${CLFS_TARGET}- oldconfig
make ARCH=powerpc CROSS_COMPILE=${CLFS_TARGET}- menuconfig # To fix any settings
}}}
The only change you'll need to make from the default config is to disable netfilter. It uses files whose names differ only by case, so some of them get clobbered when unpacking the tarball. This is just the kernel for your temporary system, you can safely enable netfilter for the final kernel which you will build in Chapter 5.
If you are installing onto HFS+, beware that OpenFirmware doesn't understand HFS+ symlinks. So if you have ''vmlinux'' linked to ''vmlinux-2.6.x'', your ''yaboot.conf'' has to point to ''vmlinux-2.6.x''.
== Chapter 5 ==
Once you've rebooted your new system you can format all the partitions you weren't able to before, with ''mkfs.ext3'' and ''mkswap''. (NB: My mkfs.ext3 segfaulted here, so I used the one from [http://busybox.net/ BusyBox] instead.) You can also write an appropriate ''yaboot.conf'', and use ''ybin'' to make your Linux partition available without needing to boot to the OpenFirmware console.
If you do as I did, installing first on HFS+ and then moving to ext3, this is the time to switch. Mount your ext3 partition, and then do:
{{{
cd /
tar --one-file-system -cf - . | (cd /mnt/linux; tar -xf -)
}}}
You should remove and recreate ''/dev/console'' and ''/dev/null'' on the ext3 partition, since they seem to get mixed up when moving across file-systems. Modify your ''yaboot.conf'' to point to the new partition, run ''ybin'', and reboot to ext3 goodness.
=== That's it! ===
After this point, the rest of the CLFS book should be perfectly valid. Er, unless I forgot anything!
== See Also ==
* Martin Schaffner's somewhat dated [http://www.linuxfromscratch.org/hints/downloads/files/lfs-from-osx.txt LFS from OS X] hint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment