Skip to content

Instantly share code, notes, and snippets.

@OhMeadhbh
Last active August 29, 2015 14:07
Show Gist options
  • Save OhMeadhbh/68a31d86dba2716e74c4 to your computer and use it in GitHub Desktop.
Save OhMeadhbh/68a31d86dba2716e74c4 to your computer and use it in GitHub Desktop.
Shell scripts to modify a Raspbian image
14,17c14,17
< #overscan_left=16
< #overscan_right=16
< #overscan_top=16
< #overscan_bottom=16
---
> overscan_left=16
> overscan_right=16
> overscan_top=16
> overscan_bottom=16
21,22c21,22
< #framebuffer_width=1280
< #framebuffer_height=720
---
> framebuffer_width=512
> framebuffer_height=320
9,11c9,11
< CODESET="guess"
< FONTFACE=""
< FONTSIZE=""
---
> CODESET="Uni3"
> FONTFACE="TerminusBold"
> FONTSIZE="VGA16"
# fixuser
#
# change default username and password on a raspbian image
#
# see https://gist.github.com/OhMeadhbh/68a31d86dba2716e74c4 for license info
#
# This program looks in the root directory specified for files like etc/passwd and
# etc/group, then hacks them to change the user and group name "pi" to a user you
# specify as the second parameter.
#
# I use it when modifying the contents of new Raspbian images using the scripts at
# https://gist.github.com/OhMeadhbh/68a31d86dba2716e74c4
#
if [ $# -lt 2 ]; then
echo "usage: $0 <root dir> <username>"
exit 1
fi
ROOT=$1
NEWUSERNAME=$2
TMPFILE=`mktemp`
cat < $ROOT/etc/passwd > $TMPFILE
awk -F":" '$1 == "pi" { print "'$NEWUSERNAME':" $2 ":" $3 ":" $4 ":" $5 ":/home/'$NEWUSERNAME':" $7} $1 != "pi" { print $0 }' < $TMPFILE > $ROOT/etc/passwd
NEWPASS=`mkpasswd -m sha-512`
cat < $ROOT/etc/shadow > $TMPFILE
awk -F":" '$1 == "pi" { print "'$NEWUSERNAME':'$NEWPASS':" $3 ":" $4 ":" $5 ":" $6 ":" $7 ":" $8 ":" $9 } $1 != "pi" { print $0 }' < $TMPFILE > $ROOT/etc/shadow
sed s/\:pi/\:$NEWUSERNAME/ < $ROOT/etc/group > $TMPFILE
awk -F":" '$1 == "pi" { print "'$NEWUSERNAME':" $2 ":" $3 ":" $4 } $1 != "pi" { print $0 }' < $TMPFILE > $ROOT/etc/group
sed s/\:pi/\:$NEWUSERNAME/ < $ROOT/etc/gshadow > $TMPFILE
awk -F":" '$1 == "pi" { print "'$NEWUSERNAME':" $2 ":" $3 ":" $4 } $1 != "pi" { print $0 }' < $TMPFILE > $ROOT/etc/gshadow
mv $ROOT/home/pi $ROOT/home/$NEWUSERNAME
rm $TMPFILE
# imgmount
#
# Mount partitions in an image file to /mnt/loop<n>p<m>
#
# See https://gist.github.com/OhMeadhbh/68a31d86dba2716e74c4 for license info.
#
# Remember to run me as root passing the name of the image file as a parameter.
# Also, try to remember the name of the loopback device it prints out; you'll
# need it for the imgumount command. Running it thusly puts the loopback device
# in the variable $LODEV:
#
# LODEV=`sudo imgmount ~/2014-09-09-wheezy-raspbian.img` ; echo $LODEV
#
# so after you're finished you can run imgumount like this:
#
# sudo imgumount $LODEV
IMGFILE=$1
LODEV=`losetup -f --show $IMGFILE`
DEVNO=`echo $LODEV | rev | cut -b 1`
for PART in $( sudo fdisk -l $LODEV| awk '/^\/dev/ { print count++ ":" $2 }' ); do
MNTDIR="/mnt/loop${DEVNO}p`echo $PART | cut -f 1 -d :`"
OFFSET="`echo $PART | cut -f 2 -d :`"
mkdir -p $MNTDIR > /dev/null 2>&1
mount -o offset=$(( 512 * `echo $OFFSET` )),loop $LODEV $MNTDIR
done
echo $LODEV
# imgumount
#
# Unmount partitions that have been mounted with imgmount
#
# See https://gist.github.com/OhMeadhbh/68a31d86dba2716e74c4 for license info.
LODEV=$1
DEVNO=`echo $LODEV | rev | cut -b 1`
for PART in $( sudo fdisk -l $LODEV| awk '/^\/dev/ { print count++ ":" $2 }' ); do
MNTDIR="/mnt/loop${DEVNO}p`echo $PART | cut -f 1 -d :`"
umount $MNTDIR
done
Copyright (c) 2014, Smithee, Spelvin, Agnew & Plinge, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Smithee, Spelvin, Agnew & Plinge nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment