Welcome! Log In Create A New Profile

Advanced

Howto: Create a rootfs for Debian Wheezy on GoFlexHome

Posted by majic79 
Howto: Create a rootfs for Debian Wheezy on GoFlexHome
August 10, 2013 05:36PM
Well, this follows up my earlier threads about rolling your own u-boot build to support the GoFlex Home (but it works equally well on a range of other devices, YMMV) once you've got them built, you'll probably want to ditch the stock OS in favour of something else, and Debian's pretty robust and has a nice new recent release "Wheezy"

This is based around Vlads Howto for squeeze, and with only slight variation you can also build an emdebian platform if that's what floats your boat.

I also go through some steps to deploy the file system to a USB stick and then transfer from USB stick to your device (hard disk deployment - it's slightly too large to fit in UBIFS on a GoFlexHome, my next task is to deploy emdebian to UBIFS)

Right, as before in my Howto roll your own u-boot and Howto roll your own linux kernel howtos, I'm assuming that you're using Debian for the build. I'm going to do this slightly differently to other guides - my work scenario prevented me from downloading the packages directly, so I built the image from dvd .iso image, I then added individual packages as required.

So as before, create a file to store your rootfs

dd if=/dev/zero of=rootfs.img bs=1M count=1024

now the rest needs to be done as root (although you may also be able to preceed these commands with sudo) - the remainder of this instruction assumes you're logged in as root

su
mkfs.ext2 -F rootfs.img
mount rootfs.img /mnt -o loop

If you've not already got them then now's a good time to download an install a few utilities

apt-get install debootstrap qemu-user-static binfmt-support

Now, put your CD image into the drive (this is where it's useful using a VirtualBox VM for development - connect .ISO to drive) if you want to mount the .ISO without burning a CD, then you can mount it like this:
mount debian-7.1.0-armel-DVD-1.iso /media/cdrom0 -o loop

Then begin the bootstrap - you'll need to choose how you're going about it:

For Debian CD-ROM install:
debootstrap --verbose --arch armel --variant=minbase --foreign --no-check-gpg wheezy /mnt file:///media/cdrom

For Debian regular download:
debootstrap --verbose --arch armel --variant=minbase --foreign wheezy /mnt http://ftp.uk.debian.org/debian
For Emdebian download: there are no .iso images on the official repositories at the moment, so you'll have to download, you'll also need the emdebian-archive-keyring
apt-get install emdebian-archive-keyring
debootstrap --verbose --keyring /usr/share/keyrings/emdebian-archive-keyring.gpg --arch armel --variant=minbase --foreign wheezy-grip /mnt http://ftp.uk.debian.org/emdebian/grip /usr/share/debootstrap/scripts/wheezy

Now we're almost ready to chroot into our file system, but a bit of qemu preparation first:

cp /usr/bin/qemu-arm-static /mnt/usr/bin
mount -t sysfs none /mnt/sys
mount -t proc proc /mnt/proc
mount -B /dev /mnt/dev
mount -B /dev/pts /mnt/dev/pts
chroot /mnt

As in Vlads instructions, you should see "I have no name!@hostname:/#" - so lets finish it off

/debootstrap/debootstrap --second-stage

which should end with "I: Base system installed successfully." in which case exit and re-enter the chroot, and set the password:

exit
mkdir /mnt/media/cdrom
mount -B /media/cdrom /mnt/media/cdrom
chroot /mnt
passwd

If planning to download your packages, then omit the cdrom mount instruction

Enter your chosen root password.

Now, we're ready to build up the base system - for the GoFlexHome, the limit is 256Mb, but as you want some space, I'd be looking at limiting it to 200Mb. First, lets tell apt where to get stuff, configure locales and prevent the system from installing too much dross that's not required

For Debian CD-Rom install:
apt-cdrom -m add /media/cdrom
For Regular Debian downloaded:
cat <<END > /etc/apt/sources.list
deb http://security.debian.org/ wheezy/updates main
deb http://ftp.debian.org/debian/  wheezy main contrib non-free
END
For Emdebian
cat <<END > /etc/apt/sources.list
deb http://security.debian.org/ wheezy-grip/updates main
deb http://ftp.uk.debian.org/emdebian/grip wheezy-grip main
deb http://security.debian.org/ wheezy/updates main
deb http://ftp.debian.org/debian/  wheezy main
END
apt-get install emdebian-archive-keyring

Finally, we're ready to install stuff
cat <<END > /etc/apt/apt.conf
APT::Install-Recommends "0";
APT::Install-Suggests "0";
END
apt-get update
export LANG=C
apt-get install apt-utils
apt-get install dialog
apt-get install locales
dpkg-reconfigure -a

Now, select an appropriate language for yourself - always include en_US.UTF-8 as well as your own language and export your own language for a default

export LANG=en_GB.UTF-8

apt-get install module-init-tools isc-dhcp-client udev netbase ifupdown iproute openssh-server iputils-ping wget net-tools ntpdate vim cron anacron hdparm less smbclient bash-completion hdparm bzip2 unzip zip screen less usbutils psmisc fakeroot ethtool

If space isn't a big concern, or if our require them, then also think about installing the following packages:

apt-get install uboot-mkimage u-boot-tools samba libpam-smbpass openbsd-inetd ftp-ssl vsftpd lighttpd rsync man info logrotate ethtool rsyslog usbmount

Some configuration next - first, lets kill one of the pesky udev rules that may change your network card assignment:

rm /lib/udev/rules.d/75-persistent-net-generator.rules

Setup TTY's (disable the virtual TTYs and turn on serial console)
echo 'T0:2345:respawn:/sbin/getty -L ttyS0 115200 linux' >> /etc/inittab
sed -i 's/^\([1-6]:.* tty[1-6]\)/#\1/' /etc/inittab

Setup network interfaces:
cat <<END > /etc/network/interfaces
auto lo eth0
iface lo inet loopback
iface eth0 inet dhcp
END

setup a machine hostname (replace "RescueDeb" with whatever you feel appropriate:

echo RescueDeb > /etc/hostname

Setup fw_env for reading/writing uboot environment variables (this must match whatever's in your uboot environment):

echo /dev/mtd0 0xA0000 0x20000 0x20000 > /etc/fw_env.config

Basic fstab

cat <<END > /etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
tmpfs          /tmp            tmpfs   defaults          0       0
END

And setup mtab

rm -f /etc/blkid.tab
ln -s /dev/null /etc/blkid.tab
rm -f /etc/mtab
ln -s /proc/mounts /etc/mtab

Now, exit the chroot and copy the .deb file you created earlier into it (I'm assuming that you compiled the source in your home area in kernel/linux-3.4.56) before re-entering the chroot and installing the .deb package:

exit
cp /home/user/kernel/linux-image-3.4.56-kirkwood_0.1_armel.deb /mnt/usr/src
chroot /mnt
cd /usr/src
dpkg -i linux-image-3.4.56-kirkwood_0.1_armel.deb

This will install the kernel to /boot, however as the architecture isn't right (although it's running the arm binaries, it's still in an x86/64 system and flash-kernel fails)

Now to manually create the uImage and uInitrd files:

mkimage -A arm -O linux -T kernel  -C none -a 0x00008000 -e 0x00008000 -n Linux-3.4.56-kirkwood -d /boot/vmlinuz-3.4.56-kirkwood /boot/uImage
mkimage -A arm -O linux -T ramdisk -C gzip -a 0x00000000 -e 0x00000000 -n initramfs-3.4.56-kirkwood -d /boot/initrd.img-3.4.56-kirkwood /boot/uInitrd
Now we can exit the chroot, remove it's links and be ready to copy it onto something

exit
umount /mnt/media/cdrom
umount /mnt/dev/pts
umount /mnt/dev
umount /mnt/proc
umount /mnt/sys
rm /mnt/usr/bin/qemu-arm-static

Take a 1GB (or larger) flash stick and plug it into your computer, have a look at dmesg to see where it's putting it

dmesg | tail -n 30
[225454.245933] usb 2-1.1: USB disconnect, device number 34
[225460.055782] usb 2-1.1: new high-speed USB device number 36 using ehci-pci
[225460.141942] usb 2-1.1: New USB device found, idVendor=0781, idProduct=5151
[225460.141949] usb 2-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[225460.141953] usb 2-1.1: Product: Cruzer Micro
[225460.141957] usb 2-1.1: Manufacturer: SanDisk
[225460.141960] usb 2-1.1: SerialNumber: 2204521D65818E21
[225460.142596] scsi24 : usb-storage 2-1.1:1.0
[225461.145387] scsi 24:0:0:0: Direct-Access     SanDisk  Cruzer Micro     8.01 PQ: 0 ANSI: 0 CCS
[225461.145936] sd 24:0:0:0: Attached scsi generic sg6 type 0
[225461.146957] sd 24:0:0:0: [sdg] 7862911 512-byte logical blocks: (4.02 GB/3.74 GiB)
[225461.147619] sd 24:0:0:0: [sdg] Write Protect is off
[225461.147622] sd 24:0:0:0: [sdg] Mode Sense: 45 00 00 08
[225461.148265] sd 24:0:0:0: [sdg] No Caching mode page present
[225461.148268] sd 24:0:0:0: [sdg] Assuming drive cache: write through
[225461.151622] sd 24:0:0:0: [sdg] No Caching mode page present
[225461.151628] sd 24:0:0:0: [sdg] Assuming drive cache: write through
[225461.161918]  sdg: sdg1 sdg2 sdg3
[225461.165012] sd 24:0:0:0: [sdg] No Caching mode page present
[225461.165019] sd 24:0:0:0: [sdg] Assuming drive cache: write through
[225461.165023] sd 24:0:0:0: [sdg] Attached SCSI removable disk
[225462.355598] EXT4-fs (sdg3): recovery complete
[225462.355608] EXT4-fs (sdg3): mounted filesystem with ordered data mode. Opts: (null)
[225462.361044] EXT2-fs (sdg1): warning: mounting unchecked fs, running e2fsck is recommended

Right, we can see it's picked it up as sdg with three partitions, let's fdisk it and make into a bootable USB stick for our device

umount /dev/sdg*
fdisk /dev/sdg

Delete all partitions (command "d" followed by partition number if there's more than one)

I build my sticks as three partitions - a rootfs (1Gb) partition as ext3, a swap and then a data partition in the rest of it, so the command sequence is:

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-7862910, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-7862910, default 7862910): +1GB

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (1955173-7862910, default 1955173): 
Using default value 1955173
Last sector, +sectors or +size{K,M,G} (1955173-7862910, default 7862910): +512MB

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (1-4, default 3): 3
First sector (2955173-7862910, default 2955173): 
Using default value 2955173
Last sector, +sectors or +size{K,M,G} (2955173-7862910, default 7862910): 
Using default value 7862910

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Then to format the disks, remount them and copy the files across:

mkfs.ext3 -L rootfs /dev/sdg1
mkfs.ext3 -L data /dev/sdg3
mkdir /media/usb0
mount /dev/sdg1 /media/usb0
cp -a /mnt/* /media/usb0

Now at this point, I will generally dismount my target file system and copy it to the data partition of my rescue drive, once booted, I can mount the data partition, mount the image and copy it across to the drive of my GoFlex

umount /dev/sdg1
umount /mnt
mount /dev/sdg3 /media/usb0
cp /home/user/rootfs.img /media/usb0
umount /dev/sdg3

Now, you should be able to plug it into your GoFlex home and boot up. You should also have ssh access, so you can deploy the image as you see fit:

ssh root@RescueDeb
mkdir /media/data
mount /dev/sdb3 /media/data
mount /media/data/rootfs.img /mnt -o loop
mkdir /media/hdd
mount /dev/sda1 /media/hdd
cp -a /mnt/* /media/hdd/

Assumes that the HDD is formatted appropriately, but setting that up's pretty easy (this will blank your hard disc drive):
ssh root@RescueDeb
fdisk /dev/sda
Repeat the selections as we did for setting up the flash memory stick partitions - although I would be inclined to provide a bit more space for expansion - maybe 7.5GB root, 512M swap and the remainder as data - partition schemes are a whole other game
mkfs.ext3 /dev/sda1
mkfs.ext3 /dev/sda3
although there's nothing to prevent you using ext4 (just make sure that you include it as an option in your kernel build)



Edited 10 time(s). Last edit at 08/11/2013 10:27AM by majic79.
Re: Howto: Create a rootfs for Debian Wheezy on GoFlexHome
August 11, 2013 04:45PM
So, some final bits - how to copy the rootfs into UBIFS, this assumes you've got as far as creating the rootfs and are ready to flash it to the device - bear in mind that this information relates specifically to the GoFlex Home - your device will have different configurations (but I'll try and document how you determine the information)

First, boot your rescue system up and using mtd-utils, determine the parameters of your UBI system:

root@RescueDeb: mtdinfo /dev/mtd2 -u
mtd2
Name:                           root
Type:                           nand
Eraseblock size:                131072 bytes, 128.0 KiB
Amount of eraseblocks:          1992 (261095424 bytes, 249.0 MiB)
Minimum input/output unit size: 2048 bytes
Sub-page size:                  512 bytes
OOB size:                       64 bytes
Character device major/minor:   90:4
Bad blocks are allowed:         true
Device is writable:             true
Default UBI VID header offset:  512
Default UBI data offset:        2048
Default UBI LEB size:           126976 bytes, 124.0 KiB
Maximum UBI volumes count:      128

Now go back to your development system and format up a root file system, based on the one you've already created

apt-get install mtd-utils
mkfs.ubifs -r /mnt -m 2048 -e 126976 -c 1992 -o ubifs.img

-r specifies to create it based on the file system found in /mnt
-m says to use minimum io size 2048 bytes (value from mtdinfo)
-e specifies the LEB size (use the default that's reported from mtdinfo -u)
-c is the eraseblock count (again, from mtdinfo)
-o is the output file

Now we need to ubinize it ready for dumping into memory, first create a ubinize.cfg file that provides some details about the file system - mostly self explanatory

cat <<END > ubinize.cfg
cat ubinize.cfg 
[ubifs]
mode=ubi
image=ubifs.img
vol_id=0
vol_size=220MiB
vol_type=dynamic
vol_name=rootfs
vol_flags=autoresize
END
image = image name of file system to process
vol_size needs to be big enough to hold the data - it will compress it, my 212Mb emdebian build compresses down to 110Mb, however if I specify less than 212 it complains about the size and ubinize is pessimistic about compression (assuming that a worst case scenario and that it won't be able to compress at all) - choose something close to the real value (slightly undersize is fine - the autoresize flag will cause it to fill the system when it's first mounted)
vol_name is pretty important - you pass in ubi information on the command line if you want to boot from it, you can specify by volume id or by name, and the default name is "rootfs", so I've stuck with it and it works for me

It will complain about MB vs MiB, so be careful out there, MB doesn't work, MiB does.

Now to ubinize...
ubinize -o ubi.img -m 2048 -p 128KiB -s 512 -O 2048 ubinize.cfg

The -s and -O parameters are pretty critical here - the GoFlex Home's default command line passes in a VID offset of 2048, to maintain some sort of compatibility, I've retained those values here - you could theoretically ditch them here and later on when it comes to flashing to memory, use at your own risk (I don't recommend trying it without some means to get back up and running - UBIFS is last on my boot list, so you should be able to boot from USB or Disc just fine), ditching -s will assume a size equal to the previously selected minimum-io-size (this can be equally bad)

Right, so you've created ubi.img and copied it to your memory stick, I'm assuming that (like me) you're booting from USB, and want to flash the ubi.img file to mtd2 (which is where the root file system for UBI resides on the GoFlex Home) - I've assumed that you've copied ubi.img to the "data" partition of your memory stick (which comes up as sdb3 when you're booted)

You've powered up and connected to your rescue system again and you're ready to rock n roll - as you've built your kernel with full support for UBI and UBIFS (you did do that? You'll need it in order to boot from it!) you won't need to modprobe it (modprobe ubi mtd=2)

mkdir /tmp/data
mount /dev/sdb3 /tmp/data

ubidetach -p /dev/mtd2
ubiformat /dev/mtd2 -f /tmp/data/ubi.img -O 2048 -s 512

And that's it, it should now drop your image into your newly created file system, you can test it out by attaching and then mounting the volume:

ubiattach -p /dev/mtd2 -O 2048
mount -t ubifs /dev/ubi0_0 /mnt
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: