上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

RIPLinux9.3のext2&3インストーラーmkextlin.shの解剖

RIPLinux
http://www.tux.org/pub/people/kent-robotti/looplinux/rip/
のバージョン9.3で、USBメモリなどにext2またはext3パーティションを作成してインストールするためのスクリプトは、
mkextlin.sh
だ。

GNU general public license 2
のようなので、公開しても良さそうなので、以下に記します。どんどん、赤字で注釈を付けていきます。
赤字の注釈の部分を削除しないと正常に動作しないかもしれません。



#!/bin/bash
#
# This is mkextlin.sh. (Jun 3, 2009)
#
# bash mkextlin.sh -h "For Help!"
#
PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PATH"

help() {
# この行からはhelpという関数
msg="Press enter to scroll, q to Quit!"
if [ -x "`type -path less`" ]; then
# lessコマンドが(あればパス付きで表示させ、それが)実行可能ファイルなら次の行以降を実行
pager=less
# pagerをlessに設定。fiまで飛ぶ
elif [ -x "`type -path more`" ]; then
# moreコマンドが(あればパス付きで表示させ、それが)実行可能ファイルなら次の行以降を実行
pager=more
# pagerをmoreに設定。fiまで飛ぶ
else
pager=cat
# pagerをcatに設定。
msg_cat="
Press [Shift]-[Page-Up] to scroll back!"
fi

# 次の行から<<EOFまでpagerに設定されたコマンドにより可能なら↓↑キーでのスクロール可能状態で表示させる
$pager << EOF

$msg

===========================================================================
If you want to install Linux to a Linux partition on a USB Flash drive.
===========================================================================

The USB device should be at least 256 MB to hold the contents of
rootfs.cgz from RIPLinuX-X.X.iso.

The USB device should be at least 128 MB to hold the contents of
rootfs.cgz from RIPLinux-X.X-non-X.iso.

========================================================================

It will create a partition and put an Ext2 or Ext3 filesystem on it,
then the Linux system will be extracted to it. You'll be able
to boot and use it, just like if it was installed to a hard
drive Linux partition.

# fdisk -l "To find the device the USB drive is on!"
"For example, if it says /dev/sdb1, just put '/dev/sdb' etc."

# bash mkextlin.sh RIPLinuX-X.X.iso /dev/sdb "You'll loose anything
on the USB drive!"

# bash mkextlin.sh -t ext3 RIPLinuX-X.X.iso /dev/sdb "If you want to use an
ext3 filesystem!"

The above example assumes the USB drive is on /dev/sdb!

Instead of specifying an ISO image you can specify a source directory
that contains the contents of the ISO image, laid out the same way
as the ISO image.

# bash mkextlin.sh source_directory /dev/sdb

If you had RIPLinuX on CD mounted under /mnt/cdrom, you could do this.

bash mkextlin.sh /mnt/cdrom /dev/sdb

To boot the Linux system, the BIOS must support booting from a USB device.
$msg_cat
EOF
# ここまで上記のpagerコマンドにより↓↑キーでのスクロール可能状態で表示させる。最初の$msgと最後の$msg_catがミソ。勉強になるなぁ
exit
}

if [ "$1" = "-h" ]; then
# このスクリプトの実行されたときの続くオプションの最初のものが-hだったら、次の行以降を実行。
help
elif [ -n "$1" ]; then

if [ -x "`type -path mktemp`" ]; then
tmpdir="`mktemp -d`"
if [ ! $? = 0 ]; then
mkdir /tmp/tmp.$$ && tmpdir=/tmp/tmp.$$
fi
else
mkdir /tmp/tmp.$$ && tmpdir=/tmp/tmp.$$
fi

if [ -z "$tmpdir" ]; then
echo "Error creating temp directory."
exit
fi

error() {
# この行からerror関数の定義
umount $tmpdir/extlinux 2>/dev/null
umount $tmpdir/rip1 2>/dev/null
rm -rf $tmpdir
exit 1
}
# この行までerror関数の定義

if [ "$1" = "-t" ]; then
ext3=yes
shift 2
fi

if [ ! -f "$1" -a ! -d "$1" ]; then
echo "ERROR: Can't find ISO image, or source directory \`$1'!"
exit
fi

if [ ! -b "$2" ]; then
echo "ERROR: Can't find USB device \`$2'!"
exit
fi

if echo $2 | grep -q "[0-9]$" ; then
echo "ERROR: The device '$2' can't end in a number!"
exit
fi

if [ ! -x "`type -path fdisk`" ]; then
echo "ERROR: Can't find \`fdisk'!"
exit
fi

if [ ! -x "`type -path cpio`" ]; then
echo "ERROR: Can't find \`cpio'!"
exit
fi

if [ ! -x "`type -path mke2fs`" ]; then
echo "ERROR: Can't find \`mke2fs'!"
exit
fi

if [ -d "$1" ]; then
if [ "$1" = "." ]; then
dir="`basename "$PWD"`"
if [ "$dir" = "boot" -a -f rootfs.cgz ]; then
dir="`dirname "$PWD"`"
elif [ -d boot -a -f boot/rootfs.cgz ]; then
dir="$PWD"
else
echo "Don't put '.', put the full path to the directory!"
exit
fi
else
dir="$1"
fi
ln -s "$dir" $tmpdir/rip1 || error
else
if ! grep -q -w "loop" /proc/devices ; then
unset loaded
if grep -q "loop\.ko" /lib/modules/`uname -r`/modules.dep 2>/dev/null ; then
modprobe loop 2>/dev/null && loaded=yes
fi
if [ ! "$loaded" = "yes" ]; then
echo "ERROR: I need loop support in the kernel, or loaded as a module!"
echo "modprobe loop"
exit
fi
fi
if ! grep -q -w "iso9660" /proc/filesystems ; then
unset loaded
if grep -q "iso9660\.ko" /lib/modules/`uname -r`/modules.dep 2>/dev/null ; then
modprobe iso9660 2>/dev/null && loaded=yes
fi
if [ ! "$loaded" = "yes" ]; then
echo "ERROR: I need iso9660 support in the kernel, or loaded as a module!"
echo "modprobe iso9660"
exit
fi
fi
mkdir -p $tmpdir/rip1
mount -r -t iso9660 -o loop $1 $tmpdir/rip1 || exit
fi
else
# 次の行の"から画面に表示させる内容
echo "
----------------------------------------------------------------------
Usage: bash mkextlin.sh RIPLinuX-X.X.iso /dev/sdb

The above example assumes the USB device is on /dev/sdb!

Instead of specifying an ISO image you can specify a source directory
that contains the contents of the ISO image, laid out the same way
as the ISO image.

bash mkextlin.sh source_directory /dev/sdb

If you have RIPLinuX on CD mounted under /mnt/cdrom, you could do this.

bash mkextlin.sh /mnt/cdrom /dev/sdb

For complete Help! type \`bash mkextlin.sh -h'.
----------------------------------------------------------------------
"
# この行までが画面に表示させる内容
exit
fi

extlinux_conf() {
# この行からextlinux_conf関数の内容
pt=$1
echo "DEFAULT menu.c32
PROMPT 0
MENU TITLE RIPLinuX

LABEL Boot Linux system!
KERNEL $kernel
APPEND vga=normal rootdelay=6 root=$pt ro

LABEL Edit and put 'root=/dev/XXXX' Linux partition to boot!
KERNEL $kernel
APPEND vga=normal ro root=/dev/XXXX

LABEL Boot Memory Tester!
KERNEL memtest
APPEND -

LABEL Boot GRUB bootloader!
KERNEL grub.exe --config-file=(hd0,0)/boot/extlinux/menu.lst" > $tmpdir/extlinux/boot/extlinux/extlinux.conf
}
# この行までextlinux_conf関数の内容

grub_conf() {
# この行からgrub_conf関数の内容
pt=$1
# 次の行からを以下のファイルに出力する
echo "title --------------------------- ( RIPLinuX ) ---------------------------
root (hd0)

title Boot Linux system!
kernel (hd0,0)/boot/extlinux/$kernel vga=normal rootdelay=6 root=$pt ro

title Boot Memory Tester!
kernel (hd0,0)/boot/extlinux/memtest

title Boot Windows NT/2K/XP NTLDR on partition #1 of first hard drive!
rootnoverify (hd1,0)
map (hd1) (hd0)
chainloader --edx=0x0080 (hd1,0)/ntldr

title Boot Windows Vista/7 Bootmgr on partition #1 of first hard drive!
rootnoverify (hd1,0)
map (hd1) (hd0)
chainloader --edx=0x0080 (hd1,0)/bootmgr

title Boot Windows NT/2K/XP NTLDR on partition #2 of first hard drive!
rootnoverify (hd1,1)
map (hd1) (hd0)
chainloader --edx=0x0080 (hd1,1)/ntldr

title Boot Windows Vista/7 Bootmgr on partition #2 of first hard drive!
rootnoverify (hd1,1)
map (hd1) (hd0)
chainloader --edx=0x0080 (hd1,1)/bootmgr

title Boot MBR on first hard drive!
rootnoverify (hd1)
map (hd1) (hd0)
chainloader +1

title Boot partition #1 on first hard drive!
rootnoverify (hd1,0)
map (hd1) (hd0)
chainloader +1

title Boot partition #2 on first hard drive!
rootnoverify (hd1,1)
map (hd1) (hd0)
chainloader +1

title Boot partition #3 on first hard drive!
rootnoverify (hd1,2)
map (hd1) (hd0)
chainloader +1

title Boot partition #4 on first hard drive!
rootnoverify (hd1,3)
map (hd1) (hd0)
chainloader +1

title Boot MBR on second hard drive!
rootnoverify (hd2)
map (hd2) (hd0)
chainloader +1

title Boot partition #1 on second hard drive!
rootnoverify (hd2,0)
map (hd2) (hd0)
chainloader +1

title Boot partition #2 on second hard drive!
rootnoverify (hd2,1)
map (hd2) (hd0)
chainloader +1

title Boot partition #3 on second hard drive!
rootnoverify (hd2,2)
map (hd2) (hd0)
chainloader +1

title Boot partition #4 on second hard drive!
rootnoverify (hd2,3)
map (hd2) (hd0)
chainloader +1

title --- For help press 'c', type: 'help'
root (hd0)

title --- For usage examples, type: 'cat (hd0,0)/boot/extlinux/grub.txt'
root (hd0)" > $tmpdir/extlinux/boot/extlinux/menu.lst
# この行までを>の後のファイルに出力する
}
# この行までgrub_conf関数の内容

for f in kernel32 kernel64 rootfs.cgz syslinux/extlinux.bin ; do
if [ ! -f $tmpdir/rip1/boot/$f ]; then
umount $tmpdir/rip1 2>/dev/null
rm -rf $tmpdir
echo "ERROR: Can't find \`/boot/$f' on \`$1'!"
exit
fi
done

umount $2 2>/dev/null
umount $2"1" 2>/dev/null

echo "*** Creating partition on $2... ***"

dd if=$2 of=$tmpdir/mbr.bin bs=512 count=1 1>/dev/null 2>/dev/null
dd if=/dev/zero of=$2 bs=512 count=1 1>/dev/null 2>/dev/null
echo -e "n\np\n1\n\n\na\n1\nw\n" | fdisk $2 1>/dev/null 2>/dev/null || fdisk_error=yes
if [ "$fdisk_error" = "yes" ]; then
dd if=$tmpdir/mbr.bin of=$2 bs=512 count=1 1>/dev/null 2>/dev/null
rm -f $tmpdir/mbr.bin
echo "ERROR: Fdisk had a problem partitioning $2"
exit
fi

sleep 3
dev=$2"1"

if [ ! -b "$dev" ]; then
echo "ERROR: Can't find USB device \`$dev'!"
exit
fi

if [ "$ext3" = "yes" ]; then
echo "*** Creating Ext3 filesystem on $dev..."
mke2fs -q -j $dev || exit
else
echo "*** Creating Ext2 filesystem on $dev..."
mke2fs -q $dev || exit
fi

mkdir -p $tmpdir/extlinux
mount $dev $tmpdir/extlinux || exit

# 次の行の"から画面に表示
echo -n "
Do you want the 32-bit kernel or the 64-bit kernel?
Put 64, or just press enter for 32-bit kernel: "
# この行まで画面に表示
read ans
if [ "$ans" = "" ]; then
kernel=kernel32
echo
echo "*** Installing 32-bit kernel!"
else
kernel=kernel64
echo
echo "*** Installing 64-bit kernel!"
fi

echo "*** Extracting rootfs.cgz to $dev..."
( cd $tmpdir/extlinux && gzip -dc $tmpdir/rip1/boot/rootfs.cgz | cpio --quiet -iumd || error )

#if [ "$kernel" = "kernel64" ]; then
# この行からは無効にされている。開発途中で、消されたのだろう。
# ( cd $tmpdir/extlinux/lib/modules && ls | grep -v "\.64bit" | xargs rm -rf )
#else
# rm -rf $tmpdir/extlinux/lib/modules/*.64bit
#fi
# この行まで無効にされている。開発途中で、消されたのだろう。

mkdir -p $tmpdir/extlinux/boot/extlinux
rm -f $tmpdir/extlinux/init
cp $tmpdir/rip1/boot/$kernel $tmpdir/extlinux/boot/extlinux || error
cp $tmpdir/rip1/boot/isolinux/memtest $tmpdir/extlinux/boot/extlinux
cp $tmpdir/rip1/boot/grub4dos/grub.exe $tmpdir/extlinux/boot/extlinux
cp $tmpdir/rip1/boot/isolinux/menu.c32 $tmpdir/extlinux/boot/extlinux || error
cp $tmpdir/rip1/boot/doc/grub.txt $tmpdir/extlinux/boot/extlinux

if [ -x $tmpdir/rip1/boot/syslinux/extlinux.bin ]; then
$tmpdir/rip1/boot/syslinux/extlinux.bin -i $tmpdir/extlinux/boot/extlinux 2>/dev/null
else
cp $tmpdir/rip1/boot/syslinux/extlinux.bin $tmpdir || error
chmod 755 $tmpdir/extlinux.bin || error
$tmpdir/extlinux.bin -i $tmpdir/extlinux/boot/extlinux 2>/dev/null
rm -f $tmpdir/extlinux.bin
fi
extlinux_conf $dev
grub_conf $dev

if [ "$ext3" = "yes" ]; then
echo "$dev / ext3 defaults 1 1" > $tmpdir/extlinux/etc/fstab
else
echo "$dev / ext2 defaults 1 1" > $tmpdir/extlinux/etc/fstab
fi

umount $tmpdir/extlinux 2>/dev/null
cat $tmpdir/rip1/boot/syslinux/mbr.bin > $2
umount $tmpdir/rip1 2>/dev/null
rm -rf $tmpdir
# 次の行の"から画面に表示
echo "
*** The Linux system on the USB drive should be ready to boot!
Done!"
# この行まで画面に表示
exit 0
# 終了コード0(問題無し)で終了




以上がmkextlin.shの内容です。

なお、ヘルプファイル、mkextlin.txtには以下のように書かれています。




==========================================================================

If you want to install Linux to a Linux partition on a USB Flash drive.

==========================================================================



The USB device should be at least 256 MB to hold the contents of

rootfs.cgz from RIPLinuX-X.X.iso.



The USB device should be at least 128 MB to hold the contents of

rootfs.cgz from RIPLinux-X.X-non-X.iso.



========================================================================



It will create a partition and put an Ext2 or Ext3 filesystem on it,

then the Linux system will be extracted to it. You'll be able

to boot and use it, just like if it was installed to a hard

drive Linux partition.



# fdisk -l "To find the device the USB drive is on!"

"For example, if it says /dev/sdb1, just put '/dev/sdb' etc."



# bash mkextlin.sh RIPLinuX-X.X.iso /dev/sdb "You'll loose anything

on the USB drive!"



# bash mkextlin.sh -t ext3 RIPLinuX-X.X.iso /dev/sdb "If you want to use an

ext3 filesystem!"



Instead of specifying an ISO image you can specify a source directory

that contains the contents of the ISO image, laid out the same way

as the ISO image.



# bash mkextlin.sh source_directory /dev/sdb



If you had RIPLinuX on CD mounted under /mnt/hdd, you could do this.



bash mkextlin.sh /mnt/hdd /dev/sdb



========================================================================
関連記事

テーマ : Linux
ジャンル : コンピュータ

コメントの投稿

管理者にだけ表示を許可する

プロフィール

kiyoshi

Author:kiyoshi
FC2ブログへようこそ!

最新記事
最新コメント
最新トラックバック
月別アーカイブ
カテゴリ
検索フォーム
RSSリンクの表示
リンク
ブロとも申請フォーム

この人とブロともになる

QRコード
QRコード
since 2011/9/25
現在の閲覧者数:
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。