diff mbox

[OpenWrt-Devel] brcm2708: add script for creating SD partitions and one more missing symbol

Message ID 1424189525-18968-1-git-send-email-noltari@gmail.com
State Rejected
Headers show

Commit Message

Álvaro Fernández Rojas Feb. 17, 2015, 4:12 p.m. UTC
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
---
 target/linux/brcm2708/config-3.18            |   1 +
 target/linux/brcm2708/image/Config.in        |  13 +-
 target/linux/brcm2708/image/Makefile         |  32 ++++-
 target/linux/brcm2708/image/create_sdcard.sh | 191 +++++++++++++++++++++++++++
 4 files changed, 235 insertions(+), 2 deletions(-)
 create mode 100755 target/linux/brcm2708/image/create_sdcard.sh

Comments

Daniel Golle Feb. 17, 2015, 5:28 p.m. UTC | #1
Hi,

I'm working on something similar for the other targets and here
are my two cents on the topic:

On Tue, Feb 17, 2015 at 05:12:05PM +0100, Álvaro Fernández Rojas wrote:
> +  which parted > /dev/null

I ended up using coreutils-sfdisk instead of parted for the simple
reason that parted is not part of OpenWrt's core package set.
However, I reckon that including parted is actually the nicer option.
Also note, that if you are intending to use this script as a part
of the ImageBuilder, you can use the "ptgen" tool.
If this is supposed to be a stand-alone installer for OpenWrt on
SD-cards, I don't get why it should be included in the source tree
at all.

> +  # this is needed to format the drive
> +  which mkfs.ext4 > /dev/null
> ...
> +# create ext4 partition with optimized settings for running on flash/sd
> +# See http://blogofterje.wordpress.com/2012/01/14/optimizing-fs-on-sd-card/ for reference.

Is there any specific reason why ext4 is used for r/w rootfs?
From what I understood, the only good reason to do so is compatibility
with legacy distributions, right?

I'm currently trying two other directions:
f2fs seems to be the appropriate replacement for jffs2 on MMC,
as it can work nicely as an overlayfs on top of squashfs, it's
light-weight and specificly designed for black-box-FTL devices.

However, fstools currently lacks support for block devices and
as identifying block devices requires libblkid(-tiny), we'll
probably end-up with two build variants of libfstools, one with
and one without support for block devices... (John: what do you
think?)

btrfs on the other hand also has sufficiently good support for
flash drives and also got built-in support for snapshots and
raid, but that might as well be overkill.
However, it does provide all the needed features to become the
single solution for all block-device based setups.

No matter if squashfs+f2fs or btrfs will be used, I believe that
rootfs-overlay, snapshot and failsafe-mode are key features of
OpenWrt. Designing new targets in a way which relies on a single
read-write mounted rootfs feels like taking the wrong turn to me.
Having that option is nice, just like having a single JFFS2 or
UBIFS r/w rootfs. With eMMC being more and more common, it'd be sad
if it remains the only choice...


Cheers


Daniel
diff mbox

Patch

diff --git a/target/linux/brcm2708/config-3.18 b/target/linux/brcm2708/config-3.18
index 873a0f6..d700210 100644
--- a/target/linux/brcm2708/config-3.18
+++ b/target/linux/brcm2708/config-3.18
@@ -258,6 +258,7 @@  CONFIG_USB_UAS=y
 CONFIG_USB_USBNET=y
 CONFIG_VECTORS_BASE=0xffff0000
 CONFIG_VFP=y
+# CONFIG_VIDEO_BCM2835 is not set
 # CONFIG_VMSPLIT_2G is not set
 # CONFIG_VMSPLIT_3G is not set
 CONFIG_VT=y
diff --git a/target/linux/brcm2708/image/Config.in b/target/linux/brcm2708/image/Config.in
index f7abd9d..b6aea19 100644
--- a/target/linux/brcm2708/image/Config.in
+++ b/target/linux/brcm2708/image/Config.in
@@ -1,5 +1,16 @@ 
+config BRCM2708_BOOTFS_TGZ
+	bool "Build a compressed tar archive of the boot filesystem."
+	depends on TARGET_brcm2708
+	default y
+
+config BRCM2708_SD_SCRIPT
+	bool "Include SD creation script"
+	depends on TARGET_brcm2708
+	select BRCM2708_BOOTFS_TGZ
+	select TARGET_ROOTFS_TARGZ
+	default y
+
 config BRCM2708_SD_BOOT_PARTSIZE
 	int "Boot (SD Card) filesystem partition size (in MB)"
 	depends on TARGET_brcm2708
 	default 20
-
diff --git a/target/linux/brcm2708/image/Makefile b/target/linux/brcm2708/image/Makefile
index ae977a6..56e6f48 100644
--- a/target/linux/brcm2708/image/Makefile
+++ b/target/linux/brcm2708/image/Makefile
@@ -1,5 +1,5 @@ 
 # 
-# Copyright (C) 2012 OpenWrt.org
+# Copyright (C) 2012-2015 OpenWrt.org
 #
 # This is free software, licensed under the GNU General Public License v2.
 # See /LICENSE for more information.
@@ -28,6 +28,36 @@  define Image/Build/RaspberryPi
 	mcopy -i $(KDIR)/boot.img $(KDIR)/Image ::kernel.img  # Copy OpenWrt built kernel
 	./gen_rpi_sdcard_img.sh $(BIN_DIR)/$(IMG_PREFIX)-sdcard-vfat-$(1).img $(KDIR)/boot.img $(KDIR)/root.$(1) \
 		$(CONFIG_BRCM2708_SD_BOOT_PARTSIZE) $(CONFIG_TARGET_ROOTFS_PARTSIZE)
+
+ ifeq ($(CONFIG_BRCM2708_BOOTFS_TGZ),y)
+	rm -rf $(BUILD_DIR)/brcm2708-bootfs
+	mkdir -p $(BUILD_DIR)/brcm2708-bootfs
+
+	$(CP) $(BUILD_DIR)/brcm2708-gpu-fw-boot/bootcode.bin $(BUILD_DIR)/brcm2708-bootfs/
+	$(CP) $(BUILD_DIR)/brcm2708-gpu-fw-boot/COPYING.linux $(BUILD_DIR)/brcm2708-bootfs/
+	$(CP) $(BUILD_DIR)/brcm2708-gpu-fw-boot/LICENCE.broadcom $(BUILD_DIR)/brcm2708-bootfs/
+	$(CP) $(BUILD_DIR)/brcm2708-gpu-fw-boot/start.elf $(BUILD_DIR)/brcm2708-bootfs/
+	$(CP) $(BUILD_DIR)/brcm2708-gpu-fw-boot/start_cd.elf $(BUILD_DIR)/brcm2708-bootfs/
+	$(CP) $(BUILD_DIR)/brcm2708-gpu-fw-boot/fixup.dat $(BUILD_DIR)/brcm2708-bootfs/
+	$(CP) $(BUILD_DIR)/brcm2708-gpu-fw-boot/fixup_cd.dat $(BUILD_DIR)/brcm2708-bootfs/
+	$(CP) cmdline.txt $(BUILD_DIR)/brcm2708-bootfs/
+	$(CP) config.txt $(BUILD_DIR)/brcm2708-bootfs/
+	$(CP) $(KDIR)/Image $(BUILD_DIR)/brcm2708-bootfs/kernel.img
+
+	$(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE)-bootfs.tar.gz --numeric-owner --owner=0 --group=0 -C $(BUILD_DIR)/brcm2708-bootfs/ .
+ endif
+
+ ifeq ($(CONFIG_BRCM2708_SD_SCRIPT),y)
+	$(CP) create_sdcard.sh $(BIN_DIR)
+
+	rm -f $(BIN_DIR)/config_sdcard
+	touch $(BIN_DIR)/config_sdcard
+	echo -e "BOOT_SIZE=$(CONFIG_BRCM2708_SD_BOOT_PARTSIZE)" >> $(BIN_DIR)/config_sdcard
+	echo -e "BOOTFS_FILE=$(IMG_PREFIX)-$(PROFILE)-bootfs.tar.gz" >> $(BIN_DIR)/config_sdcard
+	echo -e "ROOTFS_FILE=$(IMG_PREFIX)-$(PROFILE)-rootfs.tar.gz" >> $(BIN_DIR)/config_sdcard
+
+	$(TAR) -zcvf $(BIN_DIR)/$(IMG_PREFIX)-createsd.tar.gz -C $(BIN_DIR)/ $(IMG_PREFIX)-$(PROFILE)-bootfs.tar.gz $(IMG_PREFIX)-$(PROFILE)-rootfs.tar.gz config_sdcard create_sdcard.sh
+ endif
 endef
 
 define Image/Build
diff --git a/target/linux/brcm2708/image/create_sdcard.sh b/target/linux/brcm2708/image/create_sdcard.sh
new file mode 100755
index 0000000..93eb5fa
--- /dev/null
+++ b/target/linux/brcm2708/image/create_sdcard.sh
@@ -0,0 +1,191 @@ 
+#!/bin/sh
+
+. ./config_sdcard
+
+################################################################################
+#      Based on OpenELEC create_sdcard - http://www.openelec.tv
+#      Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
+#      Copyright (C) 2015 Álvaro Fernández Rojas (noltari@gmail.com)
+################################################################################
+
+# usage:   sudo ./create_sdcard <drive>
+# example: sudo ./create_sdcard /dev/sdb
+
+if [ "$(id -u)" != "0" ]; then
+  clear
+  echo "#########################################################"
+  echo "# please execute with 'sudo' or -DANGEROUS!!!- as root  #"
+  echo "# example: sudo ./create_sdcard <drive>                 #"
+  echo "#########################################################"
+  exit 1
+fi
+
+if [ -z "$1" ]; then
+  clear
+  echo "#########################################################"
+  echo "# please execute with your drive as option              #"
+  echo "# example: sudo ./create_sdcard /dev/sdb                #"
+  echo "# or:      sudo ./create_sdcard /dev/mmcblk0            #"
+  echo "#########################################################"
+  exit 1
+fi
+
+DISK="$1"
+case $DISK in
+  "/dev/mmcblk"*)
+    PART1="${DISK}p1"
+    PART2="${DISK}p2"
+    ;;
+  *)
+    PART1="${DISK}1"
+    PART2="${DISK}2"
+    ;;
+esac
+
+clear
+echo "#########################################################"
+echo "#                                                       #"
+echo "#                OpenWRT SD Installer                   #"
+echo "#                                                       #"
+echo "#########################################################"
+echo "#                                                       #"
+echo "#     This will wipe any data off your chosen drive     #"
+echo "# Please read the instructions and use very carefully.. #"
+echo "#                                                       #"
+echo "#########################################################"
+
+# check for some required tools
+
+  # this is needed to partion the drive
+  which parted > /dev/null
+  if [ "$?" = "1" ]; then
+    clear
+    echo "#########################################################"
+    echo "#                                                       #"
+    echo "# OpenWRT missing tool - Installation will quit         #"
+    echo "#                                                       #"
+    echo "#      We can't find the required tool \"parted\"         #"
+    echo "#      on your system.                                  #"
+    echo "#      Please install it via your package manager.      #"
+    echo "#                                                       #"
+    echo "#########################################################"
+    exit 1
+  fi
+
+  # this is needed to format the drive
+  which mkfs.vfat > /dev/null
+  if [ "$?" = "1" ]; then
+    clear
+    echo "#########################################################"
+    echo "#                                                       #"
+    echo "# OpenWRT missing tool - Installation will quit         #"
+    echo "#                                                       #"
+    echo "#      We can't find the required tool \"mkfs.vfat\"      #"
+    echo "#      on your system.                                  #"
+    echo "#      Please install it via your package manager.      #"
+    echo "#                                                       #"
+    echo "#########################################################"
+    exit 1
+  fi
+
+  # this is needed to format the drive
+  which mkfs.ext4 > /dev/null
+  if [ "$?" = "1" ]; then
+    clear
+    echo "#########################################################"
+    echo "#                                                       #"
+    echo "# OpenWRT missing tool - Installation will quit         #"
+    echo "#                                                       #"
+    echo "#      We can't find the required tool \"mkfs.ext4\"      #"
+    echo "#      on your system.                                  #"
+    echo "#      Please install it via your package manager.      #"
+    echo "#                                                       #"
+    echo "#########################################################"
+    exit 1
+  fi
+
+  # this is needed to tell the kernel for partition changes
+  which partprobe > /dev/null
+  if [ "$?" = "1" ]; then
+    clear
+    echo "#########################################################"
+    echo "#                                                       #"
+    echo "# OpenWRT missing tool - Installation will quit         #"
+    echo "#                                                       #"
+    echo "#      We can't find the required tool \"partprobe\"      #"
+    echo "#      on your system.                                  #"
+    echo "#      Please install it via your package manager.      #"
+    echo "#                                                       #"
+    echo "#########################################################"
+    exit 1
+  fi
+
+# (TODO) umount everything (if more than one partition)
+  umount ${DISK}*
+
+# remove all partitions from the drive
+  echo "writing new disklabel on $DISK (removing all partitions)..."
+  parted -s "$DISK" mklabel msdos
+
+# create a single partition
+  echo "creating partitions on $DISK..."
+  parted -s "$DISK" mkpart primary fat32 -- 0 ${BOOT_SIZE}MiB 
+# align the partition on 4mb boundary
+  parted -s "$DISK" mkpart primary ext2 -- $(($BOOT_SIZE + 4))MiB -2cyl
+
+# make partition active (bootable)
+  echo "marking partition active..."
+  parted -s "$DISK" set 1 boot on
+
+# tell kernel we have a new partition table
+  echo "telling kernel we have a new partition table..."
+  partprobe "$DISK"
+
+# create filesystem
+  echo "creating filesystem on $PART1..."
+  mkfs.vfat "$PART1" -I -n boot
+
+# create ext4 partition with optimized settings for running on flash/sd
+# See http://blogofterje.wordpress.com/2012/01/14/optimizing-fs-on-sd-card/ for reference.
+  echo "creating filesystem on $PART2..."
+  mkfs.ext4 -O ^has_journal -E stride=2,stripe-width=1024 -b 4096 "$PART2" -L rootfs
+
+# mount partitions
+  MNT_BOOT=/tmp/openwrt_boot
+  MNT_ROOTFS=/tmp/openwrt_rootfs
+
+  echo "mounting partition $PART1 on $MNT_BOOT..."
+  rm -rf $MNT_BOOT
+  mkdir -p $MNT_BOOT
+  mount -t vfat "$PART1" $MNT_BOOT
+
+  echo "mounting partition $PART2 on $MNT_ROOTFS..."
+  rm -rf $MNT_ROOTFS
+  mkdir -p $MNT_ROOTFS
+  mount -t ext4 "$PART2" $MNT_ROOTFS
+
+# copy boot files
+  echo "copying bootloader files to $MNT_BOOT..."
+  tar -zxvf $BOOTFS_FILE -C $MNT_BOOT
+
+# copy rootfs files
+  echo "copying rootfs files to $MNT_ROOTFS..."
+  tar -zxvf $ROOTFS_FILE -C $MNT_ROOTFS
+
+# sync disk
+  echo "syncing disk..."
+  sync
+
+# unmount partitions
+  echo "unmounting partition $MNT_BOOT..."
+  umount $MNT_BOOT
+
+  echo "unmounting partition $MNT_ROOTFS..."
+  umount $MNT_ROOTFS
+
+# cleaning
+  echo "cleaning tempdirs..."
+  rmdir $MNT_BOOT
+  rmdir $MNT_ROOTFS
+
+echo "...installation finished"