| 1 |
#!/usr/bin/perl |
|---|
| 2 |
# This is a dangerous script! |
|---|
| 3 |
# Be sure you read the README-install file and understand |
|---|
| 4 |
# fully how you are to use this script! |
|---|
| 5 |
# |
|---|
| 6 |
# Not a joke. Read it, you could bork your system with this. |
|---|
| 7 |
# |
|---|
| 8 |
use strict; |
|---|
| 9 |
use Getopt::Long; |
|---|
| 10 |
|
|---|
| 11 |
my $DEVICE = "/dev/sdX"; # CHANGE ME! to your proper SD device! e.g. /dev/sdh |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
my $USER; |
|---|
| 15 |
my @parts; |
|---|
| 16 |
my $Response; |
|---|
| 17 |
my $cmd; |
|---|
| 18 |
my @sysutils = qw/ tar fsck.ext3 mount umount mkdir rmdir file mke2fs mkdosfs fdisk unzip /; |
|---|
| 19 |
my $pgm=`basename $0`; chomp($pgm); |
|---|
| 20 |
|
|---|
| 21 |
use vars qw / $opt_help $opt_fatroot $opt_tarball $opt_partitions /; #/ |
|---|
| 22 |
|
|---|
| 23 |
sub printHelp { |
|---|
| 24 |
print STDERR " |
|---|
| 25 |
Usage: ${pgm} [options] |
|---|
| 26 |
|
|---|
| 27 |
Options: |
|---|
| 28 |
--help -h : help, this menu. |
|---|
| 29 |
--partitions : prep SD card for Zipit Linux by building partitions. |
|---|
| 30 |
--fatroot=</path/filename> : file contents to populate into FAT16 partition. |
|---|
| 31 |
--tarball=</path/filename> : file contents to populate into Linux partition. |
|---|
| 32 |
|
|---|
| 33 |
WARNING: |
|---|
| 34 |
THIS TOOL COULD BE DANGEROUS TO USE IF YOU DO NOT DETERMINE |
|---|
| 35 |
THE CORRECT DEVICE WHERE YOUR SD CARD IS LOCATED! WORKING |
|---|
| 36 |
WITH FILESYSTEMS AND PARTITIONS CAN BORK YOUR SYSTEM. |
|---|
| 37 |
|
|---|
| 38 |
READ THE INSTRUCTIONS, 'README-sdcard', AND MAKE SURE YOU KNOW |
|---|
| 39 |
WHAT YOU ARE DOING BEFORE PROCEEDING. IF NOT, ASK SOMEONE!!! |
|---|
| 40 |
|
|---|
| 41 |
"; #" |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
sub collectCmdLineOpts { |
|---|
| 45 |
my $result = GetOptions( |
|---|
| 46 |
'help|h' => \$opt_help, |
|---|
| 47 |
'fatroot=s' => \$opt_fatroot, |
|---|
| 48 |
'tarball=s' => \$opt_tarball, |
|---|
| 49 |
'partitions' => \$opt_partitions, |
|---|
| 50 |
); |
|---|
| 51 |
# we need at least something to work on... |
|---|
| 52 |
if (@ARGV || !(defined $opt_fatroot || defined $opt_tarball || defined $opt_partitions)) { |
|---|
| 53 |
printHelp(); exit; |
|---|
| 54 |
} |
|---|
| 55 |
exit if (${result} ne 1); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
sub createPartitions { |
|---|
| 59 |
# make sure they read the README-install... |
|---|
| 60 |
$USER = qx/id | cut -f1 -d\\) | cut -f2 -d\\(/; chomp $USER; |
|---|
| 61 |
$USER !~ /root/ && die "You must be root to run this script!"; |
|---|
| 62 |
$DEVICE =~ m:/dev/sdX: && die "'${DEVICE}' is not a proper system device for SD card!"; |
|---|
| 63 |
|
|---|
| 64 |
# is the device name properly formed? |
|---|
| 65 |
if ($DEVICE !~ m:/dev/sd[a-z]$:) { |
|---|
| 66 |
die "Bad device name was given for DEVICE"; |
|---|
| 67 |
} |
|---|
| 68 |
# does the device exist? |
|---|
| 69 |
$cmd = (split(/\//,$DEVICE))[2]; |
|---|
| 70 |
$cmd = "cat /sys/block/".$cmd."/removable 2>&1"; |
|---|
| 71 |
qx/$cmd/ !~ /1/ && do { |
|---|
| 72 |
print "'${DEVICE}' does not appear to be a removable device!\n"; |
|---|
| 73 |
die "Strange device named as SD card!"; |
|---|
| 74 |
}; |
|---|
| 75 |
|
|---|
| 76 |
# check to see if the system tools are available to us! |
|---|
| 77 |
while (@sysutils) { |
|---|
| 78 |
my $util = shift @sysutils; |
|---|
| 79 |
$cmd = qx:which $util 2>&1:; |
|---|
| 80 |
$cmd =~ /which: no/ && die "The utility '${util}' is not installed or in your path!"; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
# so far all seems to be ok. |
|---|
| 84 |
@parts = qx/fdisk -l ${DEVICE} | grep '^\/dev'/; |
|---|
| 85 |
print "\nThis card contains these partitions:\n"; |
|---|
| 86 |
print @parts; |
|---|
| 87 |
print "\nDo you wish to continue and re-partition?\n"; |
|---|
| 88 |
do { print "Enter (yes/no) "; $Response = <STDIN>; } while $Response !~ /^yes$|^no$/; |
|---|
| 89 |
|
|---|
| 90 |
# do we go on? |
|---|
| 91 |
if ($Response !~ /^yes$/) { print "Aborted\n"; exit; } |
|---|
| 92 |
|
|---|
| 93 |
# erase the all the partitions |
|---|
| 94 |
print "\n"; |
|---|
| 95 |
while (@parts) { |
|---|
| 96 |
my ($line,undef) = split(/\s/,pop @parts,2); |
|---|
| 97 |
$line =~ s/${DEVICE}//; |
|---|
| 98 |
if ($line gt 1) { |
|---|
| 99 |
$cmd = "echo 'd\n".$line."\nw\n' | fdisk ${DEVICE}"; |
|---|
| 100 |
} else { |
|---|
| 101 |
$cmd = "echo 'd\nw\n' | fdisk ${DEVICE}"; |
|---|
| 102 |
} |
|---|
| 103 |
print "Deleting partition: ${DEVICE}${line}\n"; |
|---|
| 104 |
$line = qx/$cmd/; |
|---|
| 105 |
} |
|---|
| 106 |
print "\nAll partitions on ${DEVICE} have been removed\n"; |
|---|
| 107 |
|
|---|
| 108 |
# create the two partitions that we need. |
|---|
| 109 |
# first the FAT16 partition. |
|---|
| 110 |
$cmd = "echo 'n\np\n1\n1\n+8M\nt\n6\nw\n' | fdisk ${DEVICE}"; |
|---|
| 111 |
print "\nCreating FAT16 partition on ${DEVICE}1\n"; |
|---|
| 112 |
qx/$cmd/; |
|---|
| 113 |
|
|---|
| 114 |
# next, all the rest of the drive is to be Linux. |
|---|
| 115 |
$cmd = "echo 'n\np\n2\n\n\nw\n' | fdisk ${DEVICE}"; |
|---|
| 116 |
print "\nCreating Linux partition on remainder of ${DEVICE}\n"; |
|---|
| 117 |
qx/$cmd/; |
|---|
| 118 |
|
|---|
| 119 |
# make the filesystems! |
|---|
| 120 |
print "\n\nFormat ${DEVICE}1 as vfat\n"; |
|---|
| 121 |
$cmd = "mkdosfs ${DEVICE}1"; qx/$cmd/; |
|---|
| 122 |
print "\nFormat ${DEVICE}2 as ext3\n"; |
|---|
| 123 |
$cmd = "mke2fs -j ${DEVICE}2"; qx/$cmd/; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
sub fillFatPartition { |
|---|
| 127 |
# mount it, then unzip the image. |
|---|
| 128 |
my $mount = "mount$$"; |
|---|
| 129 |
# validate that the image appears to be reasonable. |
|---|
| 130 |
my $line = qx/file -b ${opt_fatroot} 2>&1/; |
|---|
| 131 |
$line !~ /Zip archive data/ && do { |
|---|
| 132 |
print "\nERROR: ${line}"; |
|---|
| 133 |
die "This does not look like a zipfile: ${opt_fatroot}\n\n"; |
|---|
| 134 |
}; |
|---|
| 135 |
# make a tmp mounting point. |
|---|
| 136 |
qx/mkdir $mount/; |
|---|
| 137 |
# mount the drive partition. |
|---|
| 138 |
$line = qx/mount ${DEVICE}1 ${mount} 2>&1/; |
|---|
| 139 |
if ($line eq '') { |
|---|
| 140 |
print "\nfilling ${DEVICE}1 with the FAT16 component.\n"; |
|---|
| 141 |
qx:rm -rf ${mount}/*:; |
|---|
| 142 |
@parts = qx/echo 'n' | unzip ${opt_fatroot} -d ${mount} 2>&1/; |
|---|
| 143 |
qx/umount $mount/; |
|---|
| 144 |
print @parts; |
|---|
| 145 |
print "\n"; |
|---|
| 146 |
# look for error. |
|---|
| 147 |
$parts[$#parts-1] =~ /\(disk full\?\)/ && do { |
|---|
| 148 |
qx/rmdir $mount/; |
|---|
| 149 |
die "ERROR: Not enough room on ${DEVICE}1 for all those files!\n\n"; |
|---|
| 150 |
} |
|---|
| 151 |
} else { |
|---|
| 152 |
print "\nERROR: ${line}"; |
|---|
| 153 |
die "Something wrong with your SD card???\n\n"; |
|---|
| 154 |
} |
|---|
| 155 |
qx/rmdir $mount/; |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
sub fillExt3Partition { |
|---|
| 159 |
# mount it, then un-tar the image. |
|---|
| 160 |
my $mount = "mount$$"; |
|---|
| 161 |
# validate that the image appears to be reasonable. |
|---|
| 162 |
my $line = qx/file -b -L ${opt_tarball} 2>&1/; |
|---|
| 163 |
$line !~ /POSIX tar archive/ && do { |
|---|
| 164 |
print "\nERROR: ${line}"; |
|---|
| 165 |
die "This does not look like a proper tarball: ${opt_tarball}\n\n"; |
|---|
| 166 |
}; |
|---|
| 167 |
# make sure filesystem is intact. |
|---|
| 168 |
qx/fsck.ext3 ${DEVICE}2 -y/; |
|---|
| 169 |
# make a tmp mounting point. |
|---|
| 170 |
qx/mkdir $mount/; |
|---|
| 171 |
# mount the drive partition. |
|---|
| 172 |
$line = qx/mount ${DEVICE}2 ${mount} 2>&1/; |
|---|
| 173 |
if ($line eq '') { |
|---|
| 174 |
print "\nfilling ${DEVICE}2 with the Linux ext3 component.\n"; |
|---|
| 175 |
qx:rm -rf ${mount}/*:; |
|---|
| 176 |
@parts = qx/tar xCf ${mount} ${opt_tarball} 2>&1/; |
|---|
| 177 |
qx/umount $mount/; |
|---|
| 178 |
print @parts; |
|---|
| 179 |
print "\n"; |
|---|
| 180 |
# look for error. |
|---|
| 181 |
$parts[$#parts] =~ /tar: Error exit/ && do { |
|---|
| 182 |
qx/rmdir $mount/; |
|---|
| 183 |
die "ERROR: Not enough room on ${DEVICE}2 for all those files!\n\n"; |
|---|
| 184 |
} |
|---|
| 185 |
} else { |
|---|
| 186 |
print "\nERROR: ${line}"; |
|---|
| 187 |
die "Something wrong with your SD card???\n\n"; |
|---|
| 188 |
} |
|---|
| 189 |
qx/rmdir $mount/; |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
# process command line toggles. |
|---|
| 193 |
collectCmdLineOpts(); |
|---|
| 194 |
if ($opt_help) { printHelp(); exit; } |
|---|
| 195 |
if ($opt_partitions) { createPartitions() } |
|---|
| 196 |
if ($opt_fatroot) { fillFatPartition() } |
|---|
| 197 |
if ($opt_tarball) { fillExt3Partition() } |
|---|
| 198 |
print "Success!\n\n"; |
|---|
| 199 |
|
|---|