原帖: https://forums.gentoo.org/viewtopic-t-784340-start-0.html
useful links: http://en.gentoo-wiki.com/wiki/Initramfs
Here’s my way to boot from a squashed root filesystem wich is readonly. The squashed image is in this example located on a harddisk partition and not on a cdrom drive. With the help of this forum and http://en.gentoo-wiki.com/wiki/Initramfs I managed it to work (copy-paste…).
My partitions in the following example:
/dev/sda1 /boot
/dev/sda2 / (this is the root wich will be squashed; later unused)
/dev/sda3 /mnt/imgpart (this is the partition where the squash image resides)
0. Forget genkernel -> it’s too complicated
1a. You have a running Gentoo system
1b. You need a compiled kernel in /usr/linux
my current conditions:
* Found kernel source directory:
* /usr/src/linux
* Found kernel object directory:
* /lib/modules/2.6.29-gentoo-r6/build
* Found sources for kernel version:
* 2.6.29-gentoo-r6
2. Emerge some stuff
代码:
emerge -pv git aufs2 busybox [ebuild R ] sys-apps/busybox-1.14.2 USE="pam static -debug -make-symlinks -savedconfig (-selinux)" 0 kB [ebuild U ] dev-util/git-1.6.4 [1.6.3.3] USE="bash-completion curl -gtk iconv perl xinetd -cgi -cvs -doc -emacs -mozsha1 (-ppcsha1) -subversion -threads -tk -webdav" 2,357 kB [ebuild U ] sys-fs/aufs2-0_p20090727 [0_p20090601-r1] USE="kernel-patch -debug -inotify -ramfs" 0 kB
note: aufs2-0_p20090727 doesn’t work with gentoo-sources-2.6.29, but aufs2-0_p20090601-r1 works out of the box
3. Create the initramfs directory
Do the steps according to http://en.gentoo-wiki.com/wiki/Initramfs and hirakendu examples
代码:
mount /boot
mkdir /usr/src/initramfs
cd /usr/src/initramfs
mkdir -p bin lib dev etc proc sbin sys mnt/union mnt/static mnt/dynamic mnt/imgpart
mkdir -p lib/modules/2.6.29-gentoo-r6/misc # used for "aufs" kernel module
cp -a /lib/modules/2.6.29-gentoo-r6/misc/aufs.ko lib/modules/2.6.29-gentoo-r6/misc/
cp -a /bin/busybox bin/
ln -s busybox bin/sh
touch etc/mdev.conf
cp -a /lib/{ld-*,libc-*,libc.so*,libdl*} lib/
cp -a /lib/{libm-*,libm.so*,libpam.so*,libpam_misc*} lib/
4. Create a new init shell script
This one is a bit raw, but you can use it as a good starting point.
nano -w init
代码:
#!/bin/busybox sh
# Mount the /proc and /sys filesystems.
mount -t proc none /proc
mount -t sysfs none /sys
busybox --install -s
mknod /dev/null c 1 3
mknod /dev/tty c 5 0
# Do your stuff here.
echo "This script mounts rootfs RO with an aufs RW layer."
modprobe aufs
sleep 7
mdev -s
# Parse the kernel command line from grub
CMDLINE="$(cat /proc/cmdline)"
parse_disk() {
if [ "$(echo $1|cut -c -5)" = "UUID=" ]; then
# $1 is a UUID
echo $(findfs $1)
elif [ "$(echo $1|cut -c -6)" = "LABEL=" ]; then
# $1 is a LABEL
echo $(findfs $1)
elif [ "$(echo $1|cut -c -5)" = "/dev/" ]; then
# $1 is a device name
echo $1
else
# $1 is unrecognized.
echo "unknow-disk"
fi
}
for p in ${CMDLINE};
do
key=${p%%=*}
value=${p#*=}
case $key in
imgpart)
IMGPART=`parse_disk $value`
;;
imgfile)
IMGFILE=$value
;;
esac
done
if [ -z "${IMGPART}" ]; then
echo "Specify the squash image partition after the kernel command ${CMDLINE}"
echo "example: kenrel... imgpart=/dev/sda2 imgfile=/gentoo.sqs"
exec /bin/sh
exit 0
fi
if [ -z "${IMGFILE}" ]; then
echo "Specify the squash image file after the kernel command ${CMDLINE}"
echo "example: kenrel... imgpart=/dev/sda2 imgfile=/gentoo.sqs"
exec /bin/sh
exit 0
fi
echo IMGPART=${IMGPART}
echo IMGFILE=${IMGFILE}
if [ ! -b "${IMGPART}" ]; then
echo No partition with ${IMGPART} has been found
exec /bin/sh
exit 0
fi
# ok, parsing done
# Mount the partitions
# 1) mount the partition where the squash image resides
mount -o ro ${IMGPART} /mnt/imgpart
# 2) init a loop pointing to the image file
loop_free=$(losetup -f | sed s#p/#p#)
losetup $loop_free /mnt/imgpart/${IMGFILE}
# 3) mount the squashfs to /mnt/static
mount -t squashfs $loop_free /mnt/static
# Note: if you don't want to use a squashed image, you just
# can mount your read only root to /mnt/static
# example: mount -o ro /dev/sda2 /mnt/static
# 4) mount a memory filesystem for the write access to the static image
# unclear: memory size? -o size=1024M
mount -t tmpfs tmpfs /mnt/dynamic
# 5) mount the writable overlay to the static image
mount -t aufs -o br=/mnt/dynamic:/mnt/static=ro none /mnt/union
# Clean up.
mount --move /mnt/dynamic /mnt/union/mnt/dynamic
mount --move /mnt/static /mnt/union/mnt/static
mount --move /mnt/imgpart /mnt/union/mnt/imgpart
umount /proc
umount /sys
# Boot the real thing.
exec switch_root /mnt/union /sbin/init
echo "Failed to switch_root, dropping to a shell"
exec /bin/sh
5. Create your own initramfs
Attention: it’s created in /boot!
代码:
mount /boot chmod a+x /usr/src/initramfs/init find . -print0 | cpio -ov -0 --format=newc | gzip -9 > /boot/my-initramfs.cpio.gz
6. Edit your grub.conf
nano -w /boot/grub/grub.conf
代码: title=My initramfs with 2.6.29 root (hd0,0) kernel /kernel-2.6.29-generic-a imgpart=/dev/sda3 imgfile=/gentoo-lxde.squash initrd /my-initramfs.cpio.gz
7. Create the squash image of the root
(Note: remove the root / entry from /etc/fstab)
Do this NOT from a running system but take a Live Distro and mount the root device:
代码: mount /dev/sda2 /mnt/gentoo mount /dev/sda3 /mnt/imgpart cd /mnt/gentoo mksquashfs . /mnt/imgpart/gentoo-lxde.squash
Note
There is a problem on unmounting the disks on system halt:
- Unmounting loopback devices failed: in use but fuser finds nothing
- the same for /mnt/dynamic /mnt/static /mnt/imgpart
-> maybe somebody has a solution for this…
Good luck!
claudio