Вы находитесь на странице: 1из 0

Installing Linux in a file in an NTFS partition

Posted on 29.November.2011
Scenario
You received a nice laptop from your company to work with, with excellent processing capabilities, a reasonable
display and a good amount of RAM. The computer is supposed to be used for scientific research. You want to
unleash all this power. However, the computer has two important limitations:
Even though we are at the end of 2011, with many powerful Linux distributions available and even with
Windows 8 on the horizon, someone decided that this otherwise excellent machine should instead run on the
obsolete Windows XP.
Due to policy issues, and not hardware or software limitations, it is not possible to partition the internal hard
disk and install any other operating system for dual boot. The rationale stems from a single policy for all the
departments.
There is no limitation on storing data, but there are also limitations on installing software to run in Windows.
VirtualBox is explicitly blocked, as well as a number of other important applications.
A possible alternative to this hideous scenario could be run a Live CD of any recent Linux distribution. However,
storing data between sessions in these live systems is complicated. Even more is to install additional software as
needed.
Another possibility would be run the OS from an external drive. This works perfectly. However, for a laptop, to
keep carrying the external disk all the time is cumbersome. Moreover, the disk takes a couple of seconds to be
enabled again after a sleep or hibernation, so the system may not resume properly when trying to wake up.
A solution is as follows: create a large file in the NTFS partition, install
Linux inside it, and boot from an USB flash drive. To discourage curiosity,
the file can be encrypted. This article describes the steps to accomplish
this task using openSUSE, but a similar strategy should work with any
recent distribution.
Brainder.
Information not shared is information lost.
Installing Linux in a file in an NTFS partition | Brainder. http://brainder.org/2011/11/29/installing-linux-in-a-file-in-an-ntfs-partition/
1 of 9 16-Sep-13 11:04 AM
Requirements
A computer with an NTFS partition, inside which you want to have Linux, without tampering with the actual
Windows installation.
openSUSE installation DVD (tested with 12.1, and should work with other recent versions). Similar steps
may work with other distributions.
An USB flash drive which will be used to boot the Linux system and will also store the kernel. This flash
drive must stay connected during all the time in which the system is on, therefore it is more convenient if it is
physically small. See a picture below.
An external USB hard disk, to be used for a temporary installation that will be transferred to the file in the
NTFS partition.
The procedure
Step 1: Plug the external hard disk and install openSUSE in it. Choose the desktop environment that suits you
better (e.g. KDE/GNOME or other). Its advisable to make this choice now, as it will save you time and effort,
instead of leaving to add the graphical mode later. Leave / boot on its own partition as it will also facilitate steps
later. Do not create a swap partition for this installation (ignore warnings that may appear because of this). Lets
call this as the system E, as it will reside temporarily on the External disk.
During the installation, install GRUB to the external disk itself, not to the internal drive (i.e., not / dev/ sda), as you
dont want to tamper with the structure of internal hard drive or with the way as Windows boots. Before restarting
the new system, if you changed the partitioning of the external disk, make sure that the partition that contains
/ boot has the bootable flag active (use f di sk to fix if needed).
Step 2: Make another install of openSUSE, this time to the flash drive. Again, put / boot on its own partition. Be
generous with / boot , as it will accommodate 2 ramdisks and perhaps 2 or more kernels. Use at least 100MB.
This installation can (and should) be the minimal, with no X (text-only). Lets call this as system F, as it will reside
in the USB Flash drive.
For a 16GB USB drive, a suggestion is:
1st partition: 14GB, FAT or similar, to use as a general USB drive, nothing special.
2nd partition: 1.8GB, ext4, to be mounted as as / , and where the system will reside.
3rd partition: 0.2GB, ext4, to be mounted as / boot .
During the installation:
Like in the installation to the external hard disk, do not install GRUB to the internal drive, but to the USB
memory stick.
For the list of software, install the nt f s- 3g, nt f spr ogs and cr ypt set up. Install the man pages as well, as
they may be useful.
Installing Linux in a file in an NTFS partition | Brainder. http://brainder.org/2011/11/29/installing-linux-in-a-file-in-an-ntfs-partition/
2 of 9 16-Sep-13 11:04 AM
Before booting the system F, make sure that the partition that contains / boot is bootable (use f di sk to fix if
needed).
Step 3: Boot the system F, the flash drive. First task is to create the volume inside the NTFS filesystem, which will
store the whole Linux system:
a) Create a key-file. No need for paranoia here, as the encryption is only to discourage any curious who sees
these big files from Windows. J ust a simple text file stored in the USB flash drive, separate from the encrypted
volume:
echo Secr et Passwor d | sha1sum> / r oot / key. t xt
Note that in the command above, the password is not SecretPassword, but instead, the content of the file
key. t xt , as produced by the command sha1sum. This includes the space and dash (- ) that are inside, as well as
a newline character (invisible) at the end.
Do not store the keyfile in the NTFS partition. Put it, e.g., in the / r oot directory of the flash drive.
b) Mount the physical, internal hard drive that contains the NTFS partition:
mkdi r - p / host
mount - t nt f s / dev/ sda1 / host
c) Create a big file with random numbers in the NTFS partition. This will be the place where the full system will
reside. For 100GB, use:
dd i f =/ dev/ ur andomof =/ host / nt f sr oot bs=2048 count =50000000
This will take several hours. You may want to leave it running overnight.
d) Mount it as a loop device:
l oset up / dev/ l oop0 / host / nt f sr oot
e) Initialize as a LUKS device:
cr ypt set up l uksFor mat / dev/ l oop0 / r oot / key. t xt
A message that any data on the device will irrevocably be deleted is shown. In this case, the device is the file full
Installing Linux in a file in an NTFS partition | Brainder. http://brainder.org/2011/11/29/installing-linux-in-a-file-in-an-ntfs-partition/
3 of 9 16-Sep-13 11:04 AM
of random bits that we just created. So, confirm by typing YES in capital letters.
f) Make a device-mapping:
cr ypt set up l uksOpen / dev/ l oop0 r oot - - key- f i l e / r oot / key. t xt
This will create a / dev/ mapper / r oot device, which is an unencrypted view of the content of the big file previously
created. The / dev/ l oop0 is an encrypted view and is not meaningful for reading/writing.
g) Create the file system that later will host the root (i.e., / ):
mkf s. ext 4 - b 1024 / dev/ mapper / r oot
The block size will be 1024. Smaller sizes will mean more free disk space for a given amount of stored data.
g) Repeat the steps b-g for what will be the swap partition (8GB in this example):
dd i f =/ dev/ ur andomof =/ host / nt f spage bs=2048 count =4000000
l oset up / dev/ l oop1 / host / nt f spage
cr ypt set up l uksFor mat / dev/ l oop1 / r oot / key. t xt
cr ypt set up l uksOpen / dev/ l oop1 swap - - key- f i l e / r oot / key. t xt
mkswap / dev/ mapper / swap
Step 4: [optional] Having made the initial configurations for the future / and swap, its now time to make a small
modification of the initrd of the flash drive, so that it can boot from USB 3.0 if needed (even if the flash drive itself
is 2.0, this step is still needed if you plan to plug it into an USB 3.0 port in the same computer):
a) Make a backup copy of the original initrd and update the symbolic link:
cd / boot
cp - p i ni t r d- 3. 1. 0- 1. 2- deskt op i ni t r d- 3. 1. 0- 1. 2- opensuse
r mi ni t r d
l n - s i ni t r d- 3. 1. 0- 1. 2- opensuse i ni t r d
b) Edit the / et c/ sysconf i g/ ker nel and add the xhci - hcd module to the initial ramdisk:
I NI TRD_MODULES="xhci - hcd"
c) Run mki ni t r d to create a new ramdisk.
Installing Linux in a file in an NTFS partition | Brainder. http://brainder.org/2011/11/29/installing-linux-in-a-file-in-an-ntfs-partition/
4 of 9 16-Sep-13 11:04 AM
cd / boot
mki ni t r d
d) Rename it to something more representative:
mv / boot / i ni t r d- 3. 1. 0- 1. 2- deskt op / boot / i ni t r d- 3. 1. 0- 1. 2- usbf l ash
The newly generated initrd will load the system that will be left in the flash drive, now with support for USB 3.0,
and wont be changed from now on.
Step 5: Restart the computer and boot into the system E (the external hard disk)
a) Edit the / et c/ sysconf i g/ ker nel and add these modules to the initial ramdisk:
I NI TRD_MODULES="xhci - hcd f use l oop dm- cr ypt aes cbc sha256"
Again, the xhci - hcd module is to allow booting the system from an USB 3.0 port (even if the flash drive is 2.0). If
you dont have USB 3.0 ports, this module can be omitted. The other modules are necessary to mount the NTFS
partition, to mount files as a loopback devices, and to allow encryption/decryption using the respective algorithms
and standards.
b) Run mki ni t r d to create a new ramdisk.
cd / boot
mki ni t r d
c) Make a copy of the newly created initrd to a place where it can be edited.
cp / boot / i ni t r d- 3. 1. 0- 1. 2- deskt op / r oot / i ni t r d- 3. 1. 0- 1. 2- har ddi sk
mkdi r / r oot / i ni t r d- har ddi sk
cd / r oot / i ni t r d- har ddi sk
cat . . / i ni t r d- 3. 1. 0- 1. 2- har ddi sk | gunzi p | cpi o - i d
d) Inside the initrd, edit the file boot / 83- mount . sh (i.e. / r oot / i ni t rd- har ddi sk/ boot / 83- mount . sh) and
replace all its content by:
#! / bi n/ bash
# Thi s wi l l be t he mount poi nt of t he NTFS par t i t i on
mkdi r - p / host
Installing Linux in a file in an NTFS partition | Brainder. http://brainder.org/2011/11/29/installing-linux-in-a-file-in-an-ntfs-partition/
5 of 9 16-Sep-13 11:04 AM
# Mount t he NTFS par t i t i on
nt f s- 3g / dev/ sda1 / host
# Mount t he 100GB f i l e as a l oop devi ce
l oset up / dev/ l oop0 / host / nt f sr oot
# Open t he f i l e f or decr ypt i on ( as a mapped devi ce)
cr ypt set up l uksOpen / dev/ l oop0 r oot - - key- f i l e / key. t xt
# Mount t he ext 4 f i l esyst emon i t as / r oot
mount - o r w, acl , user _xat t r - t ext 4 / dev/ mapper / r oot / r oot
# Mount t he 8GB f i l e as a l oop devi ce
l oset up / dev/ l oop1 / host / nt f spage
# Open t he f i l e f or decr ypt i on ( as a mapped devi ce)
cr ypt set up l uksOpen / dev/ l oop1 swap - - key- f i l e / key. t xt
e) Copy the l oset up, cr ypt set up, dmset up and nt f s- 3g executables to the initrd:
cp / sbi n/ l oset up / sbi n/ cr ypt set up / sbi n/ dmset up / r oot / i ni t r d- har ddi sk/ sbi n
cp / usr / bi n/ nt f s- 3g / r oot / i ni t r d- har ddi sk/ bi n
f) Copy also all the required libraries for these executables that may be missing (use l dd to discover which).
There is certainly a way to script this, but to prepare and test the script will take as much time as to copy them
manually. This has to be done just once.
g) Recreate the initrd:
cd / r oot / i ni t r d- har ddi sk
f i nd . | cpi o - H newc - o | gzi p - 9 > . . / i ni t r d- 3. 1. 0- 1. 2- har ddi sk
h) Copy the new initrd to the / boot partition of the USB flash drive, not to the / boot of the external hard disk. So,
plug the USB drive and mount its boot partition:
mkdi r - p / mnt / usbr oot
mount - t ext 4 / dev/ sdX3 / mnt / usbboot
cp - p / r oot / i ni t r d- 3. 1. 0- 1. 2- har ddi sk / mnt / usbboot /
Step 6: Restart the system, now booting again into the system F (the USB flash drive):
a) Create two short scripts to mount and umount the encrypted filesystem. This will save time when you boot
Installing Linux in a file in an NTFS partition | Brainder. http://brainder.org/2011/11/29/installing-linux-in-a-file-in-an-ntfs-partition/
6 of 9 16-Sep-13 11:04 AM
through the pendrive to do any maintenance on the installed system:
#! / bi n/ bash
mkdi r - p / host
mount - t nt f s / dev/ sda1 / host
l oset up / dev/ l oop0 / host / nt f sr oot
cr ypt set up l uksOpen / dev/ l oop0 r oot - - key- f i l e / r oot / key. t xt
mkdi r - p / mnt / nt f sr oot
mount - t ext 4 / dev/ mapper / r oot / mnt / nt f sr oot
#! / bi n/ bash
umount / mnt / nt f sr oot
r mdi r / mnt / nt f sr oot
cr ypt set up l uksCl ose r oot
l oset up - d / dev/ l oop0
umount / dev/ sda1
r mdi r / host
b) Execute the first, so that the file in the NTFS partition is mounted at / mnt / nt f sr oot .
c) Mount the external disk partition that contains the / for the system E:
mkdi r - p / mnt / hdr oot
mount - t ext 4 / dev/ sdc2 / mnt / hdr oot
d) Copy the whole system to the new location:
cp - r pv / mnt / hdr oot / * / mnt / nt f sr oot /
After this point, the external hard drive is no longer necessary. It can be unmounted and put aside.
e) Edit the / mnt / nt f sr oot / et c/ f st ab to reflect the new changes:
/ dev/ mapper / swap swap swap def aul t s 0 0
/ dev/ mapper / r oot / ext 4 acl , user _xat t r 1 1
Step 7: Modify GRUB to load the appropriate initrd and load the respective system. Open the / boot / grub
/ menu. l st and make it look something like this:
def aul t 0
t i meout 8
Installing Linux in a file in an NTFS partition | Brainder. http://brainder.org/2011/11/29/installing-linux-in-a-file-in-an-ntfs-partition/
7 of 9 16-Sep-13 11:04 AM
t i t l e openSUSE 12. 1 - - Har d Di sk
r oot ( hd0, 2)
ker nel / vml i nuz- 3. 1. 0- 1. 2- deskt op spl ash=si l ent qui et showopt s vga=0x317
i ni t r d / i ni t r d- 3. 1. 0- 1. 2- har ddi sk
t i t l e openSUSE 12. 1 - - USB Fl ash Dr i ve
r oot ( hd0, 2)
ker nel / vml i nuz- 3. 1. 0- 1. 2- deskt op spl ash=si l ent qui et showopt s vga=0x317
i ni t r d / i ni t r d- 3. 1. 0- 1. 2- usbf l ash
t i t l e Wi ndows XP
map ( hd1) ( hd0)
map ( hd0) ( hd1)
r oot nover i f y ( hd1, 0)
makeact i ve
chai nl oader +1
The only difference between the first and second entries is the initrd. In the first, the initrd will load the system that
is inside the file in the NTFS partition, and which will contain the full graphical system. In the second, the initrd will
load the simpler, text-only install on the USB flash drive.
Concluding remarks
We installed a modern, cutting edge Linux distribution inside a regular file in an NTFS partition. We did not modify
any of the settings of Windows XP, neither did we partition the internal hard disk, nor tampered with the Windows
registry or did anything that would violate any company rule. The boot loader, the kernel, and the initrd were left in
an external device, the USB flash drive. When seen from Windows, only two inoffensive and unsuspicious files
appear in the C: \ . They contain the quiescent power of Linux.
Installing Linux in a file in an NTFS partition | Brainder. http://brainder.org/2011/11/29/installing-linux-in-a-file-in-an-ntfs-partition/
8 of 9 16-Sep-13 11:04 AM
ONE THOUGHT ON INSTALLING LINUX IN A FILE IN AN NTFS PARTITION
Pingback: Installing openSUSE 12.1 on a Flash Drive - Page 2
Share thi s:
Li ke thi s:
Be the first to like this.
This entry was posted in Linux and tagged external, linux, ntfs, opensuse, usb by A. M. Winkler. Bookmark
the permalink [http://brainder.org/2011/11/29/installing-linux-in-a-file-in-an-ntfs-partition/] .
Installing Linux in a file in an NTFS partition | Brainder. http://brainder.org/2011/11/29/installing-linux-in-a-file-in-an-ntfs-partition/
9 of 9 16-Sep-13 11:04 AM

Вам также может понравиться