sagir3
Posts: 10
Joined: Tue Feb 17, 2015 3:05 pm

[SOLVED] Using an initramfs on the RPi.

Tue Feb 17, 2015 3:32 pm

Hi everyone, I recently received my very first Rpi device, a model B+. (I actually bought a Rpi 2, but it'll take a month or two to ship, so I bought a B+ off ebay, for $30 in total, to keep me busy in the meantime). So far, it's exceeded my expectations, the B+. I can only imagine how much better the Rpi 2 will be...

Anyways, I've been trying to use an initramfs on the Rpi for the past 2 days without success. This is because of conflicting instructions on forum posts and general Google search results. Some say you must compile a kernel with modules built-in for whatever features you desire. Others say you need to edit the cmdline.txt and/or config.txt. And so on, and so on...

What I'm trying to do: have a BTRFS root filesystem on the Raspberry Pi model B+.
What I'm looking for / need help with: short, to the point instructions on how to use an initramfs so that I can utilize the btrfs module which is enabled in the kernel. If possible, I would like *not* to re-compile the kernel (though I'm totally comfortable with doing so. It just wastes time and should not be be necessary, since btrfs module is enabled).

If you need additional info, please let me know.

Additional info:

$ cat /etc/issue
Distribution: Raspbian GNU/Linux 8 \n \l

$ uname -a
Kernel: 3.18.7+ PREEMPT (seems odd to me. "dpkg" lists the kernel package as linux-image-3.12-1-rpi )

(Kernel config relating to BTRFS)
CONFIG_BLK_DEV_INITRD=y
CONFIG_BTRFS_FS=m
CONFIG_BTRFS_FS_POSIX_ACL=y
Last edited by sagir3 on Fri Feb 20, 2015 2:59 am, edited 4 times in total.

sagir3
Posts: 10
Joined: Tue Feb 17, 2015 3:05 pm

Re: Looking for short & sweet instructions on using an initr

Wed Feb 18, 2015 6:26 pm

BUMP.

Calling all engineers, devs, and/or advanced users. Nobody knows how to use an initial ramdisk (aka "initramfs"/"initrd") with the Rpi?
(I am compiling a kernel with BTRFS built-in as I write this).

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: Looking for short & sweet instructions on using an initr

Thu Feb 19, 2015 3:04 am

sagir3 wrote:

Code: Select all

$ ls /boot/ | grep initrd
initrd.img-3.12-1-rpi
Does THIS mean you always have an INITRD? Have you tried to unpack it with 'cpio' (may be 'gunzip' first) and modify it to your needs?

I'm loading mine in 'config.txt' via

Code: Select all

~ $ tail -n2 /mnt/mmcblk0p1/config.txt
ramfsfile=initramfs.gz
ramfsaddr=0x00a00000
but still using an older kernel, hope nothing important has changed in between:

Code: Select all

~ $ uname -r
3.10.24+
This is also an original kernel and was "delivered" without INITRAMFS but with build-in-support for.
You can simply create your own with 'cpio', put all your needed stuff in a directory and fire up:

Code: Select all

find . | cpio -o -H newc | gzip -c9 > ../initramfs.gz
Greetings, FM_81
Last edited by FM81 on Thu Feb 19, 2015 4:40 pm, edited 1 time in total.
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

PascalK
Posts: 6
Joined: Wed Feb 18, 2015 7:30 am

Re: Looking for short & sweet instructions on using an initr

Thu Feb 19, 2015 8:16 am

Actually with recent versions of buildroot you do have to set just one switch to use a initramfs.
Would I do is grab a version of buildroot e.g. 2014.11 from the repo, use the provided config for the pi and set the one and only switch "BR2_TARGET_ROOTFS_INITRAMFS", this will trigger the other settings (like cpio the root fs..).

For me this is working perfectly. Then you just need to copy the firmware files out of output/images/rpi-firmware and the zImage to your sdcard and boot up.

Take into account working with a ramdisk has some pitfalls, you have to set your system up befor building. But with advantages I don't want to miss (unplug power anytime without remorse).

Best regards
Pascal

sagir3
Posts: 10
Joined: Tue Feb 17, 2015 3:05 pm

Re: Looking for short & sweet instructions on using an initr

Thu Feb 19, 2015 3:41 pm

@FM81, thank you for your informative post.

Sorry about the confusion in my title, which says "...using an initrd." I've changed it now.
It appears initrd has been replaced by a newer method using initramfs. Do you know how to convert an initramfs into an initrd, or can the Pi use an initramfs as well? I'll be Googling this myself.

Yesterday, I compiled a kernel with BTRFS support built-in, and ext4 as module, and so far, I've been running on a BTRFS root filesystem just fine. Other than an extra 10 seconds boot time and 25% lesser filesystem size due to lzo compression, performance seems to be the same. Minor issue: BTRFS subvolume on / does not work, but for /home, it does. No big deal: I use tar backups either way.

@PascalK, I just read the about page of builtroot. I have initramfs support enabled in my custom kernel already. Just need a way to use the output initramfs, which is in my boot folder, with the Rpi's config.txt file (unless, it's only possible to use an initrd and not initramfs?).

$uname -r
3.18.7-btrfs-zram

$ cd /boot
$ file initramfs-3.18.7-btrfs-zram
initramfs-3.18.7-btrfs-zram: gzip compressed data, last modified: Thu Feb 19 10:11:47 2015, from Unix

Image

PascalK
Posts: 6
Joined: Wed Feb 18, 2015 7:30 am

Re: Looking for short & sweet instructions on using an initr

Thu Feb 19, 2015 3:53 pm

if you created the image with buildroot you will most likly end up with a single zImage so in my config.txt the zImage ist provided as kernel it looks like this:

config.txt

Code: Select all

# Please note that this is only a sample, we recommend you to change it to fit
# your needs.
# You should override this file using a post-build script.
# See http://buildroot.org/downloads/manual/manual.html#rootfs-custom
# and http://elinux.org/RPiconfig for a description of config.txt syntax

arm_freq=700
core_freq=250
kernel=zImage
disable_overscan=1
gpu_mem_256=100
gpu_mem_512=100
sdram_freq=400
over_voltage=0
disable_splash=1
cmdline="dwc_otg.fiq_fix_enable=1 sdhci-bcm2708.sync_after_dma=0 dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootwait"

cmdline.txt

Code: Select all

dwc_otg.fiq_fix_enable=1 sdhci-bcm2708.sync_after_dma=0 dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootwait
Does this match your settings?

Best regards,
Pascal

sagir3
Posts: 10
Joined: Tue Feb 17, 2015 3:05 pm

Re: Looking for short & sweet instructions on using an initr

Thu Feb 19, 2015 4:04 pm

@PascalK

My config.txt:

Code: Select all

gpu_mem=192
arm_freq=1100
framebuffer_depth=32
core_freq=500
sdram_freq=400
over_voltage=6
disable_overscan=1
dtparam=spi=on
The kernel line is not necessary if you have a kernel.img file present in /boot. Other than that, I don't see anything relating to the use of initramfs in there. I believe your initramfs is built inside the kernel, instead of outside, like what I have. What's your kernel size? Mine's 3.8MB, and it's using the stock config that Raspbian ships, with just BTRFS as a built-in. My initramfs size is 5MB.

cmdline.txt

Code: Select all

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 elevator=deadline root=/dev/mmcblk0p2 rootfstype=btrfs rootwait
No, they don't match...but I don't see anything relating to the use of an initramfs in there =/

PascalK
Posts: 6
Joined: Wed Feb 18, 2015 7:30 am

Re: Looking for short & sweet instructions on using an initr

Thu Feb 19, 2015 4:42 pm

I am sorry, I didn't read that you have the root filesystem not build into the kernel itself, which I did.

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: Looking for short & sweet instructions on using an initr

Thu Feb 19, 2015 4:46 pm

sagir3 wrote:Do you know how to convert an initramfs into an initrd, or can the Pi use an initramfs as well?
Pi can use INITRAMFS, I've ment it in my last post (now corrected); this was a mismatch, sorry. My information cames from here only: http://elinux.org/RPiconfig

Greetings, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

sagir3
Posts: 10
Joined: Tue Feb 17, 2015 3:05 pm

Re: Looking for short & sweet instructions on using an initr

Thu Feb 19, 2015 6:25 pm

@FM81, your instructions are for the older kernels that still use initrd, not initramfs. Apparently, for newer kernels, you don't need to specify a load address! EPIC!

This seems to be working for me:
config.txt

Code: Select all

initramfs initramfs-3.18.7-btrfs-zram
Note, using "initramfs <name of initramfs>"

Journalctl output after using :
# journalctl -b | grep -i initr

Code: Select all

Feb 15 15:47:14 rpi-bplus kernel: Trying to unpack rootfs image as initramfs...
Feb 15 15:47:14 rpi-bplus kernel: Freeing initrd memory: 5020K (d3b09000 - d3ff0000)
Now, to make sure, I'll make a tar backup and restore it onto an ext4 partition. Since I made ext4 a module within the kernel, it should boot with the initramfs enabled, and NOT boot with it disabled. Stay tuned! Almost at the bottom of this mystery...

sagir3
Posts: 10
Joined: Tue Feb 17, 2015 3:05 pm

Re: Looking for short & sweet instructions on using an initr

Fri Feb 20, 2015 2:58 am

SUCCESS! Initramfs is working on the Raspberry Pi!
Screenshot attached: Image

Post solved! If anyone else needs help setting up an initramfs, just do what I did. I'm too lazy to make a guide, so if you want, me and the rest of the guys here who helped can offer you the info necessary to make a guide.

adontz
Posts: 14
Joined: Sun Jun 15, 2014 4:11 pm

Re: [SOLVED] Using an initramfs on the RPi.

Tue Mar 03, 2015 9:26 pm

While everybody is too lazy to write a complete guide it will be very nice of you to post/attach a working buildroot config.

sagir3
Posts: 10
Joined: Tue Feb 17, 2015 3:05 pm

Re: [SOLVED] Using an initramfs on the RPi.

Tue Mar 03, 2015 10:21 pm

I didn't use buildroot or anything fancy like that. Just compiled the latest RPi git kernel using the old kernel's config (make ARCH=arm blah blah oldconfig), then ran menuconfig and enabled whatever I needed (btrfs and zram).

What are you trying to do? Get btrfs working as well? Which kernel version are you on? I'm on my phone atm. Will reply in full @ home.

mrpetrov
Posts: 19
Joined: Sun Mar 08, 2015 3:44 pm

Re: [SOLVED] Using an initramfs on the RPi.

Sun Mar 08, 2015 3:49 pm

Well, trying the stuff mentioned in this thread did not help me make Raspberry Pi 2 load the BTRFS kernel module via initramfs, so no root BTRFS filesystem for me, yet...
Anyways, what is one supposed to put in the dir which gets compressed to initramfs.gz?
I just copied btrfs.ko but it did not help at all..

sagir3
Posts: 10
Joined: Tue Feb 17, 2015 3:05 pm

Re: [SOLVED] Using an initramfs on the RPi.

Sun Mar 08, 2015 4:38 pm

mrpetrov wrote:Well, trying the stuff mentioned in this thread did not help me make Raspberry Pi 2 load the BTRFS kernel module via initramfs, so no root BTRFS filesystem for me, yet...
Anyways, what is one supposed to put in the dir which gets compressed to initramfs.gz?
I just copied btrfs.ko but it did not help at all..
Before you begin following the following instructions, make sure that the BTRFS module is at least enabled in the stock RPi kernel. If not, you're SOL and will have to compile it youself from source.

$ cp /proc/config.gz ~/
$ gunzip config.gz
$ cat config | grep -i btrfs
CONFIG_BTRFS_FS=m
CONFIG_BTRFS_FS_POSIX_ACL=y

0. Make sure your RPi is booting off the default ext2/3/4 filesystem. Backup the /boot partition.
# cd /boot
# tar cpjvf /home/yourusername/rpiboot.tar.bz2 ./ --one-file-system

1. Make an initramfs in /boot
# mkinitramfs -o /boot/initramfs-$(uname -r)

2. Edit the /boot/config.txt and /boot/cmdline.txt to reflect changes. Change kernel info as necessary:
# nano /boot/config.txt
kernel=kernel-3.18.7.img
initramfs initramfs-3.18.7

# nano /boot/cmdline.txt
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 elevator=deadline root=/dev/mmcblk0p2 rootfstype=btrfs rootwait

# sync

2. Power off your Pi and take out your microsd card and put it in a card reader. You will back up the partition like so:
# mount /dev/sdX2 /mnt
(Note: the second partition on the SD card is usually the RPi's root filesystem, at least, on my RPi B+ it is).
# cd /mnt
# tar cpjvf /home/yourusername/rpiroot.tar.bz2 ./ --one-file-system
(Note: tar backup beats a dd backup since you don't have to do a whole block by block copy. Much faster and easier).

3. Format the partition as btrfs. You already know how. Then, mount it and restore the backup:
# mkfs.btrfs /dev/sdX2 -f -L rpi2_root
(Optional: if you want transparent lzo compression, you may enable it. It slightly increases the boot time by 5-6 seconds, but makes the filesystem ~20% smaller. A fair tradeoff to me. Installing packages takes slightly longer as well).
# mount /dev/sdX2 -o compress=lzo /mnt
# tar xpjvf /home/yourusername/rpiroot.tar.bz2 -C /mnt --numeric-owner
# sync

4. Edit your fstab to reflect filesystem changes. Follow my fstab as a guide.
(note: I have a separate /home partition, but you may not have one, or not care about one)
(note 2: BTRFS subvolumes are NOT working on the root filesystem for some reason. On /home and others, it works):
# nano /etc/fstab
/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / btrfs noatime,ssd 0 0
/dev/mmcblk0p3 /home btrfs noatime,ssd 0 0
tmpfs /tmp tmpfs noatime,size=16m 0 0
tmpfs /var/log tmpfs noatime,size=16m 0 0

# sync
# umount /dev/sdX2

5. Remove SD card, put it into the RPi, do the magic dance, and it should boot up without any trouble.
If it doesn't, make sure to take a video of the bootup process and/or photos of the error.

mrpetrov
Posts: 19
Joined: Sun Mar 08, 2015 3:44 pm

Re: [SOLVED] Using an initramfs on the RPi.

Sun Mar 08, 2015 7:15 pm

@sagir3: Thank you for the detailed instructions. I, however, in the time between posting my question here and your answer, found this article:

http://blog.drewwithers.com/2013/11/ras ... ition.html

I used it for the part where some kernel module is loaded during boot - btrfs.ko

The first time my RPi2 booted - the error messages pointed my mistake of trying to run a system off of BTRFS root without btrfs-tools installed. So I reverted the Pi to ext4 to install btrfs-tools:

Code: Select all

sudo apt-get install btrfs-tools
and then I basically did what you described above.

As for the btrfs subvolumes on the root filesystem - I use this (on other Linux based systems too):

Code: Select all

#!/bin/bash
# ver 2.0
# Now naming each archive with the hostname and date it was created.
# Just make sure it is run only once a day!

backupfilename=$(hostname)-$(date +%F).tar.bz2

if [ -e "/root/snapshots" ] && [ -d "/root/snapshots" ]; then
        printf "/root/snapshots folder found. Moving on.\n"
else
        printf "/root/snapshots folder does not exist - creating it..."
        mkdir /root/snapshots
        if [ "$?" -eq "0" ]; then
            printf "done.\n"
        else
            printf "error!\nFailed to create folder /root/snapshots. Aborting snapshotting.\n"
            exit 1
        fi
fi

printf "btrfs: "
btrfs subvolume snapshot -r / /root/snapshots/snap001

if [ "$?" -eq "0" ]; then
        printf "Snapshot created.\n"
else
        printf "Failed to create snapshot. Aborting operation.\n"
        exit 1
fi

cd /root/snapshots/snap001

printf "Beginning to tar.bz2 contents of snapshoted /. Please, be patient.\nWorking..."
tar --one-file-system -cjf /root/$backupfilename .

if ! [ "$?" -eq "0" ]; then
    printf "Failure during compression with tar -cj. Continuing to clean up.\n"
fi
cd /root
printf "compressing done.\n"

printf "btrfs: "
btrfs subvolume delete /root/snapshots/snap001

if [ "$?" -eq "0" ]; then
        printf "Snapshot removed.\n"
else
        printf "Failed to remove the snapshot of / btrfs filesystem.\n"
fi

printf "Creating MD5 checksum of archive (binary mode)..."
md5sum -b $backupfilename > $backupfilename.md5
printf "done!\n"

backupfilename=
I have the above code in an executable file named "backup-root.sh" in root's home dir, so on the Pi I did:

Code: Select all

sudo -i
and then, as root in root's home dir, I ran

Code: Select all

nano backup-root.sh
and used copy-paste.

I just started the script's first run on the Pi using

Code: Select all

sudo -i
time ./backup-root.sh
and it is still running. I will update this post after it finishes with the total run time.

My Pi 2 uses the Raspbian from http://www.raspberrypi.org/downloads/ and is basically stock, so the root part weighs in at almost 3GB, but at the switch to BTRFS root in /etc/fstab I used the compress mount option.

Just checked while the backing up is still running:

Code: Select all

pi@raspberrypi ~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          7.9G  1.7G  6.1G  22% /
udev             10M     0   10M   0% /dev
tmpfs            93M  264K   93M   1% /run
/dev/mmcblk0p2  7.9G  1.7G  6.1G  22% /
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           186M     0  186M   0% /run/shm
/dev/mmcblk0p1   56M   19M   38M  33% /boot
I must note that the BTRFS file system was mounted with the compress option and then the content of the original ext4 transferred to it.
So, I expect the final archive to be under 2GB, but the time needed for its creating is still unknown. Update pending.

UPDATE:
Here is the output of my run of the above script to my Pi 2 connected via WiFi:

Code: Select all

root@raspberrypi:~# time ./backup-root.sh
/root/snapshots folder does not exist - creating it...done.
btrfs: Create a readonly snapshot of '/' in '/root/snapshots/snap001'
Snapshot created.
Beginning to tar.bz2 contents of snapshoted /. Please, be patient.
Working...tar: ./tmp/wpa_ctrl_1874-1: socket ignored
compressing done.
btrfs: Delete subvolume '/root/snapshots/snap001'
Snapshot removed.
Creating MD5 checksum of archive (binary mode)...done!

real    54m20.870s
user    50m46.040s
sys     1m7.010s
root@raspberrypi:~# ls -l
total 956040
-rwxr-xr-x 1 root root      1512 Mar  8 20:46 backup-root.sh
-rw-r--r-- 1 root root 978973279 Mar  8 21:46 raspberrypi-2015-03-08.tar.bz2
-rw-r--r-- 1 root root        65 Mar  8 21:46 raspberrypi-2015-03-08.tar.bz2.md5
drwxr-xr-x 1 root root         0 Mar  8 21:46 snapshots
root@raspberrypi:~#
So, stock Raspbian on Pi 2 takes 1 hour to backup and the .tar.bz2 archive is 1GB. The system is usable diring the backup.

Hope this helps someone!

sagir3
Posts: 10
Joined: Tue Feb 17, 2015 3:05 pm

Re: [SOLVED] Using an initramfs on the RPi.

Mon Mar 09, 2015 12:24 am

Are you sure you can snapshot a running system? It may cause trouble if some files/folders are being modified during the procedure. AFAIK, most guides tell you to snapshot when the root filesystem is unmounted and not in use.

In the case of the /home folder, snapshotting should be fine, as long as you kill X and are in a console session with no programs running.

mrpetrov
Posts: 19
Joined: Sun Mar 08, 2015 3:44 pm

Re: [SOLVED] Using an initramfs on the RPi.

Mon Mar 09, 2015 5:55 am

The script I posted above makes a snapshot of the entire / (root) partition, and then makes an archive from the snapshot. This means, that you are actually getting a "picture of the FS from a precise moment in time".
And that is one of the features I require BTRFS for the systems I use - it is really stable these days, and makes backing up a system a walk in the park, all the while the system is up and running - no extra stuff required.
But don't take my word for it - try the script I posted in a test environment. It requires only a system running on BTRFS root.

Obviously, if you are trying to backup a 3GB root, you will need at least 3GB more sans compression so your / (root) partition will have to be at least 6GB, but I think you will sort this one out.

Go read-up about BTRFS at the wiki: https://btrfs.wiki.kernel.org/index.php/UseCases

sagir3
Posts: 10
Joined: Tue Feb 17, 2015 3:05 pm

Re: [SOLVED] Using an initramfs on the RPi.

Mon Mar 09, 2015 3:47 pm

Huh, wow, now I'm even more impressed with BTRFS. Online snapshotting! How cool is that?
So, do you plan on making a comprehensive guide on how to get BTRFS on the RPi?

I'm not really an expert on BTRFS, but you seem to be.

mrpetrov
Posts: 19
Joined: Sun Mar 08, 2015 3:44 pm

Re: [SOLVED] Using an initramfs on the RPi.

Sun Mar 15, 2015 11:08 am

Tried to name it so it comes up in searches:
http://www.raspberrypi.org/forums/viewt ... 9&t=104108

Return to “Advanced users”