WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: RPi FAT Partition  (Read 3914 times)

Offline CentralWare

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 760
RPi FAT Partition
« on: December 15, 2015, 03:11:11 PM »
This question may have been posted (here or elsewhere) but I haven't been able to find the answer I'm after.
Does the FAT partition require one or more files to be located at a certain position on the disk (like the DOS days) or can I take an SD card and just format the first partition and copy the associated files onto the partition without DD'ing an image onto it?

I'm looking for a way to automate preparing multiple flash cards in one shot and if possible, it would be simpler to just create the primary partition (~16MB) and FAT format it, copy the needed files onto it, and then calculate the balance of the drive and EXT2 format it...  then copy opt/home/tce to that partition.  This way, whether I'm using a 2GB or 32GB card...  the same process would work.

If the RPi has specific offsets, any references to such would be very helpful!

Thanks!

T.J.

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1237
Re: RPi FAT Partition
« Reply #1 on: December 15, 2015, 04:18:47 PM »
The rPi does not use a on disk bootloader (like uBoot), it is hardcoded into the SoC.   The first parition is a FAT32 partition, and is mandatory.

http://elinux.org/RPi_Software

Rather than figuring out the size of the disk at the time of burning the image.   Use a normal image of minimum size, and create a 1st time boot script that automatically resize the volume to max, or whatever size you want, removes the script, and reboots.

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 404
Re: RPi FAT Partition
« Reply #2 on: December 15, 2015, 05:07:54 PM »
This question may have been posted (here or elsewhere) but I haven't been able to find the answer I'm after.
Does the FAT partition require one or more files to be located at a certain position on the disk (like the DOS days) or can I take an SD card and just format the first partition and copy the associated files onto the partition without DD'ing an image onto it?

I'm looking for a way to automate preparing multiple flash cards in one shot and if possible, it would be simpler to just create the primary partition (~16MB) and FAT format it, copy the needed files onto it, and then calculate the balance of the drive and EXT2 format it...  then copy opt/home/tce to that partition.  This way, whether I'm using a 2GB or 32GB card...  the same process would work.

If the RPi has specific offsets, any references to such would be very helpful!

Thanks!

T.J.

hi centralware,

This script *may* do most of what you want. It clones the SD card (mmcblk0) mounted in the SD card slot onto a SD card (sda) which is mounted in a USB slot.

There are a few things I am going to change, like work in sectors not cylinders, and change the start of the first partition to 4Mb instead of 1Mb etc.

If you change +50M to something suitable or $PARTITION_START

PARTITION_START=$(fdisk -l /dev/mmcblk0 | tail -n 1 | sed 's/  */ /g' | cut -d' ' -f 2)

This script is far from perfect but it may be a good starting place. Note: Please use normal caution, as any script using fdisk can be fatal (to the SD card).

Code: [Select]
#!/bin/sh

# Version: 0.02 2015-11-05 GE
# Original.

# Version: 0.01 2015-09-07 GE
# Original.

#========================================================================================
# This script duplicates and prepares a piCore SD card ready for piCorePlayer.
# piCore uses 3 heads, 8 sectors. This script generates 4 heads, 16 sectors.
#
#   1. Insert piCore SD card into mmcblk0p
#   2. Insert second SD card into sda
#
# NOTE: sda will be completely overwritten, ALL data will be lost.
#
# Disk /dev/sda: 7948 MB, 7948206080 bytes
# 4 heads, 16 sectors/track, 242560 cylinders
# Units = cylinders of 64 * 512 = 32768 bytes
#
#    Device Boot      Start         End      Blocks  Id System
# /dev/sda1              33         832       30720   c Win95 FAT32 (LBA)
# /dev/sda2             833        2519       48864  83 Linux
#========================================================================================
# TO DO:
#  - mount routine
#  - umount routine
#  - yes/no continue routine
#  - more error checking
#  - redirection of error messages
#  - cyclinder x units calculation routine for image size.
#----------------------------------------------------------------------------------------

clear
. /etc/init.d/tc-functions

echo "${GREEN}[ INFO ] This script duplicates and prepares a piCore SD card ready for piCorePlayer.${NORMAL}"
echo "${GREEN}[ INFO ] 1. piCore SD card should be in mmcblk0p.${NORMAL}"
echo "${GREEN}[ INFO ] 2. piCorePlyaer SD card should be in sda.${NORMAL}"
echo

# Check that you are running the script as root
if [ "$(id -u)" != "0" ]; then
echo "${RED}[ ERROR ] Script must be run as root.${NORMAL}"
exit 1
else
echo "${GREEN}[ INFO ] Script is running as root.${NORMAL}"
echo
fi

echo "${RED}[ WARNING ] This script will overwrite ALL data on sda!${NORMAL}"
echo "${RED}[ WARNING ] IN DEVELOPMENT - sort of works${NORMAL}"
echo
while true; do
read -p "${YELLOW}Do you wish to continue? ${NORMAL}" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "${RED}[ ERROR ] Please answer yes or no.${NORMAL}";;
esac
done

#========================================================================================
# Check for dosfstools.tcz and download and install
#-----------------------------------------------------------------------------------------
pcp_check_dosfsck() {
echo "${GREEN}"
mount /mnt/mmcblk0p2
echo "${GREEN}[ INFO ] Note: Requires dosfstools.tcz${NORMAL}"
which dosfsck
if [ $? = 0 ]; then
echo "${GREEN}[ INFO ] dosfstools.tcz already installed.${NORMAL}"
else
if [ ! -f /mnt/mmcblk0p2/tce/optional/dosfstools.tcz ]; then
echo "${GREEN}[ INFO ] dosfstools.tcz downloading... ${NORMAL}"
sudo -u tc tce-load -w dosfstools.tcz
[ $? = 0 ] && echo 'Done.' || echo 'Error.'
else
echo "${GREEN}[ INFO ] dosfstools.tcz downloaded.${NORMAL}"
fi
echo "${GREEN}[ INFO ] dosfstools.tcz installing... ${NORMAL}"
sudo -u tc tce-load -i dosfstools.tcz
[ $? = 0 ] && echo "${GREEN}[ INFO ] Done." || echo "${RED}[ ERROR ] Error."
fi
echo "${GREEN}"
umount /mnt/mmcblk0p2
}

#=========================================================================================
# Backup routine with error check and report
#-----------------------------------------------------------------------------------------
pcp_backup() {
# Delete any previous backup_done file
[ -e /tmp/backup_done ] && sudo rm -f /tmp/backup_done

# Do a backup - filetool.sh backs up files in .filetool.lst
echo "${YELLOW}[ INFO ] "
sudo filetool.sh -b
sync
echo "${NORMAL}"

# If backup_status file exists and is non-zero in size then an error has occurred
if [ -s /tmp/backup_status ]; then
echo "${RED}[ ERROR ] Backup status.${YELLOW}"
cat /tmp/backup_status
fi

# If backup_done exists then the backup was successful
if [ -f /tmp/backup_done ]; then
echo "${GREEN}[ OK ] Backup successful.${NORMAL}"
else
echo "${RED}[ ERROR ] Backup failed.${NORMAL}"
fi
}

#=========================================================================================
# Check if partition is mounted otherwise mount it
#-----------------------------------------------------------------------------------------
pcp_mount() {
PARTITION=$1
if mount | grep /mnt/$PARTITION >/dev/null 2>&1; then
echo "${RED}[ ERROR ] /mnt/$PARTITION already mounted.${NORMAL}"
RESULT=0
else
echo "${GREEN}[ INFO ] Mounting /mnt/$PARTITION...${NORMAL}"
sudo mount /mnt/$PARTITION >/dev/null 2>&1
RESULT=$?
fi
[ $RESULT = 0 ] || echo "${RED}[ ERROR ] Mounting /mnt/$PARTITION.${NORMAL}"
}

#=========================================================================================
# Check if partition is unmounted otherwise unmount it
#-----------------------------------------------------------------------------------------
pcp_umount() {
PARTITION=$1
if mount | grep /mnt/$PARTITION >/dev/null 2>&1; then
echo "${GREEN}[ INFO ] Unmounting /mnt/$PARTITION...${NORMAL}"
sudo umount /mnt/$PARTITION >/dev/null 2>&1
RESULT=$?
else
echo "${RED}[ ERROR ] /mnt/$PARTITION already unmounted.${NORMAL}"
RESULT=0
fi
[ $RESULT = 0 ] || echo "${RED}[ ERROR ] Unmounting /mnt/$PARTITION.${NORMAL}"
}

# Work out the number of partitions on sda. There should be 1 or 2 partitions.
NOOFPART=$(fdisk -l /dev/sda | grep "sda" | wc -l)
NOOFPART=$(($NOOFPART - 1))
echo "${YELLOW}"
pcp_mount sda1
if [ $? = 0 ]; then
echo "${GREEN}[ INFO ] sda found.${NORMAL}"
else
echo "${RED}[ ERROR ] sda not found.${NORMAL}"
exit 1
fi
echo "${YELLOW}"
pcp_umount sda1
pcp_umount mmcblk0p2

echo "${GREEN}[ INFO ] Found $NOOFPART partitions on sda.${YELLOW}"
case $NOOFPART in
1)
echo "${GREEN}[ INFO ] Deleting $NOOFPART partition on sda...${YELLOW}"
fdisk /dev/sda <<EOF
p
d
p
w
EOF
;;
2)
echo "${GREEN}[ INFO ] Deleting $NOOFPART partition on sda...${YELLOW}"
fdisk /dev/sda <<EOF
p
d
1
d
p
w
EOF
;;
*)
echo "${RED}[ ERROR ] Incorrect number of partitions.${NORMAL}"
exit 1
;;
esac

echo "${GREEN}[ INFO ] Creating new partitions on sda...${YELLOW}"

fdisk -H 4 -S 16 /dev/sda <<EOF
p
n
p
1
33
832
t
c
n
p
2
833
+50M
p
w
EOF

pcp_check_dosfsck

pcp_umount sda1
pcp_umount sda2

echo "${YELLOW}"
mkfs.vfat -n PCP /dev/sda1
mkfs.ext4 /dev/sda2

while true; do
read -p "${BLUE}Do you wish to continue? ${NORMAL}" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "${RED}[ ERROR ] Please answer yes or no.${NORMAL}";;
esac
done

fdisk -l /dev/sda

pcp_mount mmcblk0p1
pcp_mount mmcblk0p2
pcp_mount sda1
pcp_mount sda2

while true; do
read -p "${BLUE}Do you wish to continue? ${NORMAL}" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "${RED}[ ERROR ] Please answer yes or no.${NORMAL}";;
esac
done

cd /mnt/sda1
cp -Rvp /mnt/mmcblk0p1/* .
cd /mnt/sda2
mkdir tce
cd tce
cp -Rvp /mnt/mmcblk0p2/tce/* .

while true; do
read -p "${YELLOW}Do you wish to shutdown? ${NORMAL}" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "${RED}[ ERROR ] Please answer yes or no.${NORMAL}";;
esac
done
echo "${GREEN}[ INFO ] Shutting down... ${NORMAL}"
exitcheck.sh
exit

regards
Greg

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1237
Re: RPi FAT Partition
« Reply #3 on: December 15, 2015, 05:49:08 PM »
There are a few things I am going to change, like work in sectors not cylinders, and change the start of the first partition to 4Mb instead of 1Mb etc.

Just curious why...

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 404
Re: RPi FAT Partition
« Reply #4 on: December 15, 2015, 06:12:05 PM »
Hi Paul_123.

There are a few things I am going to change, like work in sectors not cylinders, and change the start of the first partition to 4Mb instead of 1Mb etc.
Just curious why...

Code: [Select]
Disk /dev/mmcblk0: 3904 MB, 3904897024 bytes
4 heads, 16 sectors/track, 119168 cylinders
Units = cylinders of 64 * 512 = 32768 bytes

        Device Boot      Start         End      Blocks  Id System
/dev/mmcblk0p1              33         672       20480   c Win95 FAT32 (LBA)
/dev/mmcblk0p2             673        2046       43968  83 Linux

Disk /dev/mmcblk0: 3904 MB, 3904897024 bytes
4 heads, 16 sectors/track, 119168 cylinders, total 7626752 sectors
Units = sectors of 1 * 512 = 512 bytes

        Device Boot      Start         End      Blocks  Id System
/dev/mmcblk0p1            2048       43007       20480   c Win95 FAT32 (LBA)
/dev/mmcblk0p2           43008      130943       43968  83 Linux

For me, when the units are in sectors it is easier for me to read and comprehend.

The official SD card formatter, SDFormater, tends to use a 4MB starting point. I assume they know what they are doing?

Code: [Select]
Disk /dev/sdb: 7948 MB, 7948206080 bytes
81 heads, 10 sectors/track, 19165 cylinders, total 15523840 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            8192    15523839     7757824    b  W95 FAT32

I think the latest piCore images also use a 4MB starting point for the FAT partition.

regards
Greg

« Last Edit: December 15, 2015, 06:15:23 PM by Greg Erskine »

Offline CentralWare

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 760
Re: RPi FAT Partition
« Reply #5 on: December 15, 2015, 07:29:44 PM »
Hi guys!

The goal here is to have a couple dozen uSD cards loaded (through a few 10 port hubs) and basically do the following:
  • dd if=/dev/zero of=$sdcard to wipe the card
  • fdisk $sdcard << EOF (Create FAT and EXT partitions)
  • mkfs.xxx ${sdcard}xx (Format partitions)
  • Copy boot files to FAT partition, copy opt/home/tce to EXT
  • Sync (force cache write) and eject the device
  • Rinse, Lather and Repeat :)
The only difference between each card is a file /mnt/sda1/id which is just an integer that's later used to build the Pi's IP address, hostname and a few other things.  I haven't yet tried to do this from scratch as I wasn't able to determine yet whether or not a required offset was needed for the first partition or if any files such as the 3rd stage bootstrap was needed at a given location, etc.

Due to a recent outage (yesterday) of tinycorelinux.net I'm also going to build a repo support system to ping our local repo followed by public ones to ensure /opt/tcemirror points to somewhere that's available if/when it's called upon and other little tweaks as needed.

Offline CentralWare

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 760
Re: RPi FAT Partition
« Reply #6 on: December 15, 2015, 07:58:39 PM »
Since I couldn't find anything "hard fact" regarding the bootstrap requirements for the primary partition, I went and ran a few tests.  Thus far, no offset is required when building the FAT partition nor are there any specifics (thus far!) as to where (sector) any Pi files are located.  Below, $disks is populated by scanning fdisk -l for removable usb media:
Code: [Select]
for disk in $disks
do
    dd if=/dev/zero of=/dev/$disk bs=1M count=2

    fdisk /dev/$disk << EOF
    (New>Partition1>Start=1,End=14)
    (Type>Fat32-LBA>Bootable)
    (New>Partition2>Start=15,End=NULL)
    (Write)EOF

    mkfs.vfat /dev/${disk}1 && mkfs.ext2 /dev/${disk}2

    mkdir -p /mnt/${disk}1; mount /dev/${disk}1 /mnt/${disk}1
    mkdir -p /mnt/${disk}2; mount /dev/${disk}2 /mnt/${disk}2

    cp $source/boot/* /mnt/${disk}1/ -fR
    cp $source/data/* /mnt/${disk}2/ -fR

    sync; sync; eject /dev/${disk}
done
I kept getting overlaps (Sect. 1-13 for FAT, 13-something for EXT) when DD'ing the Pi7 SSH image which flagged Fdisk errors saying logical sector and physical sector counts mis-matched, which got me to wondering if I'd have problems later down the road.  Except my forgetting to implement /etc/resolv.conf the test went through perfectly.

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1237
Re: RPi FAT Partition
« Reply #7 on: December 15, 2015, 08:28:15 PM »
The official SD card formatter, SDFormater, tends to use a 4MB starting point. I assume they know what they are doing?

I think the latest piCore images also use a 4MB starting point for the FAT partition.

You know what happens when you assume.....lol.  My images that I use on other uboot devices, start a 1MB.  Uboot fits above the 1MB partition.    Those images are partitioned manually with build scripts.

I normally just use dd to wipe out a card, I can't remember the last time I used a formatter on a sd card.  Other than making sure you have room, if you need to install a boot loader,  I don't see why you would start at 4MB
« Last Edit: December 15, 2015, 08:30:25 PM by Paul_123 »

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 404
Re: RPi FAT Partition
« Reply #8 on: December 15, 2015, 09:25:48 PM »
The official SD card formatter, SDFormater, tends to use a 4MB starting point. I assume they know what they are doing?

I think the latest piCore images also use a 4MB starting point for the FAT partition.

You know what happens when you assume.....lol.  My images that I use on other uboot devices, start a 1MB.  Uboot fits above the 1MB partition.    Those images are partitioned manually with build scripts.

I normally just use dd to wipe out a card, I can't remember the last time I used a formatter on a sd card.  Other than making sure you have room, if you need to install a boot loader,  I don't see why you would start at 4MB

hi Paul_123,

I have made hundreds of piCore/piCorePlayer images with the first partition starting at 1MB. Never had a problem!

I bought a Sandisk SD card last week to verify how a fresh SD card is formatted from the manufacture. It had a 4MB start and was almost definitely done by SDFormatter.

Code: [Select]
$ fdisk -ul /dev/sda

Disk /dev/sda: 7948 MB, 7948206080 bytes
81 heads, 10 sectors/track, 19165 cylinders, total 15523840 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device Boot      Start         End      Blocks  Id System
/dev/sda1            8192    15523839     7757824   b Win95 FAT32

The SD Association was established by the major players Panasonic, SanDisk Corporation and Toshiba Corporation. https://www.sdcard.org/index.html

So my assumption is reasonably safe. I can't imagine SDFormatter not working properly.

Having said that I only use SDFormatter to research their formatting specifications and once I SDFormatter to rescue a SD card whose size wasn't recognised correctly.

So 99.9% of the time is use piCore Linux tools.

regards
Greg