ubuntu系统debootstrap的使用

来源:互联网 发布:php数组按键名排序 编辑:程序博客网 时间:2024/06/05 16:55

debootstrap是debian/ubuntu下的一个工具,用来构建一套基本的系统(根文件系统)。生成的目录符合Linux文件系统标准(FHS),即包含了/boot、/etc、/bin、/usr等等目录,但它比发行版本的Linux体积小很多,当然功能也没那么强大,因此,只能说是“基本的系统”。

fedora下(centos亦可用)有类似功能的工具:febootstrap。观察这两个工具名称,可以看到debootstrap使用debian前缀“de”,而febootstrap使用fedora前缀“fe”,bootstrap含义为“引导”,并如果做过LFS的话,对它了解会比较多,而在编译gcc编译器时也有涉及到这个概念。不过debootstrap或febootstrap似乎没有表达出“引导”的意思。

制作根文件系统,在PC上最全面的莫过于LFS(linux form scratch,从头做一个linux系统),而嵌入式中,busybox是不二之选。当然,随着芯片的强大及一些linux发行版本的进化,也有桌面系统直接用于嵌入式领域中,比如ubuntu,就有支持硬件浮点的工具直接下载。

ubuntu默认没有安装debootstrap,安装十分简单,执行下列命令即可:

# sudo apt-get install debootstrap

使用也十分简单,命令格式为:

sudo debootstrap --arch [平台] [发行版本代号] [目录]

比如下面的命令

sudo debootstrap --arch i386 trusty /mnt

即是构建x86(32位)平台ubuntu最新发行版14.04(代号为trusty)的基本系统,存放到/mnt目录下。

当前debootstrap支持的发行版本可以在/usr/share/debootstrap/scripts查看,而各发行版代号,可以到http://en.wikipedia.org/wiki/List_of_Ubuntu_releases查看。比如gutsy是7.10的代号,precise是12.04的代号,等等。

输入上述命令后,就会从网络下载相关的文件,当看到

I: Configuring python-central...I: Configuring ubuntu-minimal...I: Configuring libc-bin...I: Configuring initramfs-tools...I: Base system installed successfully.

即表示成功。如果看到

E: Failed getting release file http://archive.ubuntu.com/ubuntu/dists/trusty/Release

或卡在

I: Retrieving Release

则可能是网络原因。

下载的文件在/mnt/var下,如:

$ tree.├── debootstrap│ ├── debootstrap.log│ └── debpaths└── var├── cache│ └── apt│ └── archives│ ├── adduser_3.113+nmu3ubuntu3_all.deb│ ├── apt_1.0.1ubuntu2_i386.deb

其中adduser_3.113是14.04对应的adduser。从这里也可以确认其下载的是哪一发行版的软件。

下面使用chroot进入/mnt目录,并查看linux版本。

latelee@ubuntu:~$ cd /mnt/latelee@ubuntu: /mnt$ lsbin boot dev etc home lib media mnt opt proc root run sbin srv sys tmp usr varlatelee@ubuntu: /mnt$ sudo -s[sudo] password for latelee:root@ubuntu: /mnt# chroot .root@ubuntu:/# lsbin boot dev etc home lib media mnt opt proc root run sbin srv sys tmp usr varroot@ubuntu:/# cat etc/issueUbuntu 14.04 LTS \n \lroot@ubuntu:/# ls /proc/ -ltotal 0root@ubuntu:/# ifconfigWarning: cannot open /proc/net/dev (No such file or directory). Limited output.root@ubuntu:/# uname -aLinux ubuntu 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:12 UTC 2014 i686 i686 i686 GNU/Linux

因为当然系统使用的并不这个新的系统,因此/proc并没有内容,而内核依然是当前系统所用的版本。

使用光盘不成功

W: Failure trying to run: chroot /home/latelee/test_sys mount -t proc proc /procW: See /home/latelee/test_sys/debootstrap/debootstrap.log for details

注:

本文并没有过多技术含量,仅是在学习过程中碰见了debootstrap而写点笔记。本文所用环境均是虚拟机vmware。

附录:

未完事宜:

限于时间,目前还没有实际启动新的系统。

一些涉及到ubuntu根文件系统构建的资源:

http://www.virtuatopia.com/index.php/Building_a_Debian_or_Ubuntu_Xen_Guest_Root_Filesystem_using_debootstrap

https://wiki.ubuntu.com/DebootstrapChroot

https://help.ubuntu.com/10.04/installation-guide/i386/linux-upgrade.html

http://www.thegeekstuff.com/2010/01/debootstrap-minimal-debian-ubuntu-installation/

http://askubuntu.com/questions/442610/debootstrap-warning-during-installation-12-04-lts-server-vmware-virtual-mach

https://help.ubuntu.com/lts/installation-guide/i386/index.html

查看ubuntu各发行版本wiki:

http://en.wikipedia.org/wiki/List_of_Ubuntu_releases

查看ubuntu安装包:

http://packages.ubuntu.com/

后记:本想写稍有点技术含量的文章,把过程所涉及到的知识点都提及,但发现自己文笔不复如前,还是按流水账那样写出来比较畅快些。

李迟,写于2014年10月20日中午休息前

本文固定链接: http://www.latelee.org/using-gnu-linux/ubuntu-debootstrap.html



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

一、下载完后要做的事情

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

添加latelee用户,密码也是latelee:

# adduser latelee
# addgroup –system admin
# adduser latelee admin
# passwd latelee

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

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

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

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

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

——————————

安装常用编译工具:
apt-get install build-essential

如果要编译内核,则要安装ncurses库:
apt-get install libncurses5-dev libncurses5

安装其它杂七杂八的:
apt-get install vim tree

(其它的,自行安装)


二、添加硬盘


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

# fdisk /dev/sdb

这里我分了2个区,sdb1是交换区,大小为4GB,剩下的是sdb2分区。

其实分区的操作很简单的,输入上面命令后,只需要输入以下内容即可(感谢强大的“默认”功能)。

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

格式化:

mkswap /dev/sdb1

mkfs.ext4 /dev/sdb2

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

cd /mnt

mkdir /tmp/tmp

mount /dev/sdb2 /tmp/tmp

cp -a * /tmp/tmp

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


三、内核


内核可以自己编译,也可以直接拿已有系统的。

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


四、引导


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

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

使用sdb2挂载到/mnt中,运行update-grub

在第一块硬盘的grub.cfg上添加sdb

# 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

# 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,示例如下:

#根分区“/”

UUID=e2aad928-67bc-4c63-8a76-d49cafe95bd /               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文件:

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
}
改后的:

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′

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来格式化。

示例:

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(下面挂载时要用到)。
# 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。
# 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文件:

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目录。

# 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日补充

 

本文固定链接: http://www.latelee.org/using-gnu-linux/ubuntu-debootstrap-ii.html


0 0