]> git.ipfire.org Git - thirdparty/mdadm.git/blame - README.initramfs
Don't close fds in write_init_super
[thirdparty/mdadm.git] / README.initramfs
CommitLineData
0ff1a185
NB
1Assembling md arrays at boot time.
2---------------------------------
3December 2005
4
5These notes apply to 2.6 kernels only and, in some cases,
6to 2.6.15 or later.
7
8Md arrays can be assembled at boot time using the 'autodetect' functionality
9which is triggered by storing components of an array in partitions of type
10'fd' - Linux Raid Autodetect.
11They can also be assembled by specifying the component devices in a
12kernel parameter such as
13 md=0,/dev/sda,/dev/sdb
14In this case, /dev/md0 will be assembled (because of the 0) from the listed
15devices.
16
17These mechanisms, while useful, do not provide complete functionality
18and are unlikely to be extended. The preferred way to assemble md
19arrays at boot time is using 'mdadm' or 'mdassemble' (which is a
20trimmed-down mdadm). To assemble an array which contains the root
21filesystem, mdadm needs to be run before that filesystem is mounted,
22and so needs to be run from an initial-ram-fs. It is how this can
23work that is the primary focus of this document.
24
25It should be noted up front that only the array containing the root
26filesystem should be assembled from the initramfs. Any other arrays
27should be assembled under the control of files on the main filesystem
28as this enhanced flexibility and maintainability.
29
30A minimal initramfs for assembling md arrays can be created using 3
31files and one directory. These are:
32
33/bin Directory
34/bin/mdadm statically linked mdadm binary
35/bin/busybox statically linked busybox binary
36/bin/sh hard link to /bin/busybox
37/init a shell script which call mdadm appropriately.
38
39An example init script is:
40
41==============================================
42#!/bin/sh
43
44echo 'Auto-assembling boot md array'
45mkdir /proc
46mount -t proc proc /proc
47if [ -n "$rootuuid" ]
48then arg=--uuid=$rootuuid
49elif [ -n "$mdminor" ]
50then arg=--super-minor=$mdminor
51else arg=--super-minor=0
52fi
53echo "Using $arg"
54mdadm -Acpartitions $arg --auto=part /dev/mda
55cd /
56mount /dev/mda1 /root || mount /dev/mda /root
57umount /proc
58cd /root
59exec chroot . /sbin/init < /dev/console > /dev/console 2>&1
60=============================================
61
62This could certainly be extended, or merged into a larger init script.
63Though tested and in production use, it is not presented here as
64"The Right Way" to do it, but as a useful example.
65Some key points are:
66
67 /proc needs to be mounted so that /proc/partitions can be accessed
68 by mdadm, and so that /proc/filesystems can be accessed by mount.
69
70 The uuid of the array can be passed in as a kernel parameter
71 (rootuuid). As the kernel doesn't use this value, it is made available
72 in the environment for /init
73
74 If no uuid is given, we default to md0, (--super-minor=0) which is a
75 commonly used to store the root filesystem. This may not work in
76 all situations.
77
78 We assemble the array as a partitionable array (/dev/mda) even if we
79 end up using the whole array. There is no cost in using the partitionable
80 interface, and in this context it is simpler.
81
82 We try mounting both /dev/mda1 and /dev/mda as they are the most like
83 part of the array to contain the root filesystem.
84
85 The --auto flag is given to mdadm so that it will create /dev/md*
86 files automatically. This is needed as /dev will not contain
87 and md files, and udev will not create them (as udev only created device
88 files after the device exists, and mdadm need the device file to create
89 the device). Note that the created md files may not exist in /dev
90 of the mounted root filesystem. This needs to be deal with separately
91 from mdadm - possibly using udev.
92
93 We do not need to create device files for the components which will
94 be assembled into /dev/mda. mdadm finds the major/minor numbers from
95 /proc/partitions and creates a temporary /dev file if one doesn't already
96 exist.
97
98The script "mkinitramfs" which is included with the mdadm distribution
99can be used to create a minimal initramfs. It creates a file called
100'init.cpio.gz' which can be specified as an 'initrd' to lilo or grub
101(or whatever boot loader is being used).
102
103
104
105
106Resume from an md array
107-----------------------
108
109If you want to make use of the suspend-to-disk/resume functionality in Linux,
110and want to have swap on an md array, you will need to assemble the array
111before resume is possible.
112However, because the array is active in the resumed image, you do not want
113anything written to any drives during the resume process, such as superblock
114updates or array resync.
115
116This can be achieved in 2.6.15-rc1 and later kernels using the
117'start_readonly' module parameter.
118Simply include the command
119 echo 1 > /sys/module/md_mod/parameters/start_ro
120before assembling the array with 'mdadm'.
121You can then echo
122 9:0
123or whatever is appropriate to /sys/power/resume to trigger the resume.