Linux 和 Windows 共享交換區(轉)

post0發表於2007-08-11
Linux 和 Windows 共享交換區(轉)[@more@]

1. 前言

現在,越來越多的人在一臺使用Linux 和 Windows. 這應該說是Linux的勝利. 我們知

道, Linux 要使用交換分割槽,

Windows 要使用交換檔案。如果一臺PIII, 有192M 記憶體,我們分配給Linux 192M 交換

區, Windows 2000 至少要

200M. 那麼,我們要用近400M硬碟空間。如果交換區更大,浪費就更可觀了。

由於兩個系統的交換區都只是執行時的臨時資料,所以,我們採用動態修改分割槽資訊的方法

來達到共享目的.

2. 方法簡介

1). 備份Windows 分割槽資訊。

2). 當啟動Linux時, 將該分割槽做成Linux 交換區,並將其啟用。

3) 當Linux 關閉時,將該分割槽重新變成Windows 交換區。

3. 具體步驟

1). 分割槽

Fdisk, 只分主分割槽, 不分擴充套件分割槽

2). 安裝 Windows.

3). 安裝Linux (佔一個主分割槽)

4). 在Linux 下, 分擴充套件分割槽)

5). 設定Linux交換區(假定/dev/hda10)

6). 建立winswap 裝置

ln -s /dev/hda10 /dev/winswap

7). 啟動Linux, 關閉交換區

# swapoff -a

8). 從檔案安裝表中刪除該分割槽

vi /etc/fstab

註釋掉該行 (/dev/hda10)

9). 將該分割槽該成 FAT16 或其他 DOS 分割槽.

10). 啟動 Windows

a). 格式化該分割槽

b). 將系統的交換檔案設在該分割槽.

11). 啟動 Linux, 計算Total Special Sectors

公式:

T = r + (s * f) + (d / 16)

引數:

Reserved Sectors at beginning : r

FAT Copies : f

Sectors per FAT : s

Root directory entries : d

參見: msinfo.sh

註解: 可以執行 msinfo.sh 來獲得.

# msinfo.sh /dev/hda10

12). 備份Windows 分割槽資訊

# dd if=/dev/winswap bs=512 count=XXX | gzip -9 > /etc/winswap.gz

這裡, XXX = T

14). 編寫啟動, 退出指令碼, 並把它們放在 /etc/rc.d/xxx.

可用 grep -nr * | grep swapon (或 swapoff) 來找系統啟用和關閉交換區, 將它們

替換稱我們

的指令碼)

我們在附錄中提供了啟動和關閉的指令碼.

4. 附加說明

1. 本文使用的是FAT16, 如果使用NTFS 或其它, 必須修改指令碼.

2. mkswap /dev/winswap 377496 (這個值需要修改, 依照你的分割槽大小)

5. 參考資料:

Linux HOWTO: Swap-space

6. 附錄 -- 相應的指令碼

1. msinfo.sh 指令碼

#!/bin/sh

#

# msinfo.sh This shell script displays the boot sector of the

# given partition.

#

# Author: Rahul U. Joshi

#

# Modifications Removed the use of expr and replaced it by the let

# command.

# check for command line arguments

if [ $# -ne 1 ]; then

echo "Usage: msinfo "

exit 1

fi

# check whether the input name is a block device

if [ ! -b $1 ]; then

echo "msinfo: $1 is not a block device"

exit 1

fi

# create two temporary files for use

TMPFILE=`mktemp -q /tmp/$0.XXXXXX`

if [ $? -ne 0 ]; then

echo "msinfo: Can't create temp file, exiting..."

exit 1

fi

TXTFILE=`mktemp -q /tmp/$0.XXXXXX`

if [ $? -ne 0 ]; then

echo "msinfo: Can't create temp file, exiting..."

rm -f $TMPFILE

exit 1

fi

back_title="`printf "%78s" "msinfo, Information about FAT16 filesystem --

Rahul

Joshi"`"

dialog --title "Boot sector of $1" --backtitle "$back_title" --infobox

" Analysing boot sector for $1 Please wait ..." 14 60

# truncate TXTFILE to zero length

echo > $TXTFILE

# get Formatting DOS version

dd 2>/dev/null if=$1 bs=1 count=8 skip=3 | dd 2>/dev/null of=$TMPFILE

printf >>$TXTFILE "%30s : %s " "Formatting DOS version" "`cat $TMPFILE`"

# get file system

dd 2>/dev/null if=$1 bs=1 count=8 skip=54 | dd 2>/dev/null of=$TMPFILE

printf >>$TXTFILE "%30s : %s " "Filesystem" "`cat $TMPFILE`"

# check if filesystem in a FAT16

if [ "`cat $TMPFILE`" != "FAT16 " ]; then

dialog --title "Boot sector of $1" --backtitle "$back_title" --infobox

" Can't find a FAT16 filesystem on $1" 14 60

exit 2

fi

# get volume label in boot sector

dd 2>/dev/null if=$1 bs=1 count=11 skip=43 | dd 2>/dev/null of=$TMPFILE

printf >>$TXTFILE "%30s : %s " "Volume label in boot sector" "`cat

$TMPFILE`"

# get Sector size

dd 2>/dev/null if=$1 bs=1 count=2 skip=11| od -An -tdS | dd 2>/dev/null

of=$TMPFILE

printf >>$TXTFILE "%30s : %d " "Sector size" `cat $TMPFILE`

sector_size=`cat $TMPFILE`

# get Reserved sectors

dd 2>/dev/null if=$1 bs=1 count=2 skip=14| od -An -tdS | dd 2>/dev/null

of=$TMPFILE

printf >>$TXTFILE "%30s : %d " " Reserved sectors" `cat $TMPFILE`

reserved_sectors=`cat $TMPFILE`

# get FAT sectors

dd 2>/dev/null if=$1 bs=1 count=1 skip=16| od -An -tdS | dd 2>/dev/null

of=$TMPFILE

fat_count=`cat $TMPFILE`

dd 2>/dev/null if=$1 bs=1 count=2 skip=22| od -An -tdS | dd 2>/dev/null

of=$TMPFILE

sectors_per_fat=`cat $TMPFILE`

# calculate the no of sectors allocated for FAT's

let fat_sectors=fat_count*sectors_per_fat

printf >>$TXTFILE "%30s : %u (%u x %u) " "FAT sectors" "$fat_sectors"

"$fat_count" "$sectors_per_fat"

# get root directory sectors

dd 2>/dev/null if=$1 bs=1 count=2 skip=17| od -An -tdS | dd 2>/dev/null

of=$TMPFILE

root_sectors=`cat $TMPFILE`

# calculate the no of sectors allocated for root directory

let root_sectors=root_sectors*32/sector_size

printf >>$TXTFILE "%30s : %u " "Root directory sectors" "$root_sectors"

# get Total special sectors

let total=reserved_sectors+fat_sectors+root_sectors

printf >>$TXTFILE "%30s : %u " "Total special sectors" "$total"

# display the information in a message box

dialog --title "Boot sector of $1" --backtitle "$back_title" --msgbox

"`cat $TXTFILE`" 14 60

# delete temporary files

rm -f $TMPFILE

rm -f $TXTFILE

# end of msinfo.sh

2. swapinit.sh

#!/bin/sh

#

# /etc/rc.d/init.d/swapinit.sh - activate the swap partition

#

# written by Rahul U. Joshi

# Verify and initialize swap space

#

echo -n 'Verifying swap space... '

loopcount=0

# flag to indicate whether the partition has been activated or not

activated=0

# check for signatures 6 times before giving up

while [ $loopcount -lt 6 ]

do

if [ "`/bin/dd 2>/dev/null if=/dev/winswap bs=1 count=10 skip=4086`" =

'SWAPSPACE2' ]; then

echo "Linux signature found, iteration $loopcount"

echo "Activating swap partitions"

swapon /dev/winswap

activated=1

break

elif [ "`/bin/dd 2>/dev/null if=/dev/winswap bs=1 count=5 skip=54`" =

'FAT16' ]; then

echo "DOS signature found, iteration $loopcount"

echo "Making swap partition"

mkswap /dev/winswap 377496

echo "Activating swap partitions"

swapon /dev/winswap

activated=1

break

else

let loopcount=loopcount+1

fi

done

if [ $activated -ne 1 ] ; then

echo "Swap signature not found after $loopcount tries"

echo "No swapping partitions activated"

exit 1

fi

3. swaphalt.sh

#!/bin/sh

#

# /etc/rc.d/init.d/swapinit.sh - activate the swap partition

#

# written by Rahul U. Joshi

# Verify and initialize swap space

#

echo -n 'Verifying swap space... '

loopcount=0

# flag to indicate whether the partition has been activated or not

activated=0

# check for signatures 6 times before giving up

while [ $loopcount -lt 6 ]

do

if [ "`/bin/dd 2>/dev/null if=/dev/winswap bs=1 count=10 skip=4086`" =

'SWAPSPACE2' ]; then

echo "Linux signature found, iteration $loopcount"

echo "Activating swap partitions"

swapon /dev/winswap

activated=1

break

elif [ "`/bin/dd 2>/dev/null if=/dev/winswap bs=1 count=5 skip=54`" =

'FAT16' ]; then

echo "DOS signature found, iteration $loopcount"

echo "Making swap partition"

mkswap /dev/winswap 377496

echo "Activating swap partitions"

swapon /dev/winswap

activated=1

break

else

let loopcount=loopcount+1

fi

done

if [ $activated -ne 1 ] ; then

echo "Swap signature not found after $loopcount tries"

echo "No swapping partitions activated"

exit 1

fi (linux知識寶庫

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-945398/,如需轉載,請註明出處,否則將追究法律責任。

相關文章