1 AUTHOR: Matthias S. Benkmann <article at winterdrache dot de>
5 LICENSE: Creative Commons Attribution-NoDerivs 2.0
6 (http://creativecommons.org/licenses/by-nd/2.0/)
8 SYNOPSIS: Install LFS next to an existing OS, not on a fresh partition (v1.0)
11 This hint presents a simple and safe way to install one or more LFS systems
12 on a pre-existing partition that may already contain data from other (even
13 non-Linux) operating systems. The different systems will be bootable
14 independent from each other and run with no loss of performance.
15 If you want to build an LFS system but can not or do not want to give it its
16 own dedicated partition, this hint is for you.
19 http://www.linuxfromscratch.org/hints/downloads/attachments/lfs_next_to_existing_systems/lfs_next_helpers.tar.bz2
26 ###########################################################################
28 ###########################################################################
31 1.1 Choosing the LFS Partition
32 1.2 Creating the LINUX_FROM_SCRATCH Directory
33 1.3 Matching Up Hint and Book
35 2. Building the LFS System
37 3. Making the LFS System Bootable
38 3.1 First Read Then Proceed
40 3.3 The Solution: pre-init
41 3.4 The Boot Manager is Special
42 3.5 Making the LFS System Bootable
45 4. Accessing the Full LFS Partition
47 5. Elevating the LFS system to /
49 5.2 Don't Saw the Branch You're Sitting On
50 5.3 Don't Break Your Boot Manager
51 5.4 Don't Touch Special Directories and Files
53 A. Acknowledgements and Changelog
56 ###########################################################################
58 ###########################################################################
60 Just to avoid misunderstandings let me make clear that this hint is *not*
61 about cross-compiling an LFS system from a non-Linux operating system. This
62 hint addresses only the aspects relevant to building and booting an LFS system
63 next to other data on an existing partition rather than using a fresh, empty,
64 dedicated partition. This hint is mostly independent of the actual instructions
65 used to build the LFS system, so you may use this hint in conjuction with a
66 cross-compiling hint. However, this hint is written under the assumption that
67 you are following the LFS book, i.e. you're using a Linux system as host for
68 the build, and any shell commands given in this hint are meant to be executed
72 ###########################################################################
74 ###########################################################################
76 1.1 Choosing the LFS Partition
77 ------------------------------
79 The LFS book explains how to create a new partition for your LFS system and
80 how to set it up properly. When you use this hint you don't need to do this.
81 Instead you pick one of your existing partitions to install on. There are
82 some conditions that a partition needs to fulfill in order to be usable with
83 this hint. If your chosen partition belongs to a Linux system (e.g. your host
84 distro), condition 1 is probably the only thing you need to worry about. But
85 if you want to install to a partition from a non-Linux OS, read them all
88 1) The partition needs to have enough free space.
90 This restriction should be obvious. See the LFS book for an estimate of how
91 much space you are going to need.
93 2) The filesystem used on the partition must offer the usual Linux/Unix
94 features such as user/group/other permissions, hard and symbolic links.
96 This hint does not use loopback-mounted files or other tricks to install
97 one filesystem on another filesystem. Therefore the target partition's
98 filesystem needs to support the features necessary for a Linux installation
99 directly. And of course these features need to be supported by the Linux
100 driver for that filesystem. Unfortunately this rules out VFAT and NTFS,
101 so installing LFS on a Windows partition is not possible with this hint.
102 But partitions from other *nixes may work (e.g. the HFS+ filesystem from
103 Mac OS X has been confirmed to support all features, including device
105 If you are unsure about your chosen partition, contact me or ask on the
106 lfs-support mailing list.
107 Note that Linux device nodes don't need to be supported on the target
108 partition. A workaround for partitions that don't support Linux device nodes
109 is offered later in this hint.
111 3) The partition needs to be usable as a root partition.
113 This restriction is usually a non-issue. It just means that the kernel
114 needs to be able to use the chosen partition as root filesystem. Partitions
115 that satisfy condition 2 should also satisfy this condition, but there may
116 be some filesystem that the kernel can't use as root fs.
118 4) If there is a file /dev/console on the partition, attempting to open it
121 The Linux kernel attempts to open /dev/console on the root partition during
122 the boot. If this file does not exist, is a regular file or an actual
123 Linux device node for the console this is harmless, but if it is something
124 else (e.g. a device node for a non-Linux OS), you should make sure that
125 the kernel's attempt to open it is safe. Contact me or ask on lfs-support
126 if you do indeed have a weird /dev/console on your target partition and
127 are unsure about its safety. BTW, it is trivial to patch the Linux kernel
128 to not open /dev/console and this will be described later in this hint, so
129 even if you should have an unsafe /dev/console, this need not be a
133 To find out if you do have a /dev/console on your chosen target
134 partition, you need to boot with a system that does not use the /dev
135 directory on that partition (e.g. boot from a rescue disk). Many
136 systems use virtual /dev directories that contain files only while the
137 system is running. These virtual files don't matter, because they
138 won't exist when you boot your LFS system.
141 1.2 Creating the LINUX_FROM_SCRATCH Directory
142 ---------------------------------------------
144 After you've chosen a target partition, mount it if it isn't already.
145 To keep the files of the new LFS system separate from those of other operating
146 systems, create a new directory LINUX_FROM_SCRATCH somewhere on
147 the target partition. It does not need to be a top-level directory and the
148 name is arbitrary. This hint will refer to this directory as
149 /.../LINUX_FROM_SCRATCH. Substitute the appropriate path to your LFS directory
150 whenever you see this.
153 1.3 Matching Up Hint and Book
154 -----------------------------
156 The LFS book is written under the assumption that the new LFS system will be
157 installed to the root directory / of the target partition. But with this hint
158 you will install into the /.../LINUX_FROM_SCRATCH directory instead.
159 Take for instance the following fragment from the LFS book:
161 Throughout this book, the environment variable LFS will be used
162 several times. It is paramount that this variable is always
163 defined. It should be set to the mount point chosen for the LFS
164 partition. Check that the LFS variable is set up properly with:
168 Make sure the output shows the path to the LFS partition's mount
169 point, which is /mnt/lfs if the provided example was followed.
171 If you followed these instructions exactly and set the variable LFS to the
172 mount point of the LFS partition, you'd install to the / directory of the
173 target partition. To match up the LFS book's instructions and this hint,
174 create a directory /mnt/lfs and use the following command:
176 mount --bind /.../LINUX_FROM_SCRATCH /mnt/lfs
178 to do a virtual mount of /.../LINUX_FROM_SCRATCH on /mnt/lfs. After doing this
179 your situation will match that in the LFS book exactly. You have a mount
180 point /mnt/lfs and it's where your LFS system will be built.
181 As a consequence you can set your LFS variable to /mnt/lfs just as done in the
182 standard book instructions.
184 Of course you could just as well skip the mount --bind command and substitute
185 /.../LINUX_FROM_SCRATCH whenever the book talks about "the mount point of
189 When the LFS book does not talk about the mount point, but about the
190 partition itself or its corresponding block device /dev/[xxx], the book's
191 instructions apply unchanged, i.e. "/dev/[xxx]" remains "/dev/[xxx]" and
192 does *not* become "/.../LINUX_FROM_SCRATCH/dev/[xxx]" or whatever.
195 ###########################################################################
196 2. Building the LFS System
197 ###########################################################################
199 Except for the points discussed in the previous chapter with respect to the
200 LFS partition and its mount point, the LFS book's instructions apply unchanged
201 up to and including the last package of Chapter 6. After you've installed that,
202 extract the lfs_next_helpers.tar.bz2 archive and install it (from inside the
203 chroot environment, just like the other Chapter 6 packages) using the following
208 This will install the program /sbin/pre-init, which allows booting of a Linux
209 system whose filesystem root is not the root directory of its partition (as is
210 the case when building according to this hint).
212 After installing lfs_next_helpers, continue with the book up to but not
213 including the chapter "Making the LFS System Bootable".
216 ###########################################################################
217 3. Making the LFS System Bootable
218 ###########################################################################
220 3.1 First Read Then Proceed
221 ---------------------------
223 If you're not already familiar with it, read the LFS book's chapter
224 "Making the LFS System Bootable" in its entirety now before you do anything
225 else. Then read the rest of this chapter in the hint. After that, follow
226 the instructions from the LFS book, modifying them as appropriate.
232 What would happen if you attempted to boot your LFS system with the
233 instructions from the book? Let's leave aside the boot manager related
234 problems for the moment. These are relatively minor and will be discussed
235 later. The major problem will manifest itself after the kernel has finished
236 loading and wants to start /sbin/init. init is the parent of all processes,
237 responsible for starting the boot scripts, for instance. The LFS book's
238 instructions build init dynamically linked by default. This means that for
239 loading init the kernel needs the dynamic linker whose path is hardwired in
240 the init binary (e.g. to "/lib/ld-linux.so.2" on the i386-pc-linux-gnu
242 Here's the first problem. The hardwired path starts with "/lib", but our
243 LFS system's lib directory is not /lib but /.../LINUX_FROM_SCRATCH/lib. So
244 unless you have another /lib directory on the target partition (e.g. from your
245 host distro) that contains a compatible dynamic linker, the kernel won't be
247 But the problems don't end there. init and the boot scripts want to read their
248 config files from the /etc directory, but the proper directory to read from is
249 /.../LINUX_FROM_SCRATCH/etc.
252 3.3 The Solution: pre-init
253 --------------------------
255 The solution to the problem above is similar to the way Chapter 6 is built.
256 Chapter 6 uses the chroot command to create an environment whose filesystem
257 root is /.../LINUX_FROM_SCRATCH. For solving the boot problem the same
258 method is used, but in a more extreme way. The new LFS system shall be
259 self-sufficient, so booting up a host system and then using chroot on that
260 host system to start the LFS system is unsatisfactory. Therefore the chroot
261 command has to be inserted into the boot process, before init is even started.
262 This way init itself will already work with the changed filesystem root and
263 because init is the parent of all processes, all other processes see only the
264 changed root, too. Furthermore LFS's own boot scripts will be responsible for
265 bringing up the system and not the scripts from a host OS.
267 The tool to make all of this happen is the pre-init program which you installed
268 from the lfs_next_helpers archive. pre-init uses the chroot(2) system call
269 (not the /bin/chroot binary) to change the filesystem root, performs some
270 other preparations necessary for a successful boot and then replaces itself
271 with the init from the LFS system, which will start up and work as if it were
272 installed on its own partition. Because pre-init is statically linked and
273 does not use any external binaries it has no dependencies on a host system.
276 3.4 The Boot Manager is Special
277 -------------------------------
279 Before you can set up your system to boot via pre-init, it is important that
280 you give special consideration to some aspects of the boot manager.
282 The first thing that you have to consider is that there can be only one
283 master boot manager in the master boot record (MBR) of a hard disk and only
284 one non-master boot manager per partition. The fact that you're using this
285 hint suggests that you have a special interest in keeping your other operating
286 system(s) untouched. Therefore you should think well about the consequences
287 before you make the decision to install a new boot manager. If you already
288 have a boot manager that can boot Linux systems (which is probably the case
289 if you're booting your host system from disk) there is usually no need to
290 install a new one. Don't forget that the boot manager has no influence on the
291 performance (or the "purity") of your LFS system. If your existing boot manager
292 can handle it, the safest and simplest course of action is to use it for
293 booting the new LFS system.
295 The other important detail you have to consider is that the boot manager is
296 loaded before pre-init can change the filesystem root. The boot manager always
297 sees a physical view of the disk and partitions. All paths it uses at
298 run-time will refer to the physical directory structure of the partitions
299 with no consideration of any mounts or changes in filesystem root that may be
300 active once the system is booted. An example should make this clear:
303 Your host system has a separate partition for /usr and you have chosen
304 that partition to install the new LFS system on.
305 You have created the directory /usr/LINUX_FROM_SCRATCH as the root
306 directory for the LFS system.
307 You have chosen to use the mount --bind trick suggested in this hint to
308 make /usr/LINUX_FROM_SCRATCH available as /mnt/lfs under the host system.
310 Viewed from inside Chapter 6's chroot environment, the stage* files of
311 the boot manager GRUB are found in /boot/grub.
313 Viewed from your host system outside chroot, the GRUB files are found in
314 /mnt/lfs/boot/grub as well as /usr/LINUX_FROM_SCRATCH/boot/grub.
316 The boot loader sees the files at (hd?,?)/LINUX_FROM_SCRATCH/boot/grub.
318 This issue is the most likely source of confusion when using this hint.
319 If booting doesn't work as expected, always check your paths. A typical
320 mistake would be to copy a new kernel to the directory /boot from inside
321 the new LFS system, to specify (hd?,?)/boot/<kernelname> in GRUB's menu.lst
322 and to be confused when GRUB complains that it can't find the kernel image.
323 Another typical mistake would be to add a new entry to /boot/grub/menu.lst
324 inside the new LFS system and to wonder why it doesn't show up in the boot
328 If you use LILO instead of GRUB you have to keep in mind that LILO does
329 not load config files at boot time and does not use the directory structure
330 to find the kernel image at boot time. It's all pre-computed at the time
331 you run /sbin/lilo. Therefore, when you use LILO, only the paths that you
332 pass on the kernel command line (e.g. the "init=..." parameter) have to
333 reflect the physical directory structure. The paths that are only read by
334 /sbin/lilo are ordinary user space paths.
336 I know little about LILO. Take the above with a grain of salt.
339 3.5 Making the LFS System Bootable
340 ----------------------------------
342 To make your LFS system bootable follow the instructions in the LFS book
343 (taking the issues mentioned in the previous section into account, especially
344 regarding the paths). When you come to the point of adding an entry for your
345 new LFS system to your boot manager's configuration, there is one small change
346 you have to make. Just add
348 init=/.../LINUX_FROM_SCRATCH/sbin/pre-init
350 to the kernel parameters. Make sure that /.../LINUX_FROM_SCRATCH reflects the
351 *physical* location of the directory on the partition. My entry in GRUB's
352 menu.lst looks like this
354 title LFS system in /lfs on /dev/hda2
356 kernel /boot/lfskernel-x.y.z root=/dev/hda2 init=/lfs/sbin/pre-init
358 Note that my kernel is /boot/lfskernel-x.y.z and not /lfs/boot/lfskernel-x.y.z.
359 /boot is the directory at the top-level of /dev/hda2, not the one in /lfs.
360 I take care to make sure that /boot and /lfs/boot are always in sync. In fact
361 I have set up /boot as a symlink to lfs/boot (Notice that the other way around,
362 i.e. /lfs/boot as symlink to ../boot wouldn't work!).
364 pre-init understands some options that you can pass on the kernel command line:
367 Overrides the directory to use as filesystem root. The default
368 is determined by stripping the suffix "/sbin/..." from the
369 location of the pre-init binary as passed to the kernel with
370 the "init=..." argument.
373 Overrides the program to execute after pre-init has completed
374 its preparations. The default is "/sbin/init". This option
375 is useful for instance when you want to boot with /bin/sh as
376 init for maintenance work.
377 <path> may be specified with or without a leading <lfsdir>
380 makedev Tells pre-init to mount a ramfs on <lfsdir>/dev and to create
381 device nodes for console, null and tty1 on this ramfs. This
382 allows you to boot from a partition that does not support
383 Linux device nodes. Note that this is *not* redundant, even
384 if your boot scripts contain commands to set up /dev.
385 Without a working <lfsdir>/dev/console (or alternatively
386 <lfsdir>/dev/null) the normal init used by LFS won't even get
387 to the point of starting your boot scripts.
389 pre-init will pass any command line arguments that it doesn't interpret on to
396 Before the kernel starts pre-init, it tries to open /dev/console as standard
397 input/output/error for it. It does this, because normal programs, including
398 the usual inits (but not pre-init), require that these standard file
399 descriptors are open.
400 If your LFS partition does not have a /dev/console at the top-level
401 (which it probably doesn't if you're not installing to the root partition of
402 another Linux system), then you'll get the following message:
404 Warning: unable to open an initial console.
406 This warning is harmless, because one of the things pre-init does when
407 preparing to boot the LFS system is to open the proper console for the new
408 LFS system, which is /.../LINUX_FROM_SCRATCH/dev/console.
409 The only problem caused by a missing /dev/console (aside from the annoying
410 warning message) is that if pre-init encounters an error that occurs before it
411 can open /.../LINUX_FROM_SCRATCH/dev/console (or while opening it), is that you
412 won't see pre-init's error message. The system will just appear to hang
413 and you have to guess at the problem (which is most likely an incorrect path
416 To overcome this problem there are several possibilities. The easiest one is
417 to just create /dev/console (e.g. by using `cp -a' to copy the console device
418 node from your host Linux distro to directory /dev on the target partition).
420 Another solution is to patch the kernel. Responsible for opening /dev/console
421 is the following statement in the file init/main.c of the kernel sources:
423 if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
424 printk("Warning: unable to open an initial console.\n");
426 Just replace "/dev/console" with "/.../LINUX_FROM_SCRATCH/dev/console" and
427 recompile the kernel. Make sure that /.../LINUX_FROM_SCRATCH is the *physical*
428 path on the partition.
431 ###########################################################################
432 4. Accessing the Full LFS Partition
433 ###########################################################################
435 The method described in this hint is designed to give the illusion that the
436 LFS system has its own dedicated partition. It's intended to look as
437 much as possible like a standard by-the-book LFS system. For that reason, the
438 other files on the partition outside of the /.../LINUX_FROM_SCRATCH directory
439 are not made visible in the LFS system by default. You should treat the
440 partition as if it was not mounted already. If you want to make the other
441 files on it available, mount it somewhere (e.g. /mnt) from inside the
444 Although it's no problem to have the same partition mounted several
445 times, there is one small issue that you should be aware of. If you mount
446 the LFS partition from inside the LFS system, you will not only get access to
447 the files outside of /.../LINUX_FROM_SCRATCH, you will also see a 2nd copy
448 of every file from your LFS system. This means, for example, that the command
449 `find / -name bash' will list your LFS system's bash shell twice, one time as
450 "/bin/bash" and a 2nd time as "/mnt/.../LINUX_FROM_SCRATCH/bin/bash" (assuming
451 you mounted the partition on /mnt). This can be annoying and may have
452 undesirable effects, such as archiving files twice when making a full backup.
453 There is a simple yet effective trick to get rid of the redundant directory
454 entries for the LFS system. Simply hide /.../LINUX_FROM_SCRATCH behind a
457 mount -t ramfs ramfs /mnt/.../LINUX_FROM_SCRATCH
459 This will make all the files in /mnt/.../LINUX_FROM_SCRATCH disappear. If you
460 automount your LFS partition at boot via an entry in /etc/fstab, e.g. because
461 you want to share a directory (e.g. /home) among different systems, you can
462 put a ramfs line corresponding to the above into fstab, too. Just make sure
463 it's listed after the line for the LFS partition.
466 Keep the /etc/passwd and /etc/group files synchronized among your LFS
467 and other systems that access the same partition. Otherwise a file created
468 by user foo:bar under the new LFS system may appear to be owned by sna:fu
469 when accessing the partition from another system, which can be a
473 ###########################################################################
474 5. Elevating the LFS system to /
475 ###########################################################################
480 At some point you may decide that you no longer want your LFS system confined
481 to /.../LINUX_FROM_SCRATCH, but want to move it up to the / level in the
482 directory hierarchy. In principle that's an easy task:
485 2. mv <contents of /> /old-root
486 3. mv <contents of /.../LINUX_FROM_SCRATCH> /
487 4. Remove "init=.../pre-init" from kernel command line
490 However, there are a few gotchas that you have to watch out for.
493 5.2 Don't Saw the Branch You're Sitting On
494 ------------------------------------------
496 The worst mistake you can make is to attempt to perform the above moving from
497 inside the LFS system, by just mounting the partition to /mnt without the
498 hide-behind-ramfs trick, and performing the moves inside /mnt. The problem
499 with this might not be obvious at first glance, but you must not forget that
500 if you mount your LFS partition to /mnt from inside the LFS system, what you
501 see in /mnt/.../LINUX_FROM_SCRATCH is the actual / directory of your LFS system
502 and all of the changes you make there will immediately affect /. Now assume
503 that you do `mv /mnt/.../LINUX_FROM_SCRATCH/lib /mnt/lib', what happens is
504 that instantly your /lib directory disappears. It should be obvious that this
507 While there are certain tricks that can be used to perform the move from inside
508 the LFS system, the safest way is to boot a different system and use
509 that for the move. It should be obvious that this system must not have any
510 crucial files on the LFS partition, so if your LFS partition is the /usr
511 partition of another system, for example, you can't use that system for doing
512 the move. The best thing to do in that case is to use a boot floppy or boot CD
516 5.3 Don't Break Your Boot Manager
517 ---------------------------------
519 Don't forget that your boot manager sees the physical directory structure of
520 the partition. Dependining on how you set things up, you may not want to move
521 around /boot and/or /.../LINUX_FROM_SCRATCH/boot.
524 5.4 Don't Touch Special Directories and Files
525 ---------------------------------------------
527 Some directories and files play a special role and must not be moved around.
528 An example of this is the lost+found directory on an ext2fs filesystem. Make
529 sure you don't accidentally move a special file or directory.
530 Remember: Glob patterns are your enemy and * is the worst of the bunch.
533 ###########################################################################
534 Appendix A: Acknowledgements and Changelog
535 ###########################################################################
538 * Martin Schaffner for his valuable feedback.
544 * Released version 1.0
547 * Hint completely rewritten.
548 * Changed license to CC by-nd 2.0.
549 * Changed versioning scheme to <major>.<minor> (starting at 1.0)