# run Gentoo LiveDVD 12.1 on a usb without ruining data

## crquan

[list]from "Gentoo Linux releases 12.1 LiveDVD" faq we are able to run it on a usb stick

Posted on April 1, 2012 by Robin H. Johnson

http://www.gentoo.org/news/20120401-livedvd.xml

http://www.gentoo.org/proj/en/pr/releases/12.1/faq.xml

Gentoo Linux Hybrid ISO - Put a Cow in your boxen EDITION https://forums.gentoo.org/viewtopic-t-906782.html [/post][post=906782][/post]

```
Code Listing 2.3: Copy the image

# dd if=image.iso of=/dev/sdb
```

But this way has two major problems:

1) existing data on the usb will be ruined;

2) the remaining space of a larger usb cannot be utilized;

since I have a 8GB usb stick and have used over 2GB to save some stuff; I want to make it to run Gentoo LiveDVD but don't want to ruin existing data

to files on this usb organized clean, I don't want to just extract all files of the iso into root of the usb; I made it this way;

1) create a named "gentoo" folder in the usb;

2) copy Gentoo LiveDVD 2012.1 iso into this "gentoo" dir;

3) extract "boot" and "isolinux" from iso into this "gentoo" dir;

4) rename all "isolinux" to "syslinux", isolinux.bin to syslinux.bin, isolinux.cfg to syslinux.cfg;

5) # syslinux -d /gentoo/syslinux /dev/sdb1

After that, this usb becomes bootable usb; if you try boot with this, it can show same bootmenu as running from cdrom, but if you really boot some entry, it would fail at some later linux booting step;

So we need to change the syslinux.cfg entry, I just show the first one as example

```
--- /mnt/cdrom/isolinux/isolinux.cfg   2012-03-27 21:06:47.000000000 +0000

+++ /mnt/iso/gentoo/syslinux/syslinux.cfg   2012-04-20 03:35:46.000000000 +0000

@@ -33,8 +33,8 @@ F10 f10.msg fmenu.png

 

 label gentoo-x86_64

   MENU LABEL Gentoo ^x86_64

-  kernel /boot/gentoo

-  append root=/dev/ram0 init=/linuxrc dokeymap aufs looptype=squashfs loop=/image.squashfs cdroot initrd=/boot/gentoo.igz vga=791 splash=silent,theme:livecd-12.0 console=tty1 quiet 

+  kernel ../boot/gentoo

+  append aufs_mem=2G cdroot=LABEL=PENDRIVE isoboot=gentoo/gentoo12.iso initrd=../boot/gentoo12.xz vga=791 splash=silent,theme:livecd-12.0 console=tty1 quiet doload=ehci-hcd,usb-storage

 TEXT HELP

   Boot the default x86_64 Gentoo kernel

 ENDTEXT
```

Some explains:

1) the kernel file "/boot/gentoo" I didn't put them into root dir of usb, so I choose relative parent dir of where syslinux.cfg exists; some reason apply to "initrd="

2) I don't know why "root=/dev/ram0 init=/linuxrc" exists there, it's just not required, and is default value by initramfs;

3) I use default us keymap, so removed dokeymap;

4) "aufs looptype=squashfs loop=/image.squashfs" also can be automatically detected, also removed;

5) I use "aufs_mem=2G" for better 3D desktop experience;

6) I use "cdroot=LABEL=PENDRIVE" to tell initramfs to find real cd root; the "PENDRIVE" here is my sdb1 vfat volume label, you can change it to your own;

7) add "isoboot=gentoo/gentoo12.iso" that's where I put iso in the usb;

 :Cool:  "doload=ehci-hcd,usb-storage" for some rare case where driver for the usb stick is not automatically loaded;

9) you can keep the old gentoo.igz as initrd, or I have some changes to "/init" and "/etc/initrd.scripts" inside the initramfs, as well I repacked it with xz for better compression ratio;

   these changes to initramfs are to better support usb;

without these changes you won't be able to access other files in the same usb stick, where other features provided by the LiveDVD are the same;

I made some changes to initrd to make the live desktop be able to access files in the usb stick, here is the diff

```
--- gentoo-initramfs.orig/init   2012-04-20 03:55:13.822983374 +0000

+++ gentoo-initramfs/init   2012-04-20 03:53:50.150985224 +0000

@@ -387,6 +387,10 @@ then

    fi

 fi

 

+# Run debug shell if requested

+good_msg 'After bootstrapCD'

+rundebugshell

+

 # Determine root device

 good_msg 'Determining root device...'

 while true

@@ -491,13 +495,16 @@ do

 done

 # End determine root device

 

+# Run debug shell if requested

+good_msg "End determine root device: ${REAL_ROOT}"

+rundebugshell

+

 #verbose_kmsg

 

 # If CD root is set determine the looptype to boot

 if [ "${CDROOT}" = '1' ]

 then

    good_msg 'Determining looptype ...'

-   cd "${NEW_ROOT}"

 

    # Find loop and looptype

    [ -z "${LOOP}" ] && find_loop

@@ -608,6 +615,11 @@ CDBOOT=1

 # Execute script on the cdrom just before boot to update things if necessary

 cdupdate

 

+# Extract firmware

+if [ -e "${NEW_ROOT}/mnt/iso/gentoo/firmware.tar" ]; then

+   tar -C "${NEW_ROOT}/lib/firmware" -xf "${NEW_ROOT}/mnt/iso/gentoo/firmware.tar"

+fi

+

 if [ "${SUBDIR}" != '' -a -e "${CHROOT}/${SUBDIR}" ]

 then

    good_msg "Entering ${SUBDIR} to boot"

@@ -652,6 +664,10 @@ umount /sys || echo '*: Failed to unmoun

 umount /proc || echo '*: Failed to unmount the ramdisk /proc!'

 echo -e "${BOLD}.${NORMAL}"

 

+# Run debug shell if requested

+good_msg "Before exec /sbin/switch_root -c /dev/console ${CHROOT} ${REAL_INIT:-/sbin/init} ${INIT_OPTS}"

+rundebugshell

+

 exec /sbin/switch_root -c "/dev/console" "${CHROOT}" "${REAL_INIT:-/sbin/init}" "${INIT_OPTS}"

 

 # If we get here, something bad has happened

--- gentoo-initramfs.orig/etc/initrd.scripts   2012-04-20 03:55:13.823983373 +0000

+++ gentoo-initramfs/etc/initrd.scripts   2012-04-20 03:53:49.710985233 +0000

@@ -78,6 +78,10 @@ findmediamount() {

 

       for x in $*

       do

+         case "${x}" in

+         LABEL\=*|UUID\=*) x=`findfs "${x}" 2>/dev/null`

+         esac

+

          # Check for a block device to mount

          if [ -b "${x}" ]

          then

@@ -283,7 +287,7 @@ findnfsmount() {

 }

 

 check_loop() {

-   if [ "${LOOP}" = '' -o ! -e "mnt/cdrom/${LOOP}" ]

+   if [ "${LOOP}" = '' -o ! -e "${NEW_ROOT}/mnt/cdrom/${LOOP}" ]

    then

    

       bad_msg "Invalid loop location: ${LOOP}"

@@ -1075,22 +1079,20 @@ getdvhoff() {

 }

 

 setup_aufs() {

-        ( cd ${NEW_ROOT}

         # Setup aufs directories

         mkdir /mnt

-        for i in dynamic static; do mkdir /mnt/${i}; done

+        for i in dynamic static iso cdrom; do mkdir /mnt/${i}; done

+        for i in iso cdrom; do mount --move ${NEW_ROOT}/mnt/${i} /mnt/${i}; done

 

         good_msg "Loading aufs module"

         modprobe aufs > /dev/null 2>&1

 

-   mount -t squashfs -o loop,ro "mnt/cdrom${LOOPEXT}${LOOP}" "/mnt/static"

+   mount -t squashfs -o loop,ro "/mnt/cdrom${LOOPEXT}${LOOP}" "/mnt/static"

         mount -t tmpfs -o size=${aufs_mem:-420M} none /mnt/dynamic

-        mount -t aufs -o br:/mnt/dynamic:/mnt/static aufs ${NEW_ROOT}

-

-        mkdir /newroot/mnt/dynamic /newroot/mnt/static

+        umount ${NEW_ROOT}; mount -t aufs -o br:/mnt/dynamic:/mnt/static aufs ${NEW_ROOT}

         echo "aufs / aufs defaults 0 0">${NEW_ROOT}/etc/fstab

 

-        for i in dynamic static; do mount --move /mnt/${i} /newroot/mnt/${i}; done

-        )

+        for i in dynamic static iso cdrom; do mkdir ${NEW_ROOT}/mnt/${i}; done

+        for i in dynamic static iso cdrom; do mount --move /mnt/${i} ${NEW_ROOT}/mnt/${i}; done

 }
```

So finally I made a perfect usb stick that can boot Gentoo LiveDVD with capability to save data in the same usb, also I have put required nonfree b43 firmware as firmware.tar under "gentoo" of the usb; that it could drive my HP laptop wireless automatically; with remaining free space of the usb, I can even save a movie there; take one usb, play everywhere

if anyone need the prepared modified "gentoo12.xz" initramfs file, I can upload it somewhere, with xz compression, it's now 7.9MB; know where has a good place to paste binary file?

----------

## crquan

reported bugid

https://bugs.gentoo.org/show_bug.cgi?id=413039

and set blocks on:

Bug 378027 - [TRACKER] Gentoo 12.1 Hybrid ISO

----------

## djdunn

kudos, I would suggest maybe getting this moved to Documentation, Tips & Tricks?

----------

## John R. Graham

Moved from Gentoo Chat to Documentation, Tips & Tricks.

- John

----------

## crquan

Some hints on how to use this patch: (was posted to the bugid and sync'ed here)

https://bugs.gentoo.org/show_bug.cgi?id=413039#c2

Please extract the gentoo.igz file, apply the patch in the 2nd code snippet in above post: it mainly changed these two script of the initrfamfs:

```

+++ gentoo-initramfs/init   2012-04-20 03:53:50.150985224 +0000

+++ gentoo-initramfs/etc/initrd.scripts   2012-04-20 03:53:49.710985233 +0000
```

After applied, re-make the gentoo.igz by

```
$ (cd gentoo12-initramfs/; find |cpio --owner=0:0 -H newc -o) |gzip -v9 >gentoo12.igz
```

Or I prefer in xz format because it saves space:

```
$ (cd gentoo12-initramfs; find |cpio --owner=0:0 -H newc -o) |xz -v --check=crc32 >gentoo12.xz
```

the 1st code changes in the link is technically not required since syslinux boot menu give you a chance to change it during runtime:

So, with the above gentoo.igz, the steps to make a bootable usb is:

   assume it's sdb1 mounted somewhere (I have only vfat usb, don't want to try reformat, but I think both vfat, ntfs, or extX series most linux file system would be supported );

1) create a named "gentoo" folder in the usb;

2) copy Gentoo LiveDVD 2012.1 iso into this "gentoo" dir;

3) extract "boot" and "isolinux" from iso into this "gentoo" dir;

4) copy above made gentoo.igz or gentoo.xz into this /gentoo/boot/ dir, to replace the original one copied from iso;

4) rename all "isolinux" to "syslinux", isolinux.bin to syslinux.bin, isolinux.cfg to syslinux.cfg;

5) # syslinux -d /gentoo/syslinux /dev/sdb1

These steps could be script-able in future;

Then for this usb, we never touched any other contents outside "/gentoo/" and make it bootable now: you can try boot with it, but if you didn't change syslinux.cfg, you need to change parameters on runtime, for syslinux running, Press 'Tab' to change it:

it's default like this:

> /boot/gentoo root=/dev/ram0 init=/linuxrc dokeymap aufs looptype=squashfs loop=/image.squashfs cdroot initrd=/boot/gentoo.igz vga=791 splash=silent,theme:livecd-12.0 console=tty1 quiet

You can change it to

> ../boot/gentoo aufs_mem=2G cdroot=LABEL=PENDRIVE isoboot=gentoo/gentoo12.iso initrd=../boot/gentoo12.xz vga=791 splash=silent,theme:livecd-12.0 console=tty1 quiet doload=ehci-hcd,usb-storage

This is what I tested running well, but not all required:

1) ../boot/gentoo is required because this kernel file is not put in the

   root of the usb, you can use the absolute name "/gentoo/boot/gentoo" ok,

   or this style of relative path, syslinux is reading that according

   where syslinux.cfg exists, "/gentoo/syslinux/syslinux.cfg" is where

   I saved it, so this relative path "../boot/gentoo" works for me;

2) "root=/dev/ram0 init=/linuxrc" is not required at all,

   it's no use but no harm;

3) aufs_mem=2G, adjust according how much memory do you have on your machine;

4) cdroot=LABEL=PENDRIVE, PLEASE change to your actual usb device name,

   you may use like "cdroot=/dev/sdc1", but it may be different for each time

   so you may want to use your file system label, like I'm using

   "PENDRIVE" as vfat label for my /dev/sdc1;

5) isoboot=gentoo/gentoo12.iso, when I copied the iso, I just renamed it to

   this shorter name, please change it to your actual iso name,

   this is relative to root of the usb; because it's read by linux init scripts

6) initrd=../boot/gentoo12.xz, use relative same as above "../boot/gentoo" change;

   please point to your actual re-made gentoo12.igz filename;

7) doload=ehci-hcd,usb-storage, to make sure usb-storage kernel module load,

   since I was using "nodetect" to save time booting;

After this boot and user login, the usb content is available in Nautilus or other similar file manager, mounted at "/mnt/iso" by init scripts, this is kind of confusing, but I was trying to keep above changes to init scripts as minimum,

And "/mnt/iso" is default by init scripts mounted as read-only, the user could sudo "mount -o remount,rw /mnt/iso" to make it read-write; and I have another change in init scripts to make it default read-write;

The "/mnt/iso" is not umount-able since the actual gentoo12.iso is loop mounted from there, and layered with a tmpfs to the runtime root;

Thanks!

----------

## Silentsand74

Could you please post somewhere (for instance rapidshare.com) the prepared "gentoo12.xz" initramfs file?

----------

## ustcscgy

this is totally awsome!

Because in a new verison livedvd --- gentoo 20121221 ,the end of world edition, when i try to make a liveusb by using unetbootin, it can not display a graphic menu;

while by using official dd method, windows xp can't use it anymore,even if you add a new partition and format it into fat32.

Your method is absolutely right! By using the iso as a whole and replace isolinux with syslinux and pass right args to kernel, it works! And I think it is a universal way to make liveusb (I'll try that later).

Now I want to write a little more notice here:

1) use the same syslinux version all the time: syslinux version 4 and 5 are a lot different. and the origin iso is using isolinux 4.05. But I am using syslinux 5.00 and it works.(no test for syslinux 4.x)

2) in order to make usb bootable, dd a syslinux-provided mbr to your /dev/sdX:

dd if=/usr/share/syslinux/mbr.bin of=/dev/sdb

3) if you get a error like me : failed to load vesamenu.c32 

"failed" instead of "not found"

Here is important:

cp /usr/share/syslinux/*.c32 /mnt/usb/gentoo/syslinux/

because in syslinux-5.0.0, some of these .c32 files are dynamic linked.

Good Luck and enjoy the best livedvd on the planet!!

UPDATE:

A little more thing:

1) it seems that ONLY when you prepare your partition on usb disk in linux is OK. no windows, no ntfs. USE FDISK ON LINUX!

2) For a USB flash drive, windows can only recognize it's first partition, so, prepare a large fat32 partition for iso storage and windows storage and a second ext partition for live-linux storage.

3) to make it bootable, 

fdisk /dev/sdX

a          [stand for active]

1          [partition number which .iso is in, highly recommend 1]

w

q

----------

## ddddd

Hey crquan, tnx for the great tip. I'm trying your method right now with new Gentoo 20121221 LiveDVD - End Of World Edition http://www.gentoo.org/news/20121221-livedvd.xml

I did everything that you advised except some changes:

4) I didn't rename isolinux default path into syslinux path. I only renamed isolinux.bin to syslinux.bin and isolinux.cfg to syslinux.cfg.

5) I used slightly different syslinux command:

```
syslinux.exe -maf -d /gentoo/isolinux h:
```

(I'm trying to make ntfs flash usb stick on Windows with cygwin and other tools. That's why you see .exe in some commands.)

[*] gentoo.igz on new Gentoo 20121221 LiveDVD has been already packed with xz. So, the algorithm of modifying this file is the following now:

```
mkdir.exe tmp

cd tmp/

cat ../gentoo.igz |xz -d |cpio -idmv

# patch or modify init and /etc/initrd.scripts files

find |cpio --owner=0:0 -H newc -o |xz -v --check=crc32 > ../gentoo_fixed.igz
```

[*] init and /etc/initrd.scripts files were changed in this release. I'm trying to make your changes correct but cannot ATM mount cdrom (flash) after booting from the flash. It gives me the following errors:

```
>> Attempting to mount media: /dev/sdd1

!! Media not found

...

Could not find the root block device in .
```

Does anybody can tell me what am I doing wrong?

----------

## NeddySeagoon

ddddd,

Your error 

```
Could not find the root block device in .
```

should have listed all the block devices that the kernel found, just before the full stop.

In your case, the list is empty, that means that no device drivers for your block devices are available to the kernel.

----------

## ddddd

When I type "shell" and trying to mount smth by hands it gives me the following:

```
# mount -r -t ntfs /dev/sdd1 /mnt/cdrom/

mount: mounting /dev/sdd1 on /mnt/cdrom failed: No such device
```

But I have /dev/sdd and /dev/sdd1.

Without ntfs parameter also no success:

```
#mount -r /dev/sdd1 /mnt/cdrom/

mount: mounting /dev/sdd1 on /mnt/cdrom failed: Invalid argument
```

But if I try to mount my usual HDD it is successful:

```
#mount -r /dev/sdc2 /mnt/cdrom/
```

And I can see my files on sdc2 too.

So, you say that the kernel has no drivers for this usual USB flash with ntfs filesystem? It's standard USB 2.0 Flash Transcend 16 Gb JetFlash 600, 620 High-Speed stick. What should I do? Is it happen because of ntfs? Should I try to format my flash stick to fat32 and try again?

----------

## NeddySeagoon

ddddd,

Its unlikely the kernel ntfs drive is present because nobody uses it.  At one time it was very dangerous to ntfs file systems. That was 'fixed' by reducing its write capabilities to changing the content of existing files, provided the file size was unchanged.  Everyone that needs ntfs now uses ntfs-3g.  ntfs-3g is a user space application that depends on the kernel providing FUSE support.

For ntfs to work, you need ntfs-3g in the initrd.  In place of ntfs in your mount command, try -t ntfs3g and -t ntfs-3g, since I'm not sure if the filesystem name gets the hypen or not.

I think its unlikely that  ntfs-3g will be in the initrd so it may still fail.

You have a much better chance with vfat. 

Why do you put a journalled fs, like ntfs, on a FLASH stick. The journal accesses are bad for speed and FLASH life.

----------

## ddddd

OK, I'll try to use ntfs-3g or vfat. This flash was formatted by some guy in my uni when fat32 couldn't  eat dvd iso file with more then 4Gb space. I think that journalling for flash disk is bad too because of it's limited number of rewrites in comparison with HDDs. But fat32 is dying filesystem too.

----------

## NeddySeagoon

ddddd,

The 4G limit is hardwired into vfat (fat32) by some design choices made when fat12 was invented. It isn't going to go away.

----------

## ddddd

Ntfs-3g doesn't work for me too. I reformatted flash into fat32 and tryed to boot. Mouting phase was successful. But it gave me another error this time:

```
>> Determining looptype ...

!! Invalid loop location: /image.squashfs

!! Please export LOOP with a valid location, or reboot and pass a proper loop=...

!! kernel command line!
```

Can anybody give me advise concerning this error? Why does it happen and what should I do?

----------

## ddddd

Adding "aufs looptype=squashfs loop=/image.squashfs" back to syslinux.cfg doesn't help me too. I tried to analyze init scripts in gentoo.igz. And it looks like there's an error in above patch. It should be

```
if [ "${LOOP}" = '' -o ! -e "${CDROOT_PATH}/${LOOP}" ]
```

instead of

```
if [ "${LOOP}" = '' -o ! -e "${NEW_ROOT}/${CDROOT_PATH}/${LOOP}
```

because /newroot/mnt/cdrom/ doesn't mount before that check. But /mnt/cdrom (${CDROOT_PATH}) mounts before this loop check in the script.

I changed that string and tried to boot again. LOOP error disappeared. But kernel hangs this time with a following message:

```
drm/i810 does not support SMP

RAMDISK: incomplete write (-28 != 4096)

XZ-compressed data is corrupt

Kernel panic - not syncing: VFS: unable to mount root fs on unknown-block(2,0)

Pid: 1, comm: swapper/0 Not tainted 3.6.8-gentoo-r1 #1

Call Trace:

...
```

I tried to search forums with "export loop valid location " and found the following post:

 *Quote:*   

> I unpacked the initramfs, added the isofs module (since genkernel doesn't put that in automatically?) and created an entry for it in /etc/modules/fs. Now my ISO boots up fine in QEMU without any intervention!

 

https://forums.gentoo.org/viewtopic-p-3998906.html#3998906

I looked inside /etc/modules/fs of gentoo.igz file:

```
ext2

ext3

ext4

btrfs

reiserfs

jfs

nfs

xfs

zfs

fuse
```

I don't see isofs there. I can't find isofs module inside lib\modules\3.6.8-gentoo-r1\ of that gentoo.igz too. If I try to add isofs into doload list of syslinux.cfg kernel hangs without any messages (black screen and two keyboard indicators are blinking).

Why does kernel hangs? Am I did smth wrong? Any advices on that message?

----------

## ddddd

I added ramdisk_size=1048576 into append section of syslinux.cfg. "RAMDISK: incomplete write (-28 != 4096)" error disappeared. But I still have:

```
drm/i810 does not support SMP 

XZ-compressed data is corrupt 

Kernel panic - not syncing: VFS: unable to mount root fs on unknown-block(2,0) 

Pid: 1, comm: swapper/0 Not tainted 3.6.8-gentoo-r1 #1 

Call Trace: 

...
```

----------

## ddddd

It looks like gentoo.igz was wrongly copied into flash stick. I repacked it and copied into flash again. The above errors dissapeared but I sill have kernel panic:

```
>> Determining looptype ...

>> Mounting squashfs filesystem

>> Copying read-write image contents to tmpfs

>> No cdupdate.sh script found, skipping...

>> Booting (initramfs)...

>> Before exec /sbin/switch_root -c /dev/console /newroot /sbin/init

switch_root: can't execute '/sbin/init': No such file or directory

Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000100

Pid: 1, comm: switch_root Tainted: P         O 3.6.8-gentoo-r1 #1

Call Trace: 

...... 
```

Can anybody give me advice about this error?

I have /sbin/init file in gentoo.igz, it has the following inside:

```
!<symlink>яю. . / i n i t   
```

I think I understood the problem. This file is probably a symlink. I was repacking gentoo.igz file in Windows on ntfs filesystem. That's why symlinks probably weren't exported correctly? If it is then I have serious problem. Where will I find ext2 filesystem for example without linux livecd at least?   :Crying or Very sad: 

----------

## NeddySeagoon

ddddd,

```
switch_root: can't execute '/sbin/init': No such file or directory 
```

is the error.   That's the init script the kernel tries to run to bring the system up.

Its claiming its not there.  Is it there a problem with exec permissions, either on the file or on the filesystem ?

----------

## ddddd

I will check exec permissions. I repacking gentoo.igz in cygwin. I thought that symlinks isn't supported by ntfs in linux way, but now I think that probably cygwin should emulate somehow linux symlinks in proper way.

----------

## ddddd

This files structure I have inside gentoo.igz archive:

```
UserX@dragon /cygdrive/d/1/tmp

$ ls -l sbin/ |grep init

lrwxrwxrwx 1 UserX Отсутствует       7 дек 30 17:25 init -> ../init

UserX@dragon /cygdrive/d/1/tmp

$ ls -l |grep init

-rwxr-xr-x  1 UserX Отсутствует 21765 дек 30 18:37 init

lrwxrwxrwx  1 UserX Отсутствует     4 дек 30 17:25 linuxrc -> init
```

So, I think cygwin emulates symlinks correctly. Execution flags are there too. How to check permissions on the file system?

The other guy has the same bug there:

https://forums.gentoo.org/viewtopic-t-946026-highlight-switchroot+execute+sbin+init+directory.html

----------

