Dear visitor, welcome to Dreamboard. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.
This post has been edited 2 times, last edit by "nattar" (Mar 8th 2006, 6:54pm)
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
#! /usr/bin/python
import os, sys, getopt
#################################################################
# This is GPL software.
# Written by noggie
#################################################################
# The script will prepare a CF for booting an image.
# - Partition with FAT + EXT3 (+ optionally swap)
# - Format partitions
# Partition sizes. The remainder goes to the ext3 partition.
# Swap is only made if the "-s" option is provided.
SIZE_FAT = 30 # MB
SIZE_SWAP = 96 # MB
# Some paths
CF_PREFIX = '/dev/ide/host1/bus0/target0/lun0/'
CF_DISK = CF_PREFIX + 'disc'
CF_FAT = CF_PREFIX + 'part1'
CF_EXT3 = CF_PREFIX + 'part2'
CF_SWAP = CF_PREFIX + 'part3'
PROC_PREFIX = '/proc/ide/hdc/'
# Optional arguments
opts, pargs = getopt.getopt(sys.argv[1:], 'svb', ['swap', 'verbose', 'batch'])
opts = dict(opts)
make_swap = (opts.has_key('-s') or opts.has_key('--swap'))
verbose = (opts.has_key('-v') or opts.has_key('--verbose'))
# Warn the poor user
if not opts.has_key('-b'):
sys.stdout.write('THIS WILL DELETE ANY PREVIOUS CONTENTS ON THE CF!!\n')
print 'Do you want to continue? '
if sys.stdin.readline().lower()[0] != 'y':
os.exit(1)
def vprint(fmt, *args):
if verbose:
sys.stderr.write(fmt % args)
# Get CF geometry
fd = open(PROC_PREFIX + 'geometry', 'r')
for line in fd:
if line.startswith('logical'):
cylinders, heads, sectors = line.replace('logical', '').strip().split('/')
fd.close()
# Convert strings to ints.
cylinders = int(cylinders)
heads = int(heads)
sectors = int(sectors)
cf_size = cylinders * heads * sectors
vprint('Geometry : %d/%d/%d (=%d MB)\n', cylinders, heads, sectors, cf_size/2048)
# Make sure CF is not in use
fd = open('/proc/mounts', 'r')
for line in fd:
if line.startswith(CF_PREFIX):
dir = line.split()[1]
vprint('Umounting %s\n', dir)
os.system('/bin/umount ' + dir)
fd.close()
fd = open('/proc/swaps', 'r')
for line in fd:
if line.startswith(CF_PREFIX):
dev = line.split()[0]
vprint('Removing swap %s\n', dev)
os.system('/sbin/swapoff ' + dev)
# Clear existing partition table
vprint('Clearing existing partition table\n');
fd = os.popen('2>/dev/null 1>&2 /sbin/sfdisk -q ' + CF_DISK, 'w')
fd.write('0,0\n')
fd.close()
# Compute new partitions
sectors_pr_mb = 1024 * 1024 / 512
sectors_pr_cyl = sectors * heads
fat_cyls = (SIZE_FAT * sectors_pr_mb) / sectors_pr_cyl + 1
if make_swap:
swap_cyls = (SIZE_SWAP * sectors_pr_mb) / sectors_pr_cyl + 1
else:
swap_cyls = 0
ext3_cyls = cylinders - 1 - fat_cyls - swap_cyls
vprint('Calculated partition sizes\n');
vprint('\tPartition table : 1 (=%d KB)\n', sectors_pr_cyl / 2)
vprint('\tFAT : %d (=%.2f KB)\n', fat_cyls, fat_cyls * sectors_pr_cyl / 2.0)
vprint('\tEXT3 : %d (=%.2f KB)\n', ext3_cyls, ext3_cyls * sectors_pr_cyl / 2.0)
vprint('\tswap : %d (=%.2f KB)\n', swap_cyls, swap_cyls * sectors_pr_cyl / 2.0)
# Create new partition table
vprint('Creating new partition table\n')
fd = os.popen('2>/dev/null 1>&2 /sbin/sfdisk -q ' + CF_DISK, 'w')
fd.write(',%d,6\n' % fat_cyls)
if make_swap:
fd.write(',%d,\n' % ext3_cyls)
fd.write(',,S\n')
else:
fd.write(',,\n')
fd.close()
if verbose:
os.system('/sbin/sfdisk -l ' + CF_DISK)
# Format partitions
vprint('Formatting partitions\n')
os.system('/usr/sbin/mkdosfs ' + CF_FAT)
os.system('/sbin/mkfs.ext3 ' + CF_EXT3)
if make_swap:
os.system('/sbin/mkswap ' + CF_SWAP)
# Make sure we have mount-points
for dir in ['/media/cf', '/media/cf2']:
try:
os.mkdir(dir)
except OSError:
pass
# Add entries to /etc/fstab, if necessary
fd = open('/etc/fstab', 'r')
fstab = fd.readlines()
fd.close()
changed = 0
if len(filter(lambda x: x.startswith(CF_FAT), fstab)) == 0:
fstab.append(CF_FAT + ' /media/cf auto defaults 0 0\n')
changed = 1
if len(filter(lambda x: x.startswith(CF_EXT3), fstab)) == 0:
fstab.append(CF_EXT3 + ' /media/cf2 auto defaults 0 0\n')
changed = 1
if make_swap and (len(filter(lambda x: x.startswith(CF_SWAP), fstab)) == 0):
fstab.append(CF_SWAP + ' swap swap defaults 0 0\n')
changed = 1
if changed:
fd = open('/etc/fstab', 'w')
fd.writelines(fstab)
fd.close()
# Finally, mount the partitions
os.system('/bin/mount /media/cf')
os.system('/bin/mount /media/cf2')
if make_swap:
os.system('/sbin/swapon ' + CF_SWAP)
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#! /bin/sh
cp /boot/* /media/cf
cd /media/cf2
cp -a /bin /etc /hdd /home /lib /sbin /share /tmp /usr .
mkdir boot dev media proc sys var
cd media
mkdir card cf cf2 cifs dvd hdd mmc1 net nfs ram realroot union usb
(
echo /cf/bootlogo.elf
echo /cf/vmlinux.gz root=/dev/hdc2 console=ttyS0,115200
) > /media/cf/autorun.bat
sed -e '/boot/s/^/#/' -e '/media\/cf/s/^/#/' /etc/fstab > /media/cf2/etc/fstab
echo "/dev/ide/host1/bus0/target0/lun0/part1 /boot auto defaults 0 0" >> /media/cf2/etc/fstab
|