Typo
[linux_from_scratch_hints.git] / OLD / lfs_next_to_existing_systems.txt
blobb6ee8ee924a87bc23898dff4d6c27d9a82e9dda0
1 TITLE:          Install LFS next to existing systems on the same partition (v2)
2 LFS VERSION:    3.0 (but should work for later versions, too)
3 AUTHOR:         Matthias S. Benkmann <m.s.b@gmx.net>
5 SYNOPSIS:
6         This hint explains how you can install your new LFS system on a
7         partition (typically the / partition) already occupied by another
8         Linux system.
9         Compared to the one-partition-hint.txt, this hint has the
10         following advantages:
11         
12         -does not require the use of a loopback mounted filesystem. This means
13          that you never have to recompile your host kernel for this hint.
14         -does not require destroying or modifying the host system
15         -host and new LFS system will *both* be bootable without loss of 
16          performance to either.
17         -can be used to install several LFS systems in parallel on the
18          same partition. Note that this hint does *NOT* require the 
19          installation of a special boot manager to achieve this.
21 HINT:
22 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23 NOTE: I DO NOT TAKE RESPONSIBILITY FOR ANY DAMAGE CAUSED BY THESE 
24 INSTRUCTIONS! ALL THAT I GUARANTEE IS THAT I WROTE THEM WITH BEST INTENTIONS!
25 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27 #############################################################################
28                             CHANGELOG
29 #############################################################################
31 2002-02-18   -moved fixing of mount points to before reboot, to prevent
32               (harmless) mountfs failure when rebooting to make LFS the 
33               primary system
34              -changed build instructions for static mount to something less
35               typo-prone
36              -minor textual changes 
37              -submitted v2
39 #############################################################################
40                             REQUIREMENTS    
41 #############################################################################
43 This hint requires at least a 2.4.x kernel on the LFS system you want to
44 build (the host system's kernel can be anything).
45 Beginning with LFS 3.0, the book uses a 2.4.x kernel, so if you are building
46 by a recent version of the book, this requirement is met.
48 This hint assumes that you install your LFS system in a top-level directory
49 on the partition you want to use, such as /lfs or /mnt/lfs (assuming you
50 have a different partition mounted on /mnt, otherwise this would not be
51 a top-level dir)
52 Other directories are possible but may cause problems (e.g. installing in
53 /bin/lfs is a stupid idea). You should not use a subdirectory unless you
54 have used this hint at least once and understand what you have to change.
58 #############################################################################
59                         BUILDING THE LFS SYSTEM
60 #############################################################################
62 You build the LFS system as described in the book (well, of course you
63 skip the creation of a new partition and filesystem). After installing bash
64 in chapter 5, make a backup copy of the static bash
66    cp $LFS/bin/bash $LFS/bin/static-bash
69 After installing sh-utils in chapter 5, make a backup copy of the static chroot
71    cp $LFS/usr/bin/chroot $LFS/usr/bin/static-chroot
73 No special precautions have to be taken when building the rest of chapter 5
74 and 6.
77 After installing all the packages in chapter 6, do the following
78 (outside of chroot):
80 1. compile a static version of the mount utility according to the following
81    instructions
82    
83    cd util-linux-* &&
84    ./configure &&
85    cp defines.h defines.h.old &&
86    sed /ENABLE_NLS/d defines.h.old >defines.h &&
87    make -C lib &&
88    make -C mount LDFLAGS=-static &&
89    cp mount/mount $LFS/bin/static-mount
90    
91 2. create an empty directory $LFS/old-distro.
93 3. Now create the following script as $LFS/sbin/init2 
94    You have to replace
95    "<LFS directory>" with the directory where your LFS system is located
96    RELATIVE TO THE PARTITION IT IS ON. If you installed in /mnt/lfs for
97    instance this would be just "/lfs". If you installed in /lfs (i.e. on
98    the / partition), this is "/lfs", too, of course.
99    Note that you can *NOT* use the
100    variable "$LFS" as it is not available when init2 is started.
102 ------------------- $LFS/sbin/init2 ------------------------------------------
103 #!<LFS directory>/bin/static-bash
104 export PATH=<LFS directory>/sbin:<LFS directory>/bin:<LFS directory>/usr/sbin
105 export PATH=$PATH:<LFS directory>/usr/bin
106 static-mount -n --bind / <LFS directory>/old-distro
107 exec static-chroot <LFS directory> /usr/bin/env -i /sbin/init "$@"
108 --------------------------------------------------------------------------
110 Make sure that env is located in $LFS/usr/bin (it usually is but you might
111 have moved it to /bin for some reason).
113 Make init2 executable with the command `chmod +x $LFS/sbin/init2'
116 #############################################################################
117                       CREATING SYSTEM BOOT SCRIPTS
118 #############################################################################
120 You have to make the following changes to the checkfs and mountfs boot 
121 scripts when creating them:
123 - in both the start) and the stop) section change 
124      /bin/mount -? -o remount,r? /
125   to   
126      /bin/mount -? -o remount,r? /old-distro
127   i.e. replace / with /old-distro
128   
129 - in the stop) section of mountfs add the line
130      /bin/mount -o remount,ro /old-distro
131   before the line
132      /bin/umount -a -r
135 Make sure you did not forget to change the checkfs script, too!
139 #############################################################################
140                     MAKING THE LFS SYSTEM BOOTABLE
141 #############################################################################                    
143 Execute the instructions from "Making the LFS system bootable" with
144 the following changes:
146 1. In the /etc/fstab file you create, *replace* "/" with "/old-distro" 
147    IN THE ENTRY FOR THE ROOT FILESYSTEM (not everywhere!), 
148    so that /etc/fstab does not have a / entry but only
149    a /old-distro entry. IMPORTANT! Give this entry the `noauto' option.
151 2. When preparing your boot loader, make the following changes:
153 If you use LILO (as the book):   
154 - <partition> is the partition you new LFS system is located on, e.g. the
155   same as your host distro's / if you installed in /lfs, or the partition
156   mounted at /mnt if you installed in /mnt/lfs.
157   This is not really a change from the standard LFS instructions. I just
158   repeat it in case you were wondering if it still holds.
159   
160 - "image=/boot/lfskernel" must be changed to 
161   "image=<LFS directory>/boot/lfskernel"
162   <LFS directory> must be replaced with the appropriate directory as in
163   the creation of the init2 script above.
165 - make LILO pass "init=<LFS directory>/sbin/init2" to the kernel. You do
166   this by adding the line 
167      append="init=<LFS directory>/sbin/init2"
168   to the boot entry for your new LFS in lilo.conf.
169   
170 If you use GRUB:
171 - change the kernel line of the menu.cfg entry you created for your new LFS
172   system so that it uses <LFS directory>/boot/lfskernel as the kernel and
173   passes "init=<LFS directory>/sbin/init2". The line should look similar to
174   the following
175   
176     kernel /lfs/boot/lfskernel root=/dev/hda1 ro init=/lfs/sbin/init2
177   
178   Replace /lfs with the appropriate value for <LFS directory> as in the
179   creation of the init2 script above. Replace /dev/hda1 with the appropriate
180   partition (e.g. the same as your host system's / if you installed in /lfs, 
181   or the partition mounted at /mnt if you installed in /mnt/lfs).
182   
183   NOTE: Unless you reinstall GRUB, you have to add the entry for the LFS
184   system to the menu.lst OF YOUR HOST SYSTEM. Just creating a menu.lst
185   in $LFS/boot/grub won't achieve anything. It is best to sync the menu.lst
186   from your host system with that from your LFS system.
188 3. In the unlikely case that the partition of your new LFS system does not 
189    already have a /dev directory (e.g. because you're installing on the /usr
190    partition of your host system), you have to create one. 
191    The easiest way is to copy the one from your LFS
193      cp -a $LFS/dev $LFS/..
195 4. Now continue with the book. Everything should work as advertised and if you
196    did everything right, you should be able to boot into your LFS system
197    without problems.
198    Via the directory /old-distro you have access to the real / of the 
199    partition and to the files of your host distribution. Note that the
200    files in /old-distro may belong to users that do not exist on the LFS 
201    system so that ls only shows numeric UIDs and GIDs.
204 #####################
205 GOTCHAS
206 #####################
208 1. You may or may not see errors from umount regarding "/dev/root: not found" 
209    and/or "/: not mounted" during shutdown. These are harmless.
211 2. If you want to remount the / partition, you have to actually remount
212    /old-distro
214    NEVER unmount /old-distro. If you do this, you won't be able to remount /
215    read-only anymore, i.e. you won't be able to shutdown cleanly.
216    Note that `umount -a' unmounts /old-distro, so don't use this command. 
217    "mountfs stop" is safe because it remounts /old-distro (and hence /) 
218    read-only prior to calling umount -a (provided you changed mountfs properly
219    as you were told above). However after `mountfs stop' there is
220    no way to remount / read-write anymore, so don't use `mountfs stop' unless
221    you're shutting down your computer.
223    If it happens to you that you do unmount /old-distro by 
224    "accident" (read: "stupidity"), do the following:
225    1. kill all processes and log out on all terminals
226    2. log in as root
227    3. Issue the command 
228          /bin/sync 
229       3 times. Say "I don't want a corrupted filesystem" aloud and knock on your 
230       head after each time (this is !important! Do not
231       complain to anyone about corrupted filesystems if you did not follow the
232       sync, speak, knock on head, sync, speak, knock on head, sync, speak, 
233       knock on head order precisely)
234    4. /sbin/reboot 
235       fsck should start automatically after the reboot, but there
236       should not be any corruption (at least if you did step 3 properly)
239 ##############################################################################
240                        SWAPPING HOST AND LFS SYSTEM
241 ##############################################################################
243 Right now your LFS system is booted via chroot. This does not have any
244 adverse effects on performance but it doesn't "feel right" so once you are
245 content with your LFS system you will want to make it the primary system.
246 This is a bit tricky. The following instructions don't delete any
247 files but if something goes wrong, it is possible that neither your LFS nor
248 your host distro boots anymore. You should keep a Linux boot disk or CD
249 handy. Now this is what you have to do:
251 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
252 NOTE: I DO NOT TAKE RESPONSIBILITY FOR ANY DAMAGE CAUSED BY THESE 
253 INSTRUCTIONS! ALL THAT I GUARANTEE IS THAT I WROTE THEM WITH BEST INTENTIONS! 
254 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
256 1. Boot into your LFS system.
257    Make sure that you don't have any mount points in /old-distro (why would
258    you want to?). Then do
259    
260      cd /old-distro
261      mkdir old-distro  #this creates directory /old-distro/old-distro
262      for dir in * ; do
263        if [ ! $dir -ef / -a ! $dir -ef ./old-distro -a ! $dir -ef ./boot \
264                          -a ! $dir -ef ./lost+found ]; then
265          mv $dir old-distro
266        fi
267      done
268    
269    THIS IS THE POINT OF NO RETURN! AFTER THE ABOVE COMMANDS, NEITHER YOUR
270    LFS NOR YOUR HOST ARE BOOTABLE ANYMORE! YOU HAVE TO COMPLETE THE FOLLOWING
271    INSTRUCTIONS SUCCESSFULLY!
272    
273      cd /
274      for dir in * ; do
275        if [ $dir != old-distro -a $dir != boot -a $dir != lost+found ]; then 
276          ln -s <LFS directory>/$dir /old-distro/$dir
277        fi
278      done
279        
280    In the above, replace <LFS directory> as always with the directory of
281    your LFS system relative to its partition.
283    Note that these symlinks are currently broken.
284    They will work when booted without the init2 script, however.
285    
286 2. Change the config file of your boot manager and remove the "2" from
287    "init=<LFS directory>/sbin/init2" , i.e. we now want to start the real
288    init program, not the init2 script.
289    MAKE SURE YOU EDIT THE CORRECT CONFIG FILE. Depending on how you have
290    your boot manager set up, this is either /boot or /old-distro/boot.
291    It is best to synchronize those 2 to avoid trouble.
293 3. If your boot manager requires being reinstalled after changing the config
294    file, reinstall it (e.g. run `/sbin/lilo' for LILO).
295    
296 4. Unmount (or remount read-only) all filesystems EXCEPT /, /old-distro
297    and non-physical filesystems (such as proc, devfs, devpts,...). 
298    Note that you MUST NOT use umount -a. You have to issue a `umount -r' 
299    command for every filesystem listed in /proc/mounts except for the ones 
300    mentioned above.
301    It may be necessary to kill some processes before the unmounting will
302    succeed. Switching to runlevel 1 should usually give you a system with no
303    processes running to keep your filesystems busy.
304    
305 5. Change the /etc/fstab of your LFS system to list / instead of /old-distro.
306    Also change the `noauto' to `defaults' (or whatever you normally use).
307    
308 6. Change checkfs and mountfs to what they are on a normal LFS system, i.e.
309    undo the changes you were told to make in CREATING SYSTEM BOOT SCRIPTS
310    above.
312 7. Now we have to fix the mount points. Check your /etc/fstab to find out
313    which directories are mount points in / (i.e. mount points that contain
314    only one "/". "/dev/pts" for instance does NOT count.). 
315    One directory that is always a mount point in / is /proc. Let's fix it:
316    
317      cd /old-distro    
318      rm proc
319      mkdir proc
320    
321    Now proceed with the other mount points. Remove the symlink in /old-distro
322    and create a real directory instead.
323    NOTE: You don't really need to do this for all mount points, only for those
324    that are mounted automatically at boot. Mount points marked "noauto" can
325    be skipped.
327 8. Now remount your root fs read-only with
329      /bin/mount -o remount,ro /old-distro
331    and then reboot and choose the boot entry for your new LFS system again
332    (your host system doesn't work anymore).
334 9. Your LFS system should work as usual but every directory <dir> in / (except
335    for old-distro/, <LFS directory> and some mount points) is just
336    a symlink to <LFS directory>/<dir>. 
337    This is not pretty, so we are going to fix it now.
338    
339    First we have to check the mount points. If you did everything right so
340    far, then there should be no mount points that are symlinks into 
341    <LFS directory> anymore (at least none that have a filesystem currently 
342    mounted).
343     Use the command
344    
345      cat /proc/mounts
346      
347    to get a list of all mounted filesystems. The dangerous entries will look 
348    like this
349    
350      foo   <LFS directory>/bar   type   rw   0 0
351    
352    Note that only mount points directly under <LFS directory> are dangerous.
353    A mount point "<LFS directory>/xyzzy/bar" is harmless because it's in a
354    subdirectory of <LFS directory>.
355    
356    If you did indeed forget a mount point <LFS directory>/bar, 
357    do the following
358    
359      umount <LFS directory>/bar
360      cd /
361      rm bar
362      mkdir bar
363    
364    Do the same with other mount points in <LFS directory>. Unmount, remove the 
365    symlink and create a real directory instead.
366    
367    NOTE: If you absolutely can't unmount a certain filesystem, this doesn't
368    matter AS LONG AS YOU REMOVE THE SYMLINK. THERE ***MUST NOT*** REMAIN ANY
369    SYMLINKS IN / THAT POINT TO MOUNT POINTS THAT ARE CURRENTLY IN USE!
370    
371    WARNING!!!  WARNING!!!  WARNING!!!  WARNING!!! 
373    TRIPLE CHECK THAT ALL SYMLINKS IN / POINT TO REAL DIRECTORIES AND ***NOT***
374    TO MOUNT POINTS THAT ARE CURRENTLY IN USE.
375    
377    Okay, now that we have made *positively sure* that none of the symlinks in
378    / point to mount points that are in use, we can continue.
379    
380    First we have to turn /tmp into a real directory:
381    
382      rm tmp
383      mv <LFS directory>/tmp .
384    
385    Now we need a few preparations
386      
387      export LD_LIBRARY_PATH=<LFS directory>/lib:<LFS directory>/usr/lib
388      cp /bin/mv /tmp
389      cp /lib/ld-linux.so.2 /tmp
390    
391    Finally we can get rid of the rest of the symlinks.  
392    NOTE: If something goes wrong with the moving of the /lib directory,
393    don't panic. <LFS directory>/sbin/sln is a statically linked ln that
394    you can use to set up symlinks to make it work again.
395    
396      cd /
397      for l in * ; do
398        if [ -L $l ]; then
399          source=$(find $l -printf "%l\n") &&
400          rm $l &&
401          /tmp/ld-linux.so.2 /tmp/mv $source $l
402        fi
403      done
404      
405      rm /tmp/mv
406      rm /tmp/ld-linux.so.2
407      
408 10. Finally change the entry in your boot manager's config file to what it is
409    really meant to be. Remove the "init=<LFS directory>/sbin/init"
410    and change "<LFS directory>/boot/lfskernel" to "/boot/lfskernel".
411    Note that if you followed the instructions exactly, /boot is still the 
412    old /boot from the host distro. Synchronize with /lfs/boot if necessary.
413    Don't forget to run /sbin/lilo if you use LILO after you changed your
414    lilo.conf.
416 11. Just to be safe you should now unmount all filesystems or remount them
417    read-only. Then you can reboot and
418    everything should be fine. Your old distro can be found in /old-distro .
419