ubuntu系统debootstrap的使用之二:启动

上一篇文章:《ubuntu系统debootstrap的使用》只是介绍了如何从网络上制作(下载)一个新的、简单的根文件系统到本地中,但它只具外形而未能实际运用价值。因为只下载了一个最基本的系统,换言之,deboostrap并没有帮你做内核、引导的事情。所以要自己来做。本文就将这个基本的系统放到设备上跑一跑。为了方便测试,将上面的系统放到另外一块硬盘中。

一、下载完后要做的事情

添加用户(不添加不行,否则启动新系统,无法登陆):
添加latelee用户,密码也是latelee:

1
2
3
4
5
6
7
8
9
# adduser latelee
# addgroup --system admin
# adduser latelee admin

# passwd latelee

Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

虽然很多人不建议,但我还是添加了root用户密码

1
2
3
4
# passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

查看/etc/shadow看看root密码是否设置了。如果没有设置,是没有一堆乱码的。下面是加了密码的文件:

1
2
# cat /etc/shadow
root:$6$0apsPMDz$rG8PKojxmE/F/nh0WBA25eDsBL31H5sWQllF2OKp2/xy/xS47q37diFQXVQY/wl3FYEVvDooLTZZOFrUbJlCi.:16351:0:99999:7:::

安装常用编译工具:

1
apt-get install build-essential

如果要编译内核,则要安装ncurses库:

1
apt-get install libncurses5-dev libncurses5

安装其它杂七杂八的:

1
apt-get install vim tree

(其它的,自行安装)

二、添加硬盘

在VMware添加外设十分简单,在VM->Settings...->Add...中已经有很多外设可添加。这里选Hard Disk,大小选100GB或200GB都可以(按实际情况占物理盘空间,不是一开始就占了100GB空间的)。此时系统还不能识别出第二块硬盘,要重启虚拟机系统,才能识别到,一般是sdb。下面格式化之。

1
# fdisk /dev/sdb

这里我分了2个区,sdb1是交换区,大小为4GB,剩下的是sdb2分区。
其实分区的操作很简单的,输入上面命令后,只需要输入以下内容即可(感谢强大的“默认”功能)。

1
2
3
4
n 回车 回车 回车 +4GB  (第一个分区OK)
n 回车 回车 回车 回车   (第二个分区OK)
p (查看分了几个区)
w (写入分区表并退出)

格式化:

1
2
3
mkswap /dev/sdb1

mkfs.ext4 /dev/sdb2

将上面的新系统拷贝到sdb2中。

1
2
3
4
5
6
7
cd /mnt

mkdir /tmp/tmp

mount /dev/sdb2 /tmp/tmp

cp -a * /tmp/tmp

(不局限于命令,明白意思即可自行改)

三、内核

内核可以自己编译,也可以直接拿已有系统的。
本来想编译一个比较小一点的内核,但没时间研究那么多的模块选项。如果使用默认配置编译,大概有4000多个模块。占用空间约1.8GB,是比较庞大的。为了省事,可以直接用ubuntu14.04自带的内核。

四、引导

(此节内容部分未完全经测试测试,略具参考意义)

1
grub-install /dev/sdb (不加分区,好像不行)

使用sdb2挂载到/mnt中,运行update-grub
在第一块硬盘的grub.cfg上添加sdb

1
2
3
4
5
6
7
8
9
10
11
12
13
# update-grub
Generating grub configuration file ...
Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.
Found linux image: /boot/vmlinuz-3.13.0-32-generic
Found initrd image: /boot/initrd.img-3.13.0-32-generic
Found linux image: /boot/vmlinuz-3.13.0
Found initrd image: /boot/initrd.img-3.13.0
Found linux image: /boot/vmlinuz-3.13.0.old
Found initrd image: /boot/initrd.img-3.13.0
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
Found Ubuntu 14.04 LTS (14.04) on /dev/sdb2
done

查看分区uuid

1
2
3
4
5
# blkid
/dev/sdb1: UUID="6b367f92-b39c-4ba9-8f93-5ff31769d6c8" TYPE="swap"
/dev/sdb2: UUID="e2aad928-67bc-4c63-8a76-d49cafe95bdb" TYPE="ext4"
/dev/sda1: UUID="f6e3980f-c507-4c57-99cb-6bf13d62f8be" TYPE="ext4"
/dev/sda5: UUID="ed578acd-5ff6-4c98-9f63-6b3ab532f057" TYPE="swap"

按上结果,编辑挂载分区脚本/etc/fstab,示例如下:

1
2
3
4
5
6
7
8
9
10
11
#根分区“/”

UUID=e2aad928-67bc-4c63-8a76-d49cafe95bdb /   ext4 errors=remount-ro 0   1

# 其它分区,如有,则要一一添加。本例是没有的
#/boot 
# UUID=49f71055-4a5b-4ef1-a5ae-d40c507a30e1 /boot   ext4 defaults  0   2

#交换分区
# swap
UUID=6b367f92-b39c-4ba9-8f93-5ff31769d6c2 none swap sw  0   0

原来的grub.cfg文件:

1
2
3
4
5
6
7
8
9
10
11
12
menuentry 'Ubuntu 14.04 LTS (14.04) (on /dev/sdb2)' --class gnu-linux --class gnu --class os $menuentry_id_option 'osprober-gnulinux-simple-e2aad928-67bc-4c63-8a76-d49cafe95bdb' {
     insmod part_msdos
     insmod ext2
     set root='hd1,msdos2'
     if [ x$feature_platform_search_hint = xy ]; then
       search --no-floppy --fs-uuid --set=root --hint-bios=hd1,msdos2 --hint-efi=hd1,msdos2 --hint-baremetal=ahci1,msdos2  e2aad928-67bc-4c63-8a76-d49cafe95bdb
     else
       search --no-floppy --fs-uuid --set=root e2aad928-67bc-4c63-8a76-d49cafe95bdb
     fi
     linux /boot/vmlinuz-3.13.0 root=UUID=f6e3980f-c507-4c57-99cb-6bf13d62f8be ro find_preseed=/preseed.cfg auto noprompt priority=critical locale=en_US quiet splash $vt_handoff
     initrd /boot/initrd.img-3.13.0
}

改后的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
menuentry 'Ubuntu 14.04 LTS (14.04) (on /dev/sdb2)' --class gnu-linux --class gnu --class os $menuentry_id_option 'osprober-gnulinux-simple-e2aad928-67bc-4c63-8a76-d49cafe95bdb' {
     insmod part_msdos
     insmod ext2
     set root='hd1,msdos2'
     if [ x$feature_platform_search_hint = xy ]; then
       search --no-floppy --fs-uuid --set=root --hint-bios=hd1,msdos2 --hint-efi=hd1,msdos2 --hint-baremetal=ahci1,msdos2  e2aad928-67bc-4c63-8a76-d49cafe95bdb
     else
       search --no-floppy --fs-uuid --set=root e2aad928-67bc-4c63-8a76-d49cafe95bdb
     fi
     linux /boot/vmlinuz-3.13.0 root=UUID=e2aad928-67bc-4c63-8a76-d49cafe95bdb ro find_preseed=/preseed.cfg auto noprompt priority=critical locale=en_US quiet splash $vt_handoff

     initrd /boot/initrd.img-3.13.0
}


menuentry 'Ubuntu, with Linux 3.13.0' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-advanced-f6e3980f-c507-4c57-99cb-6bf13d62f8be' {
          recordfail
          load_video
          gfxmode $linux_gfx_mode
          insmod gzio
          insmod part_msdos
          insmod ext2
          set root='hd0,msdos1'
          if [ x$feature_platform_search_hint = xy ]; then
            search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  f6e3980f-c507-4c57-99cb-6bf13d62f8be
          else
            search --no-floppy --fs-uuid --set=root f6e3980f-c507-4c57-99cb-6bf13d62f8be
          fi
          echo     'Loading Linux 3.13.0 ...'
          linux     /boot/vmlinuz-3.13.0 root=UUID=f6e3980f-c507-4c57-99cb-6bf13d62f8be ro find_preseed=/preseed.cfg auto noprompt priority=critical locale=en_US quiet splash $vt_handoff
          echo     'Loading initial ramdisk ...'
          initrd     /boot/initrd.img-3.13.0
     }

说明:
root=’hd1,msdos2’,第二块硬盘的启动区,变成hd1,msdos2。第一块硬盘分区,是root=’hd0,msdos1’。

1
2
uname -a
Linux ubuntu 3.13.0 #3 SMP Wed Oct 8 09:38:48 CST 2014 i686 i686 i686 GNU/Linux

五、设备上的测试

上面第四点写得比较乱,因为当时不得其法,边学边搞,最后莫名其妙地搞定了。这里再在一个设备上进行实验,并按操作的步骤进行描述。其中根文件系统源文件已经修改好,内核也移植好。
我所用的设备上有2个SATA口,所以可以使用一个硬盘启动,再挂一个硬盘,如果不具备此条件,也可以使用SATA转USB,使用PC来操作。另外,还可以用Live CD来做,如果空间不足够,挂载NFS来解决。

1、将硬盘分区。并用mkfs.ext4、mkswap来格式化。

示例:

1
2
3
4
   Device Boot       Start           End       Blocks   Id  System
/dev/sdb1 2048 4196351 2097152   83  Linux
/dev/sdb2 4196352 14682111 5242880   83  Linux
/dev/sdb3 14682112 15684095  500992   83  Linux

格式化后,挂载分区。这里我指定sdb1为根分区,sdb2为程序分区,sdb3为交换分区。

2、查看分区的UUID(下面挂载时要用到)。

1
2
3
4
# blkid
/dev/sdb1: UUID="5726537f-778d-408a-827e-dc76b1a316ba" TYPE="ext4"
/dev/sdb2: UUID="f9e6893a-5181-4e1a-91e5-31873259a1c9" TYPE="ext4"
/dev/sdb3: UUID="22f27499-7be5-4b06-8a21-67b145c98fa3" TYPE="swap"

3、编辑自动挂载脚本/etc/fstab。

1
2
3
4
5
6
7
# new 2014.11.20
# / was on /dev/sda1
UUID=5726537f-778d-408a-827e-dc76b1a316ba /   ext4 errors=remount-ro 0   1
#/opt /dev/sda3
UUID=f9e6893a-5181-4e1a-91e5-31873259a1c9 /opt ext4 defaults  0   2
# swap
UUID=22f27499-7be5-4b06-8a21-67b145c98fa3 none swap sw  0   0

4、编辑grub.cfg

在grub.cfg中,内核路径不能错。如果/boot单独一个分区,则直接写成/vmlinuz-3.17.1,如果不是,则要写成/boot/vmlinuz-3.17.1。之前就因为这个原因耽误了一段时间。另外root指定的分区名称也要与实际的一致。
下面是一个十分简单但可启动的grub.cfg文件:

1
2
3
4
5
6
7
set default=0 #默认第一项,按下面的格式可以写很多项
set timeout=3 # 超时时间
menuentry 'Late Lee Ubuntu' {
  set root='hd0,msdos1'
  echo "vmlinuz...."
  linux   /boot/vmlinuz-3.17.1 root=/dev/sda1 ro console=tty0 console=ttyS0,115200n8
}

5、安装grub

这里要安装的硬盘为sdb,上面所说的根分区我挂载到/tmp/latelee目录。

1
2
3
# grub-install /dev/sdb --root-directory=/tmp/latelee
Installing for i386-pc platform.
Installation finished. No error reported.

完成上面的步骤后,就OK了。

李迟,2014年11月10日,周一晚,2014年11月20日补充