Jump to content











Photo

Linux - Load your root partition to RAM and boot it


  • Please log in to reply
15 replies to this topic

#1 siro

siro
  • Members
  • 7 posts
  •  
    Germany

Posted 19 May 2011 - 10:48 AM

This tutorial will guide you through the steps to modify your initramfs to load all files from / to a tmpfs. This will only work with Debian 5 or newer and Ubuntu 9 ? or any unix thats supports booting from a initramfs. Since this is a virtual filesystem in RAM, not a virtual harddisk, this has many advantages.

What you need:
* lots of RAM
* Debian based distribution or any that supports booting from initramfs
* mkinitramfs or a tool to build a new initramfs
* some linux knowledge
* no need to create an image
* no need for Grub4Dos
* no need for a "special driver"

Step 1:
Choose a distribution thats supports booting from initramfs. (like ubuntu)

Step 2:
Install to harddisk. Make sure you split it into multiple partitions (/, /boot, /home, swap, ...).

Step 3:
Boot your new system, install updates, drivers if neccessary (this will improve performance), strip it down to the minimum. Every file will be loaded to RAM ! A fresh install uses about 2 GB auf harddisk-space.

Step 4:
modify /etc/fstab :
* make a backup
cp /etc/fstab /etc/fstab.bak
* find the line specifing the root partition and change it in:
none / tmpfs defaults 0 0
* save

Step 5:
edit the local script in your initramfs:
cd /usr/share/initramfs-tools/scripts/
* make a backup of /usr/share/initramfs-tools/scripts/local
cp local local.bak
* modify local, find this line:
# FIXME This has no error checking

# Mount root

mount ${roflag} -t ${FSTYPE} ${ROOTFLAGS} ${ROOT} ${rootmnt}
* change it to:
# FIXME This has no error checking

# Mount root

#mount ${roflag} -t ${FSTYPE} ${ROOTFLAGS} ${ROOT} ${rootmnt}

mkdir /ramboottmp

mount ${roflag} -t ${FSTYPE} ${ROOTFLAGS} ${ROOT} /ramboottmp

mount -t tmpfs -o size=100% none ${rootmnt}

cd ${rootmnt}

cp -rfa /ramboottmp/* ${rootmnt}

umount /ramboottmp

* save
* execute, or rebuild initramfs
mkinitramfs -o /boot/initrd.img-ramboot
* replace modified local with original file
cp -f local.bak local
Step 6:
* modify this file (needs a better solution)
/boot/grub/grub.cfg
* copy the first boot entry and replace the /initrd line with this:
/initrd /initrd.img-ramdisk
* label the new entrie as RAMBOOT
This will boot our generated initramfs instead the original one.
Step 7:
* reboot
* choose standart boot (no ramdisk)
* choose RAMBOOT and all your files on the root partition will be loaded to a tmpfs

Results:
Test setup:
*Kernel : Linux 2.6.38-8-generic (i686)
*Compiled : #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011
*Distribution : Ubuntu 11.04
*no graphics driver
*Desktop HP 8100E
*Core i7 860
*4GB Memory 2 Modules (DDR3)
*WDVeliciraptor 10k rpm 160GB

Tiotest results for 4 concurrent io threads:

,----------------------------------------------------------------------.

| Item                  | Time     | Rate         | Usr CPU  | Sys CPU |

+-----------------------+----------+--------------+----------+---------+

| Write         800 MBs | 0.1489 s | 5372.806 MB/s |  2.80 %  | 32.40 % |

| Random Write   16 MBs | 0.0025 s | 6320.793 MB/s |   0.0 %  |   0.0 % |

| Read          800 MBs | 0.0928 s | 8616.790 MB/s |  11.7 %  |  11.7 % |

| Random Read    16 MBs | 0.0021 s | 7454.676 MB/s |   0.0 %  |   0.0 % |

`----------------------------------------------------------------------'

Tiotest latency results:

,-------------------------------------------------------------------------.

| Item         | Average latency | Maximum latency | % >2 sec | % >10 sec |

+--------------+-----------------+-----------------+----------+-----------+

| Write        |        0.003 ms |        0.048 ms |  0.00000 |   0.00000 |

| Random Write |        0.002 ms |        0.011 ms |  0.00000 |   0.00000 |

| Read         |        0.002 ms |        0.025 ms |  0.00000 |   0.00000 |

| Random Read  |        0.002 ms |        0.023 ms |  0.00000 |   0.00000 |

|--------------+-----------------+-----------------+----------+-----------|

| Total        |        0.002 ms |        0.048 ms |  0.00000 |   0.00000 |

`--------------+-----------------+-----------------+----------+-----------'

I hope this helps a bit :)
siro
  • subzero likes this

#2 Sha0

Sha0

    WinVBlock Dev

  • Developer
  • 1679 posts
  • Location:reboot.pro Forums
  • Interests:Booting
  •  
    Canada

Posted 19 May 2011 - 12:50 PM

* choose RAMBOOT and all your files on the root partition will be loaded to a tmpfs

The modern Linux initramfs-style initrds will be extracted to rootfs (a special instance of a ramfs, just as tmpfs is a special instance of ramfs), if I'm not mistaken (see linux/Documentation/filesystems/ramfs-rootfs-initramfs.txt).

What is the advantage of using tmpfs rather than rootfs? Do you have size limitations on the tmpfs?

#3 siro

siro
  • Members
  • 7 posts
  •  
    Germany

Posted 22 May 2011 - 11:34 AM

You are right, the rootfs is a ramfs. There are two factors to keep in mind:
*the initrd is a ramfs:
** Ramfs will grow dynamically,
it will grow until theres no more free RAM, it may crash your system
** tmpfs will not grow dynamically,
it will grow until it reaches the maximum size you specified or theres no more free space in RAM or on your swap device. If both are full you will receive the error: “No space left on device".

* I don't use the rootfs to keep things simple
changes in /scripts/local are simple, you can still use any kind of disk-encryption, softraid, ...

#4 Goldfanger

Goldfanger
  • Members
  • 2 posts
  •  
    Costa Rica

Posted 19 June 2011 - 08:57 PM

Correct me if I'm wrong, but it doesn't look like you ever sync back from RAM to the hard drive, to make changes & settings persistent? I recall some instructions for mounting Firefox in RAM, then using rsync to automatically copy any parts of your status of tabs, history, etc that have changed (and ONLY the parts that have changed...) from tmpfs back to disk each 5 minutes or whatever you choose (minimal performance impact & less frequent writes to disks).

Looks like nice results; what GHz is your RAM?

Edited by Goldfanger, 19 June 2011 - 09:33 PM.


#5 Goldfanger

Goldfanger
  • Members
  • 2 posts
  •  
    Costa Rica

Posted 19 June 2011 - 10:23 PM

I'm using a Live-CD now to format my drives, so I'd better post this or else a crash will lose these links I've collected (and lose the time to create my disk formatting), so I'm not taking a long time to look, but here is:

Possibly some of this code is compatible w/ your step #6 which you'd like to improve (not sure if he got his overall package to work, but it's recent & ubuntu): http://ubuntuforums....d.php?t=1267853

“the script” & “automation” to not lose your Firefox history (copy from tmpfs to drive each few minutes...the above ubuntu guy also tries to do that but these guys seem more certain that they got it right): https://wiki.archlin...he_cache_to_RAM

Puppy seems a bit different but have a refined widely-used system that runs from RAM: http://puppylinux.or...a/howPuppyWorks

a similar distro to deb/ubuntu's boot process is able to do all this: “At the next boot, Knoppix will scan for any image file named knoppix.img and then ask if you want to load it. If you load it, any changes you make will be saved into the image file automatically. Make multiple persistent images! What happens if you make lots of configuration changes or download and install packages and then Knoppix breaks? Doesn't a persistent image bring the same problems as a hard disk install? Here, a good strategy is to make a copy of your previous, stable persistent image to another file e.g. backup.img. Knoppix will only load knoppix.img. Go ahead, do your experiments, make your changes, install packages etc with confidence. If Knoppix breaks, just rename backup.img to knoppix.img and you have very quickly rolled back to your last, stable and good configuration” ((( their unionfs = deb/ubuntu's initramfs ))): http://www.knoppix.n...tent_Disk_Image

.

...now for fast boots, imagine loading from dual sata-3's striped into RAID-0, OCZ's PCIe RevoDrives, or even a usb3.0 flash-stick!

less promising how-to's:
a gentoo guy did it but in 2005 w/limited RAM (so he only did /usr, maybe /lib ...loks overly-complicated): http://forums.gentoo...c-t-296892.html

description of logical steps for a similar distro (uses an initramfs) but he got lost midway thru, dated 2010: “You can grab the INITRD image and mount it in order to modify it. The INITRD file is an image of a small Linux OS that always run in RAM in first place before switching the root directory to the HDD or the corresponding device where the Linux system was installed.

What I want to do is to create a TMPFS mount (RAM) while initrd is running and use dd to copy the image into that directory and then switchroot to the RAM. So far I have achieved to run INITRD and stay there only as a shell (some kind of embedded system). Obviously if I exit the shell, the system crash and gives me backtraces since it did not load the real root.

I do not know if someone have reached this step and I would like to discuss this. The goal is
1. Run a personalized initrd
2. mount a temp filesystem in RAM
3. use DD or other method to copy the image into ram
4. have the image available for switchroot
5. continue the boot process, which in theory it will be all in ram

I'm using a LIVECD image to try this since it´s INITRD image is designed to load the necessary devices in an environment of mobility... this INITRD file is the key. Still trying and proving.”

http://www.denx.de/w...inuxRamdiskRoot seems to follow mostly the same steps.

Edited by Goldfanger, 19 June 2011 - 10:30 PM.


#6 siro

siro
  • Members
  • 7 posts
  •  
    Germany

Posted 20 June 2011 - 10:01 PM

Hi,
I don't sync back, thats true. You can do it using shutdown scripts or a tool like rsync.
Ram speed is DDR3-1333 Dual channel

If you create a tmpfs you can't copy files with dd, since it's not a virtual harddisk, it's a virtual filesystem. You have to use cp, rsync, ...

  • This can be done by modifing the entries in your bootloader (grub/lilo), you can choose which initrd to load
  • I've done that, see above
    mount -t tmpfs -o size=100% none ${rootmnt}
    
    
  • I've done that, see above
    cd ${rootmnt}
    
    cp -rfa /ramboottmp/* ${rootmnt}
  • no need for switchroot, since all files are already copied to root
  • init will continue as normal after it copied all the files to tmpfs

I created my "personal RAM boot initrd" using initramfs-tools. All the files you need to modify are in /usr/share/initramfs-tools/. Then run initramfs-tools -o /boot/<yournewinitrd.img>

#7 subzero

subzero
  • Members
  • 4 posts
  •  
    Algeria

Posted 21 March 2014 - 11:34 AM

Hi, 

 

First, I want thank for your very useful tutorial. 

 

I'm using PXE with DEBIAN 7.4. My pxelinux.cfg/defaults contains :

IPAPPEND 2
SERIAL 0 115200
PROMPT 1
TIMEOUTE 10
DEFAULT linux
label linux
kernel vmlinux-diskless
append root=/dev/nfs nfsroot=10.0.0.1/nfsroot/diskless initrd=/initrd rw console=tty0 netdev=irq=10,name=eth1 ip=::::diskless:eth0:dhcp panic=100 

I want apply this concept, to avoid using the NFS after booting,

So I edited initrd/scripts/nfs . I replaced :

   nfsmount -o nolock ${roflag} ${NFSOPTS} ${NFSROOT} ${rootmnt} 

by

mkdir /ramboottmp

nfsmount -o nolock ${roflag} ${NFSOPTS} ${NFSROOT} /ramboottmp
mount -t tmpfs -o size=100% none ${rootmnt}

cd ${rootmnt}

cp -rfa /ramboottmp/bin ${rootmnt}
cp -rfa /ramboottmp/lib ${rootmnt}
cp -rfa /ramboottmp/lib64 ${rootmnt}
cp -rfa /ramboottmp/etc ${rootmnt}
cp -rfa /ramboottmp/tmp ${rootmnt}

umount /ramboottmp 

the system boot correctly, but my rootfs did not changed, it always in NFS instead to become RAM.

I tried edit initrd/scripts/local, but I had the same results.



#8 siro

siro
  • Members
  • 7 posts
  •  
    Germany

Posted 21 March 2014 - 02:40 PM

To boot from nfs and create a tmpfs root partition you need to edit

 

/usr/share/initramfs-tools/scripts/nfs

 

there's a function called do_nfsmount()

 
replace this line
 
nfsmount -o nolock ${roflag} ${NFSOPTS} ${NFSROOT} ${rootmnt}
 
with
 
mkdir /ramboottmp

nfsmount -o nolock ${roflag} ${NFSOPTS} ${NFSROOT} /ramboottmp

mount -t tmpfs -o size=100% none ${rootmnt}

cd ${rootmnt}

cp -rfa /ramboottmp/* ${rootmnt}

umount /ramboottmp

Attention: I haven't tested this code !



#9 subzero

subzero
  • Members
  • 4 posts
  •  
    Algeria

Posted 21 March 2014 - 03:16 PM

I tried using this code, but It did not work. 



#10 SiebenDX

SiebenDX
  • Members
  • 1 posts
  •  
    Germany

Posted 30 October 2014 - 09:32 AM

I tried using this code, but It did not work. 

There is a bug in the line "/initrd /initrd.img-ramdisk" where you have to change your grub.cfg.

 

It has to be "/initrd /initrd.img-ramboot" -> ramboot insted of ramdisk

 

I use this method for a long time now, it works perfect!



#11 Divetoxx

Divetoxx

    Newbie

  • Members
  • 18 posts
  • Interests:Assembler, fractals, buddhism
  •  
    Belarus

Posted 19 February 2015 - 09:50 AM

It's very good manual, I tried it and got success.

But I have found very horrible, terrible thing!

 

I have 16 Gb of RAM. My root partition is about 2 Gb, so it uses about 2 Gb of RAM and 14 Gb are free.

Next I simple do this:

mount /dev/sda1 /mnt

(/dev/sda1 is my first HDD partition)

Next I simple play the movie:

mplayer /mnt/videos/Oblivion.mkv

Before this I started the command "top" in my terminal emulator. While playing the movie I can see the amount of used memory is growing and growing and when the movie is ending I see 7 Gb are used! Only 2 Gb were used before playing. The size of Oblivion.mkv is 5 Gb. 2+5=7.

So I figured out that the /dev/sda1 is now a part of my root file system and if I read any file from it this file is "putting" on tmpfs!

If I play 2-3 movies of 5 Gb each size then tmpfs goes to the end of RAM!

When I do umount /mnt I see only 2 Gb used memory again. I don't know any other methods to free this memory.

Couldn't anybody help, please?

May be If using the block device /dev/ram0 instead of tmpfs to load the root then this terrible thing won't occur? But it won't be so simple...


Edited by Divetoxx, 19 February 2015 - 10:00 AM.


#12 Divetoxx

Divetoxx

    Newbie

  • Members
  • 18 posts
  • Interests:Assembler, fractals, buddhism
  •  
    Belarus

Posted 21 February 2015 - 02:16 PM

Sorry, it was simply file caching :)

Commnd echo 1 > /proc/sys/vm/drop_caches empties it.
 



#13 jabol240

jabol240
  • Members
  • 2 posts
  •  
    United Kingdom

Posted 26 February 2015 - 01:25 AM

Hi guys, very nice tutorial and thanks for it!!!

Some questions of mine, if you could answer...

 

#1 does this custom ramdisk need re-doing or refreshing after any kernel update or something?

#2 will it work fine with dm-crypt encrypted disk where the / sits?

#3 does it ensure that no files AT ALL are EVER written to / without my explicit action?

 

Thanks again!



#14 siro

siro
  • Members
  • 7 posts
  •  
    Germany

Posted 26 February 2015 - 08:08 AM

to #1

In general the initramfs doesn't need to be recreated. It is for example if you install dm-crypt / cryptsetup.

Every time you create a new ramdisk you'd need to apply this changes. You could edit the specific files on disk and make sure they are invoked to create a new initramfs. This folder and tools to generate the initramfs vary from distribution to distribution.

 

to #2

(I haven't this tested yet.)

It will work with dm-crypt and other tools, as the ramdisk mounting takes place after the disk has been decrypted.

 

to #3

The ramdisk is mounted read + write at /. Files are always written to it, created, deleted.... Files on disk aren't modified. It doesn't write to your root partition (/dev/sda2), except you mount it as external drive. A partition (like the boot partition) mounted to /boot isn't covered by the ramdisk. Changes to those folders will change files on disk.

 

To safe changes back to the root partition you could do:
#1 create a shutdown script, that mount the root partition and copies files from ramdisk to the root partition

#2 mount the root partition and copy file by hand

#3 boot a initramfs without ramdisk support


Edited by siro, 26 February 2015 - 08:09 AM.


#15 jabol240

jabol240
  • Members
  • 2 posts
  •  
    United Kingdom

Posted 26 February 2015 - 03:49 PM

Yes exactly, whenever I need to for example update my system, I'll just choose the "normal" (without ramdisk)option in GRUB and that's it!

 

Thanks SO MUCH again!


Edited by jabol240, 26 February 2015 - 03:49 PM.


#16 HimuraBattousai

HimuraBattousai
  • Members
  • 1 posts
  •  
    Australia

Posted 28 February 2016 - 10:36 AM

Thanks for the guide, very useful to me.

 

I would like to make a contribution, regarding editing /boot/grub/grub.cfg and the note that this needs a better solution; I have one.

cd /boot/
ln -s vmlinuz-<version number> vmlinuz-ramboot
update-grub

In my testing, the ramboot kernel/initrd winds up as the default, with the 'regular' one being the next option.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users